有没有办法看到一个path的所有中间目录的权限..?

我有一个文件path..是否有任何单一的命令来查看path中的所有中间目录的文件/目录权限..?

Solutions Collecting From Web of "有没有办法看到一个path的所有中间目录的权限..?"

我知道一个这样的命令,但它已经差不多30年了,尽管它仍然可以正常工作。 它应该用sh或perl来重写,但是这个工作:

#!/bin/csh -f # pup - print all dir perms from here up to root if ( $#argv == 0 ) then set args = ( $cwd ) else set args = ( $argv ) endif @ mult = ( $#argv > 1 ) foreach dir ( $args ) if ( $mult ) echo "${dir}:" loop: if ( "$dir" == '' ) set dir = '/' ls -lLd "$dir" if ( "$dir" == '/' ) goto next_for set dir = ( `echo "$dir" | sed 's;/[^/]*$;;'` ) goto loop next_for: end 

这是一个运行它的例子:

 % pup /usr/src/usr.bin/vi/vi drwxr-xr-x 3 root wsrc 1024 May 29 2008 /usr/src/usr.bin/vi/vi drwxr-xr-x 13 root wsrc 512 Aug 7 2008 /usr/src/usr.bin/vi drwxr-xr-x 229 root wsrc 4096 Aug 7 2008 /usr/src/usr.bin drwxrwxr-x 18 root wsrc 512 Aug 7 2008 /usr/src drwxr-xr-x 22 root wheel 512 Jan 28 2010 /usr drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 

它默认为cwd。 是的,输出不漂亮。 如果今天早上我有动力,也许我会重写它。


编辑

这是一个使用你的系统ls程序的重写。 它在处理符号链接方面具有优势:

 #!/usr/bin/env perl use strict; use warnings; use Cwd qw( getcwd abs_path ); use File::Basename qw( dirname ); sub ls { 0 == system("ls", "-lid", @_) } @ARGV = getcwd() unless @ARGV; for my $path (@ARGV) { if (-l $path) { ls($path); $path = abs_path($path); } while (ls($path) && $path !~ m{ \A /+ \z }x) { $path = dirname(abs_path($path)); } } exit; 

但我真的很讨厌ls格式化的东西。 例如:

 chthon(tchrist)% pup1 /dev/null /dev/zero /dev/tty /dev/stdin /dev/fd/0 ~ Mail 79283 crw-rw-rw- 1 root wheel 2, 2 Nov 7 07:53 /dev/null 77953 drwxr-xr-x 3 root wheel 22016 Oct 17 21:15 /dev 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 79284 crw-rw-rw- 1 root wheel 2, 12 Oct 25 2008 /dev/zero 77953 drwxr-xr-x 3 root wheel 22016 Oct 17 21:15 /dev 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 79280 crw-rw-rw- 1 root wheel 1, 0 Nov 4 15:17 /dev/tty 77953 drwxr-xr-x 3 root wheel 22016 Oct 17 21:15 /dev 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 79285 crw-rw-rw- 1 root wheel 22, 0 Oct 25 2008 /dev/stdin 77953 drwxr-xr-x 3 root wheel 22016 Oct 17 21:15 /dev 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 79308 crw-rw-rw- 1 root wheel 22, 0 Oct 25 2008 /dev/fd/0 77954 dr-xr-xr-x 2 root wheel 1024 Oct 25 2008 /dev/fd 77953 drwxr-xr-x 3 root wheel 22016 Oct 17 21:15 /dev 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 1143296 drwxr-xr-x 197 tchrist wheel 48128 Nov 7 07:53 /home/tchrist 2 drwxr-xr-x 15 root wheel 512 Aug 12 2008 /home 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 1146538 lrwx------ 1 root wheel 5 Oct 23 2008 Mail -> /mail 2 drwxr-xr-x 134 tchrist wheel 5120 Nov 7 07:43 /mail 2 drwxr-xr-x 21 root wheel 1024 Aug 23 2010 / 

明白了吗? 所以这是一个完全内置的。

