[英]How to recursively list all files and directories
在Free BSD上使用tcsh shell,有没有办法递归列出所有文件和目录,包括文件的所有者,组和相对路径?
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
我想要的输出如下:
owner group ./relative/path/to/file
这个怎么样:
find . -exec ls -dl \{\} \; | awk '{print $3, $4, $9}'
使用树 。 默认情况下,很少有Linux发行版安装它(在这些只有GUI的黑暗时代 :-),但它总是在标准存储库中可用。 它也应该可用于* BSD,请参阅http://mama.indstate.edu/users/ice/tree/
采用:
tree -p -u -g -f -i
要么
tree -p -u -g -f
或者查看手册页以获取许多其他有用的参数。
适用于Linux Debian:
find $PWD -type f
find
接近:
find . -printf "%u %g %p\n"
还有“%P”,如果您希望路径相对于指定目录,则会从文件名中删除前缀。
注意,这是GNU查找,我不知道BSD查找是否也支持-printf。
你已经得到了一个有效的答案,但作为参考你应该能够在BSD上做到这一点(我已经在mac上测试过了):
find . -ls
如果您喜欢使用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";
}
使用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, $_);
}
也许比其他答案更冗长,但应该做到这一点,并且应该省去你必须记住要键入的内容。 (代码未经测试。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.