Is there a way to see the permissions of all the i

2019-05-09 18:52发布

I have a file path.. Is there any single command to see the file/directory permissions of all the intermediate directories in the path..?

3条回答
孤傲高冷的网名
2楼-- · 2019-05-09 19:14

I know of one such command, but it is almost 30 years old although it still works fine. It should be rewritten in sh or perl, but this works:

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

And here is an example of running it:

% 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 /

It default to the cwd. Yes, the output is not pretty. If I get motivated this morning, maybe I’ll rewrite it.


EDIT

Here’s a rewrite that uses your system ls program. It has advantages in how it deals with symlinks:

#!/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;

But I really hate the way ls formats things. For example:

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 /

See what I mean? So here’s one that’s entirely built-in.

Took me forever to track down the major/minor macros, since the ls routine I stole from find2perl. It probably won’t do these right on other systems than the ones I test for.

#!/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};
}

For example:

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 /
查看更多
来,给爷笑一个
3楼-- · 2019-05-09 19:15

You can run this code :

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/

Edit: added local keyword

Edit2: The last item error is resolved

查看更多
\"骚年 ilove
4楼-- · 2019-05-09 19:28

Here is a simple while-loop which does the job:

f="$PWD"
while [ "$f" != "/" ]
do
    ls -ld "$f"
    f=$(dirname "$f")
done
查看更多
登录 后发表回答