因为我find2perlfind2perlfind2perl ,所以我永远找到了主要/次要的宏。 这可能不会在其他系统上做这些,而不是我测试的那些。

 #!/usr/bin/env perl use strict; use warnings; my $USE_SHELL_LS_PROGRAM = 0; use Cwd qw( getcwd abs_path ); use File::Basename qw( dirname ); @ARGV = getcwd() unless @ARGV; *ls = $USE_SHELL_LS_PROGRAM ? \&your_ls : \&my_ls; for my $path (@ARGV) { if (-l $path) { ls($path); $path = abs_path($path); } while (ls($path) && $path !~ m{ \A /+ \z }x) { $path = dirname(abs_path($path)); } } exit; ######## sub your_ls { 0 == system("ls", "-lid", @_); } sub my_ls { use POSIX qw( strftime ); my $name = shift(); my @rwx = qw(--- --x -w- -wx r-- rx rw- rwx); my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = CORE::lstat($name) or do { printf STDERR "$0: cannot lstat $name: $!\n"; return 0; }; my $pname = $name; $blocks or $blocks = int(($size + 1023) / 1024); my $perms = $rwx[$mode & 7]; $mode >>= 3; $perms = $rwx[$mode & 7] . $perms; $mode >>= 3; $perms = $rwx[$mode & 7] . $perms; substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _; substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _; substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _; if (-f _) { $perms = '-' . $perms; } elsif (-d _) { $perms = 'd' . $perms; } elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($name); } elsif (-c _) { $perms = 'c' . $perms; $size = sizemm($rdev); } elsif (-b _) { $perms = 'b' . $perms; $size = sizemm($rdev); } elsif (-p _) { $perms = 'p' . $perms; } elsif (-S _) { $perms = 's' . $perms; } else { $perms = '?' . $perms; } printf "%9u %-10s %4d %-8s %-8s %8s %s %s\n", $ino, $perms, $nlink, user($uid), group($gid), $size, strftime("%F %T", localtime $mtime), $pname; return 1; } sub sizemm { my $dev = shift; # these are almost always wrong: my $major = ($dev >> 8) & 0xff; my $minor = $dev & 0xff; # now fix the ones we know how to for ($^O) { if (/openbsd/) { $major = ($dev >> 8) & 0xff; $minor = ($dev & 0xff) | (($dev & 0xffff0000) >> 8); } if (/darwin/) { $major = ($dev >> 24) & 0xff; $minor = $dev & 0xffffff; } if (/solaris/) { $major = ($dev >> 18) & 0x3fff; $minor = $dev & 0x3ffff; } if (/linux/) { $major = (($dev >> 8) & 0xfff) | (($dev >> 32) & ~0xfff); $minor = ($dev & 0xff) | (($dev >> 12) & ~0xff); } } return sprintf("%3d, %3d", $major, $minor); } # cache user number to name conversions sub user { use User::pwent; my $uid = shift; our %user; $user{$uid} = getpwuid($uid)->name || "#$uid" unless defined $user{$uid}; return $user{$uid}; } # cache group number to name conversions sub group { use User::grent; my $gid = shift; our %group; $group{$gid} = getgrgid($gid)->name || "#$gid" unless defined $group{$gid}; return $group{$gid}; } 

例如:

 chthon(tchrist)% pup2 /dev/null /dev/zero /dev/tty /dev/stdin /dev/fd/0 ~ Mail 79283 crw-rw-rw- 1 root wheel 2, 2 2011-11-07 07:34:47 /dev/null 77953 drwxr-xr-x 3 root wheel 22016 2011-10-17 21:15:55 /dev 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 79284 crw-rw-rw- 1 root wheel 2, 12 2008-10-25 08:03:50 /dev/zero 77953 drwxr-xr-x 3 root wheel 22016 2011-10-17 21:15:55 /dev 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 79280 crw-rw-rw- 1 root wheel 1, 0 2011-11-04 15:17:35 /dev/tty 77953 drwxr-xr-x 3 root wheel 22016 2011-10-17 21:15:55 /dev 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 79285 crw-rw-rw- 1 root wheel 22, 0 2008-10-25 08:03:50 /dev/stdin 77953 drwxr-xr-x 3 root wheel 22016 2011-10-17 21:15:55 /dev 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 79308 crw-rw-rw- 1 root wheel 22, 0 2008-10-25 08:03:51 /dev/fd/0 77954 dr-xr-xr-x 2 root wheel 1024 2008-10-25 08:03:51 /dev/fd 77953 drwxr-xr-x 3 root wheel 22016 2011-10-17 21:15:55 /dev 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 1143296 drwxr-xr-x 197 tchrist wheel 48128 2011-11-07 04:16:49 . 2 drwxr-xr-x 15 root wheel 512 2008-08-12 16:51:23 /home 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 1146538 lrwx------ 1 root wheel 5 2008-10-23 14:50:24 Mail -> /mail 2 drwxr-xr-x 134 tchrist wheel 5120 2011-11-07 07:43:59 /mail 2 drwxr-xr-x 21 root wheel 1024 2010-08-23 11:43:46 / 

你可以运行这个代码:

 lsd() { local v="$1"; while :; do v="${v%/*}"; [[ "$v" && ! -f "$v" ]] || break; ls -ld "$v"; done; } lsd /usr/share/doc/acl/README drwxr-xr-x 2 root root 4096 14 mai 12:28 /usr/share/doc/acl/ drwxr-xr-x 145 root root 4096 4 nov. 06:23 /usr/share/doc/ drwxr-xr-x 263 root root 12288 4 nov. 06:23 /usr/share/ drwxr-xr-x 14 root root 4096 28 oct. 22:47 /usr/ 

编辑:添加本地关键字

编辑2:最后一个项目错误已解决

这是一个简单的while循环来完成这个工作:

 f="$PWD" while [ "$f" != "/" ] do ls -ld "$f" f=$(dirname "$f") done