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

时间:2009-02-01 18:59:43

标签: linux shell

在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

accepted answer to this question显示文件的相对路径,但不显示所有者和组。

8 个答案:

答案 0 :(得分:24)

这个怎么样:

find . -exec ls -dl \{\} \; | awk '{print $3, $4, $9}'

答案 1 :(得分:19)

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

使用:

tree -p -u -g -f -i

tree -p -u -g -f

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

答案 2 :(得分:17)

适用于Linux Debian:

find $PWD -type f     

答案 3 :(得分:10)

find接近:

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

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

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

答案 4 :(得分:8)

你已经得到了一个有效的答案,但作为参考,你应该能够在BSD上做到这一点(我已经在mac上测试过了):

find . -ls

答案 5 :(得分:4)

如果您喜欢使用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";
}

答案 6 :(得分:0)

我发现的简单方法是:

ls -lthr / path_to_directory / *

“ *”-表示级别。

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

答案 7 :(得分:-4)

使用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, $_);
}

也许比其他答案更冗长,但是应该这样做,并且应该省去你必须记住输入的内容。 (代码未经测试。)