3

I know that ls -l lists the permissions of every file in a directory but what is the command if I want to see the permissions of just a specific file?

0

4 Answers 4

6

To just get the mode:

stat -c %a file

(where file can also be a directory).

Note: this is with the stat command from the GNU Coreutils. Otherwise the solution is system dependent.

3

To get all the info provided by ls -l for a single file or folder, use the -d option and specify the file:

ls -ld filename
2

If your intention is to do something depending on the file permission then in some cases you can consider simple test (aka [ or [[) conditional statement:

  • -r file exists and read permission is granted
  • -w file exists and write permission is granted
  • -x exists and execute permission is granted

For example:

[ -w file ] && echo foo >> file
1

As "permissions" doesn't just cover octal unix permissions on modern Linux systems, I'd like to elaborate a little:

Apart from stat -c %a file @vinc17 suggested, there's stat -c %C file for the SELinux context on RHEL Systems, and getfacl file for volumes using ACLs.

namei -m /path/to/file might be helpful for finding out all octal permissions leading down the path to the file, as wrong permissions on the parent directories can inhibit access.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .