Linux 如何递归列出所有文件和目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/501367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:57:10  来源:igfitidea点击:

How to recursively list all files and directories

linuxshell

提问by Ben

Using the tcsh shell on Free BSD, is there a way to recursively list all files and directories including the owner, group and relative path to the file?

在 Free BSD 上使用 tcsh shell,有没有办法递归列出所有文件和目录,包括所有者、组和文件的相对路径?

ls -alR comes close, but it does not show the relative path in front of every file, it shows the path at the top of a grouping i.e.

ls -alR 接近,但它没有在每个文件前面显示相对路径,它显示了分组顶部的路径,即

owner% ls -alR
total 0
drwxr-xr-x   3 owner  group  102 Feb  1 10:50 .
drwx------+ 27 owner  group  918 Feb  1 10:49 ..
drwxr-xr-x   5 owner  group  170 Feb  1 10:50 subfolder

./subfolder:
total 16
drwxr-xr-x  5 owner  group   170 Feb  1 10:50 .
drwxr-xr-x  3 owner  group   102 Feb  1 10:50 ..
-rw-r--r--  1 owner  group     0 Feb  1 10:50 file1
-rw-r--r--  1 owner  group     0 Feb  1 10:50 file2

What I would like is output like:

我想要的是输出如下:

owner group ./relative/path/to/file

The accepted answer to this questionshows the relative path to a file, but does not show the owner and group.

这个问题公认答案显示了文件的相对路径,但不显示所有者和组。

采纳答案by James Brady

How about this:

这个怎么样:

find . -exec ls -dl \{\} \; | awk '{print , , }'

回答by Torsten Marek

findcomes close:

find接近:

find . -printf "%u %g %p\n"

There is also "%P", which removes the prefix from the filename, if you want the paths to be relative to the specified directory.

还有“%P”,如果您希望路径相对于指定目录,它会从文件名中删除前缀。

Note that this is GNU find, I don't know if the BSD find also supports -printf.

注意这是GNU find,不知道BSD find是否也支持-printf。

回答by Chris Lutz

Use a shell script. Or a Perl script. Example Perl script (because it's easier for me to do):

使用 shell 脚本。或 Perl 脚本。示例 Perl 脚本(因为我更容易做):

#!/usr/bin/perl
use strict;
use warnings;
foreach(`find . -name \*`) {
  chomp;
  my $ls = `ls -l $_`;
  # an incomprehensible string of characters because it's Perl
  my($owner, $group) = /\S+\s+\S+\s+(\S+)\s+(\S)+/;
  printf("%-10s %-10s %s\n", $owner, $group, $_);
}

Perhaps a bit more verbose than the other answers, but should do the trick, and should save you having to remember what to type. (Code untested.)

也许比其他答案更冗长,但应该可以解决问题,并且应该让您不必记住要键入的内容。(代码未经测试。)

回答by slim

If you fancy using Perl don't use it as a wrapper around shell commands. Doing it in native Perl is faster, more portable, and more resilient. Plus it avoids ad-hoc regexes.

如果您喜欢使用 Perl,请不要将其用作 shell 命令的包装器。在原生 Perl 中执行此操作更快、更便携且更有弹性。此外,它避免了临时正则表达式。

use File::Find;
use File::stat;

find (\&myList, ".");

sub myList {
   my $st = lstat($_) or die "No $file: $!";

   print  getgrnam($st->gid), " ", 
          getpwuid($st->uid), " ", 
          $File::Find::name, "\n";
}

回答by Davide

Use tree. Few linux distributions install it by default (in these dark daysof only GUIs :-), but it's always available in the standard repositories. It should be available for *BSD also, see http://mama.indstate.edu/users/ice/tree/

使用。很少有 Linux 发行版会默认安装它(在这些只有 GUI 的黑暗日子里:-),但它在标准存储库中始终可用。它也应该可用于 *BSD,请参阅http://mama.indstate.edu/users/ice/tree/

Use:

用:

tree -p -u -g -f -i

or

或者

tree -p -u -g -f

or check the man page for many other useful arguments.

或查看手册页以获取许多其他有用的参数。

回答by Andrew

You've already got an answer that works, but for reference you should be able to do this on the BSDs (I've tested it on a mac) :

您已经得到了一个有效的答案,但作为参考,您应该能够在 BSD 上执行此操作(我已经在 mac 上对其进行了测试):

find . -ls

回答by Аз_10000

Works in Linux Debian:

适用于 Linux Debian:

find $PWD -type f     

回答by Ajith H

Simple way I found was this:

我发现的简单方法是这样的:

ls -lthr /path_to_directory/*

ls -lthr /path_to_directory/*

" * " - represents levels.

“ * ” - 代表级别。

Ajiths-MBP:test ajith$ ls -lthr *
test2:
total 0
-rw-r--r--  1 ajith  staff     0B Oct 17 18:22 test2.txt

test3:
total 0
-rw-r--r--  1 ajith  staff     0B Oct 17 18:22 test3.txt

test1:
total 0
-rw-r--r--  1 ajith  staff     0B Oct 17 18:21 test1.txt
drwxr-xr-x  3 ajith  staff    96B Oct 17 18:22 test1_sub_dir


Ajiths-MBP:test ajith$ ls -lthr */*
-rw-r--r--  1 ajith  staff     0B Oct 17 18:21 test1/test1.txt
-rw-r--r--  1 ajith  staff     0B Oct 17 18:22 test2/test2.txt
-rw-r--r--  1 ajith  staff     0B Oct 17 18:22 test3/test3.txt

test1/test1_sub_dir:
total 0
-rw-r--r--  1 ajith  staff     0B Oct 17 18:22 test1_sub_file.txt