Can the Unix list command 'ls' output nume

2019-01-29 14:43发布

问题:

Is it possible when listing a directory to view numerical unix permissions such as 644 rather than the symbolic output -rw-rw-r--

Thanks.

回答1:

Use this to display the octal values (numerical chmod permissions) and file name:

stat -c '%a %n' *

For more options see stat.

For a more efficient way to do this using an alias, see my comment below.



回答2:

Closest I can think of (keeping it simple enough) is stat, assuming you know which files you're looking for. If you don't, * can find most of them:

/usr/bin$ stat -c '%a %n' *
755 [
755 a2p
755 a2ps
755 aclocal
...

It handles sticky, suid and company out of the box:

$ stat -c '%a %n' /tmp /usr/bin/sudo
1777 /tmp
4755 /usr/bin/sudo


回答3:

you can just use GNU find.

find . -printf "%m:%f\n"


回答4:

You can use the following command

stat -c "%a %n" *

Also you can use any filename or directoryname instead of * to get a specific result.

On Mac, you can use

stat -f '%A %N' *


回答5:

@The MYYN

wow, nice awk! But what about suid, sgid and sticky bit?

You have to extend your filter with s and t, otherwise they will not count and you get the wrong result. To calculate the octal number for this special flags, the procedure is the same but the index is at 4 7 and 10. the possible flags for files with execute bit set are ---s--s--t amd for files with no execute bit set are ---S--S--T

ls -l | awk '{
    k = 0
    s = 0
    for( i = 0; i <= 8; i++ )
    {
        k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
    }
    j = 4 
    for( i = 4; i <= 10; i += 3 )
    {
        s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
        j/=2
    }
    if ( k )
    {
        printf( "%0o%0o ", s, k )
    }
    print
}'  

For test:

touch blah
chmod 7444 blah

will result in:

7444 -r-Sr-Sr-T 1 cheko cheko   0 2009-12-05 01:03 blah

and

touch blah
chmod 7555 blah

will give:

7555 -r-sr-sr-t 1 cheko cheko   0 2009-12-05 01:03 blah


回答6:

You don't use ls to get a file's permission information. You use the stat command. It will give you the numerical values you want. The "Unix Way" says that you should invent your own script using ls (or 'echo *') and stat and whatever else you like to give the information in the format you desire.



回答7:

no, it can only print numercial uids/guids.



回答8:

Use this to display the Unix numerical permission values (octal values) and file name.

stat -c '%a %n' *

Use this to display the Unix numerical permission values (octal values) and the folder's sgid and sticky bit, user name of the owner, group name, total size in bytes and file name.

stat -c '%a %A %U %G %s %n' *

Add %y if you need time of last modification in human-readable format. For more options see stat.

Better version using an Alias

Using an alias is a more efficient way to accomplish what you need and it also includes color. The following displays your results organized by group directories first, display in color, print sizes in human readable format (e.g., 1K 234M 2G) edit your ~/.bashrc and add an alias for your account or globally by editing /etc/profile.d/custom.sh

Typing cls displays your new LS command results.

alias cls="ls -lha --color=always -F --group-directories-first |awk '{k=0;s=0;for(i=0;i<=8;i++){;k+=((substr(\$1,i+2,1)~/[rwxst]/)*2^(8-i));};j=4;for(i=4;i<=10;i+=3){;s+=((substr(\$1,i,1)~/[stST]/)*j);j/=2;};if(k){;printf(\"%0o%0o \",s,k);};print;}'"

Folder Tree

While you are editing your bashrc or custom.sh include the following alias to see a graphical representation where typing lstree will display your current folder tree structure

alias lstree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'"

It would display:

   |-scripts
   |--mod_cache_disk
   |--mod_cache_d
   |---logs
   |-run_win
   |-scripts.tar.gz


回答9:

Building off of the chosen answer and the suggestion to use an alias, I converted it to a function so that passing a directory to list is possible.

# ls, with chmod-like permissions and more.
# @param $1 The directory to ls
function lls {
  LLS_PATH=$1

  ls -AHl $LLS_PATH | awk "{k=0;for(i=0;i<=8;i++)k+=((substr(\$1,i+2,1)~/[rwx]/) \
                            *2^(8-i));if(k)printf(\"%0o \",k);print}"
}


回答10:

tl;dr: The quick answer to the question is, no ls cannot print numerical permissions, use stat.

Extended permissions will get lost in translation when fumbling around with awk.

e.g:

$ ls -la /usr/local/src/
total 8
drwxrwsr-x  2 root staff 4096 Oct 23 11:18 .
drwxrwsr-x 10 root staff 4096 Oct 23 11:18 ..

The proposed solution would print this:

$ ls -la /usr/local/src | awk '{>
total 8
765 drwxrwsr-x  2 root staff 4096 Oct 23 11:18 .
765 drwxrwsr-x 10 root staff 4096 Oct 23 11:18 ..

Which is utterly wrong.

stat on the other hand gets it right:

$ stat /usr/local/src File: /usr/local/src Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 801h/2049d Inode: 917803 Links: 2 Access: (2775/drwxrwsr-x) Uid: ( 0/ root) Gid: ( 50/ staff) Access: 2018-10-24 12:30:58.197907324 +0900 Modify: 2018-10-23 11:18:59.810677000 +0900 Change: 2018-10-23 11:18:59.810677000 +0900 Birth: -

Bonus from man stat:

$ stat -c %a /usr/local/src
2775

This has been tested on Debian stable.