简体   繁体   中英

How to recursively list all files and directories

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?

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 ie

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 question shows the relative path to a file, but does not show the owner and group.

这个怎么样:

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

Use tree . Few linux distributions install it by default (in these dark days of 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/

Use:

tree -p -u -g -f -i

or

tree -p -u -g -f

or check the man page for many other useful arguments.

适用于Linux Debian:

find $PWD -type f     

find comes close:

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.

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

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

find . -ls

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.

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";
}

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

#!/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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM