How to recursively list all files and directories

前端 未结 8 1752
长发绾君心
长发绾君心 2021-02-01 03:32

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 cl

相关标签:
8条回答
  • 2021-02-01 03:57

    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.

    0 讨论(0)
  • 2021-02-01 04:05

    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.)

    0 讨论(0)
  • 2021-02-01 04:07

    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) :

    find . -ls
    
    0 讨论(0)
  • 2021-02-01 04:08

    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.

    0 讨论(0)
  • 2021-02-01 04:09

    Simple way I found was this:

    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
    
    0 讨论(0)
  • 2021-02-01 04:15

    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";
    }
    
    0 讨论(0)
提交回复
热议问题