There is a chmod command to set file permissions, but can I get file permissions in octal mode (such as 755) from the command line?
There is a chmod command to set file permissions, but can I get file permissions in octal mode (such as 755) from the command line?
You can try
Replace
*
with the relevant directory or the exact filename that you want to examine.From the man page of stat,
Usage:
With files:
With folders:
(Reference)
File permissions in Linux can be displayed in octal format using Linux stat command.
Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, Navigate to the directory where you want to find the file permissions in octal mode.
%A Access rights in human readable form
%a Access rights in octal
%n File name
Source: http://kmaiti.blogspot.com/2011/09/umask-concept.html
As detailed in “755”-style permissions with ‘ls’ by Adam Courtemanche on AgileAdam.com, you can create an alias
lso
that acts likels -l
but slightly processes the output1 to display permissions also in octal. This adds a leading column showing three-digit2 octal permissions. As written, this works for most files and directories, but it does not work properly if the sticky or setuid/setgid bits are set.3This has a serious shortcoming, though, as techtonik points out. You cannot pass arguments to this
lso
alias as you would to thels
command, because they are taken as additional arguments toawk
instead. Thus you cannot runlso
on a specific file or directory, nor can you pass any options (like-F
, or--color
) tolso
.The fix is to define
lso
as a function rather than an alias.If you're trying this out interactively in your shell, run
unalias lso
to remove the alias--you can do that either before or after you define the function. If you're putting it into a file that is sourced, such as~/.bashrc
, just take out thealias
line and add the function definition.Why does this work? Unlike aliases, bash shell functions can take positional parameters, i.e., command-line arguments.
"$@"
expands to the full argument list, causing arguments to thelso
function to be passed tols
. (Unlike an alias definition, a function body is not quoted; hence it was necessary to remove the\
characters before$
and"
.)Since you can pass options to
lso
when defined this way as a function, you may wish to remove the-a
and-G
options from the definition--you can pass them manually in cases where you want them. (The-l
option is required for details like file permissions to be shown at all, so there is no benefit to removing it.)Thanks to techtonik for pointing out the limitation in defining
lso
as an alias, thus motivating me to expand this post with material about making it a function instead.1One may note this seems to flout the general rule about not parsing output from
ls
.ls
produces very human-readable output; this introduces idiosyncrasies and limitations making it generally unsuitable as input for other commands. In this case we parsels
since we wish to preserve the exact behavior ofls
except our one added change.2One limitation of this alias, which also applies to the function version shown below it, and which may be considered a bug, is that it only displays three octal digits even when the fourth octal digit is zero. As jfmercer has rightly pointed out, the octal digits displayed here don't reflect the sticky bit if present, nor setuid or setgid bits.
3More seriously than merely not showing the fourth octal digit is that this method assumes they are not set, and if they are--if you see
t
,s
, orS
in the permission string--then you should disregard the octal digits. This because the bits are inferred from the permissions string in a way that does not account for sticky setuid/setgid bits.Just extending\simplifying previous 'stat' related answers:
You can simply run:
The output will contain octal permission along with other info.
Details(stat version and example):
Note the: (0644/-rw-r--r--)
For portability, you can use
perl
:If you want to notice when an error occurs, try:
You can use
find
with the-printf
action.ls
doesn't show octal permissions, but you can use thisfind
-based workaround:For example, to check my Videos directory:
The
%m
format specifier tells the-printf
action to print octal permissions, while the%f
format specifier causes it to print the filename.You can pass multiple filenames to
find
. You can even use globs (e.g.,find * -printf "%m:%f\n"
).You don't have to use a test like
-name
or-iname
; it is sufficient to pass the names of the files or directories you are interested in as starting points tofind
. That is, supply their names as arguments immediately after the wordfind
, as shown above.find
gives you great control over how it shows output. There are two modifications in particular that you might find useful:By default,
find
recurses subdirectories, similar tols -R
. If you don't wantfind
to visit the subdirectories of the starting points you pass to it, you can add-maxdepth 0
(or use-maxdepth
with other values to indicate how deep you want it to go).%f
only shows a filename, so iffind
has to recurse to get to a file, you might not know where it is located. To show a path, beginning with whichever starting point the file was found under, use%p
instead.See
man find
for more information about using thefind
command.Another Method (
ls
andawk
)This can be used to list all directory files with their permissions:
This is essentially the same command as in Adam Courtemanche's
lso
alias, which that answer cited, just run as a single command. If you're only using this once, or on rare occasion, then you might not want to bother writing it as an alias or shell function.You can use an alias that will show listing somewhat similar to "ls":