How to show octal permissions for a working directory's files (Guide/Question)

C

CrazedNerd

Guest
I was wondering how i would show octal permissions for files with octals. Typically, we use "ls -l" to show permissions in human readable format, which looks something like this:

Code:
-rw-rw-r-- 1 user user 0 Jul 12 10:54 file

In octal format, this would be displayed as "664" instead of "-rw-rw-r--". If we wanted to show all files in our working directory (like with ls) but with octal permissions displayed on the left hand of the file name, we have two options:

Code:
ls | xargs stat -c '%a %n'

or

Code:
find -maxdepth 1 | xargs stat -c '%a %n'

Both above do almost the same thing: ls displays each non-hidden file, and find displays every file. I've added "-maxdepth 1" to keep find from listing files in subdirectories of the working directory. After our first command, the results are piped into "xargs", which takes the output of ls and find, and creates more arguments for stat. Stat executed with "-c %a %n" causes files/directories listed after it, which has been put into a list with "xargs", to be listed one newline after another (-c) with octal permissions (%a) and their name (%n). The results of these commands should be a list of each item like this:

Code:
755 Videos

However, one problem i'm finding is that if there's whitespace in the filenames, the output produces an error:

Code:
stat: cannot stat './VirtualBox': No such file or directory
stat: cannot stat 'VMs': No such file or directory

Here, stat is getting './VirtualBox VMs' as two seperate arguments because of the white space...I'm wondering if there's a way to circumvent this problem. Any ideas?
 
Last edited by a moderator:


However, one problem i'm finding is that if there's whitespace in the filenames, the output produces an error:

Code:
stat: cannot stat './VirtualBox': No such file or directory
stat: cannot stat 'VMs': No such file or directory

Here, stat is getting './VirtualBox VMs' as two seperate arguments because of the white space...I'm wondering if there's a way to circumvent this problem. Any ideas?

How about piping through sed? This seems to work for me.
Bash:
ls | sed 's/ /\\ /g' | xargs stat -c '%a %n'

Output:
Code:
740 .bash_aliases
740 .bashrc
...
720 VirtualBox VMs
755 Virtual Machines
 
How about piping through sed? This seems to work for me.
Bash:
ls | sed 's/ /\\ /g' | xargs stat -c '%a %n'

Output:
Code:
740 .bash_aliases
740 .bashrc
...
720 VirtualBox VMs
755 Virtual Machines
Nice, I have no experience with sed, I'll be sure to make an alias out of that command.
 
You could also use awk.
Code:
ls | xargs stat -c '%a %n' | awk '{ print $1,$2 }'
 
Here, stat is getting './VirtualBox VMs' as two seperate arguments because of the white space...I'm wondering if there's a way to circumvent this problem. Any ideas?
In those cases, you need to use "quotation marks around the whole thing".
You can use this:
Code:
stat -c '%a' /path/to/file
 
In those cases, you need to use "quotation marks around the whole thing".
You can use this:
Code:
stat -c '%a' /path/to/file
Yeah but the goal is to make a list by automating the display of each item. It's also better to never use whitespace when making file names, but I can't always control how files are generated from resulting installations like with VirtualBox.
 
Check out this script I wrote. It puts the octal in first column. Only issue is it doesn't preserve formatting/spaces.
Bash:
# include hidden
shopt -s dotglob

# iterate
for f in *
do
    echo -n $(stat "$f" -c '%a')" "
    echo $(ls -ld --color=always "$f")
done

Output:
Code:
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Public
722 drwx-w--w- 3 geoff geoff 4096 Apr 20 09:07 Shared
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Templates
700 drwx------ 4 geoff geoff 4096 May 10 14:22 .Trash-1000
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Videos
720 drwx-w---- 5 geoff geoff 4096 Apr 22 16:52 VirtualBox VMs
755 drwxr-xr-x 2 geoff geoff 4096 Jul 6 13:53 Virtual Machines

If anyone can fix the spacing, I'd appreciate.
 
You could also use awk.
Code:
ls | xargs stat -c '%a %n' | awk '{ print $1,$2 }'
while this is interesteing, it's still giving me the same white space error, but all the errors are huddled together at the top of the output which does make it better:
Code:
...
stat: cannot stat 'VirtualBox': No such file or directory
stat: cannot stat 'VMs': No such file or directory
 
Check out this script I wrote. It puts the octal in first column. Only issue is it doesn't preserve formatting/spaces.
Bash:
# include hidden
shopt -s dotglob

# iterate
for f in *
do
    echo -n $(stat "$f" -c '%a')" "
    echo $(ls -ld --color=always "$f")
done

Output:
Code:
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Public
722 drwx-w--w- 3 geoff geoff 4096 Apr 20 09:07 Shared
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Templates
700 drwx------ 4 geoff geoff 4096 May 10 14:22 .Trash-1000
700 drwx------ 2 geoff geoff 4096 Feb 25 19:08 Videos
720 drwx-w---- 5 geoff geoff 4096 Apr 22 16:52 VirtualBox VMs
755 drwxr-xr-x 2 geoff geoff 4096 Jul 6 13:53 Virtual Machines

If anyone can fix the spacing, I'd appreciate.
I ran this on my computer, and the spacing isn't perfect but there is only so much space in a line and this is neat because it's an octal version of "ls -l". Congrats!

what is "shopt -s dotglob"?
 
while this is interesteing, it's still giving me the same white space error, but all the errors are huddled together at the top of the output which does make it better:
Code:
...
stat: cannot stat 'VirtualBox': No such file or directory
stat: cannot stat 'VMs': No such file or directory
You could probably do something with find instead.
Code:
find . -print0 | xargs -0 stat -c '%a %n' | awk '{ print $1,$2 }'
But that might run into issues where you have sub directories with spaces in them. But when creating files and directories it's generally not a good idea to use spaces in those.
 
Last edited:
I ran this on my computer, and the spacing isn't perfect but there is only so much space in a line and this is neat because it's an octal version of "ls -l". Congrats!
Thanks! I was inspired.
what is "shopt -s dotglob"?
it tells the for loop to include hidden files.
 


Top