Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 90 down vote accepted

You can try

stat -c "%a %n" *

Replace * with the relevant directory or the exact filename that you want to examine.

From the man page of stat,

-c  --format=FORMAT
          use  the  specified  FORMAT instead of the default; output a newline after
          each use of FORMAT
%a     Access rights in octal
%n     File name

Usage:

  • With files:

    $ stat -c "%a %n" ./Documents/Udev.html 
    664 ./Documents/Udev.html
    
  • With folders:

    $ stat -c "%a %n" ./Documents/
    755 ./Documents/
    

(Reference)

share|improve this answer

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.

stat -c '%A %a %n' *

%A Access rights in human readable form

%a Access rights in octal

%n File name

Octal numbers and permissions

You can use octal number to represent mode/permission:

r: 4
w: 2
x: 1

For example, for file owner you can use octal mode as follows. Read, write and execute (full) permission on a file in octal is 0+r+w+x = 0+4+2+1 = 7

Only Read and write permission on a file in octal is 0+r+w+x = 0+4+2+0 = 6

Only read and execute permission on a file in octal is 0+r+w+x = 0+4+0+1 = 5

Use above method to calculate permission for group and others. Let us say you wish to give full permission to owner, read & execute permission to group, and read only permission to others, then you need to calculate permission as follows: User = r+w+x = 0+4+2+1 = 7 Group= r+w+x = 0+4+2+0 = 6 Others = r+w+x = 0+0+0+1 = 1

Effective permission is 761.

Source: http://kmaiti.blogspot.com/2011/09/umask-concept.html

share|improve this answer
    
The initial 0 represent special bit too: pastebin.com/6ymkFt71 –  Braiam Jul 28 '13 at 1:45

As detailed here, you can create an alias lso that acts like ls -l but displays permissions also in octal.

alias lso="ls -alG | 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}'"
share|improve this answer
    
doesn't work with directories - awk: read error (Is a directory) –  techtonik Jul 25 at 7:25

For portability, you can use perl:

$ perl -e 'printf "%04o %s\n", (stat)[2] & 07777, $_ for @ARGV' *.txt
0644 1.txt
0644 2.txt
0644 3.txt
0644 4.txt
0600 PerlOneLiner.txt
0664 perl.txt

If you want to notice when an error occurs, try:

perl -e '
for (@ARGV) {
    print "$!: $_\n" and next unless -e;
    printf "%03o %s\n", (stat)[2] & 07777, $_;
}
' *.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.