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:
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:
or
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:
However, one problem i'm finding is that if there's whitespace in the filenames, the output produces an error:
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?
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: