我有一个文件路径..是否有任何单一命令可以查看路径中所有中间目录的文件/目录权限..?
答案 0 :(得分:4)
您可以运行此代码:
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:最后一项错误已解决
答案 1 :(得分:2)
我知道一个这样的命令,但它已经快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 /
明白我的意思?所以这里是一个完全内置的。
我永远地想要追踪主要/次要的宏,因为我从find2perl
偷走了ls例程。它可能不会在我测试的其他系统上做到这些。
#!/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-- r-x 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 /
答案 2 :(得分:2)
这是一个简单的while循环,可以完成这项任务:
f="$PWD"
while [ "$f" != "/" ]
do
ls -ld "$f"
f=$(dirname "$f")
done