Q. I need to find file permission and store the same to a shell variable. How do I find out the file permission without parsing ls -l output?
A. Use GNU stat command to display file or file system status. It has option to display output in specific format.
Display stat for /etc/passwd file
Type the following command:
$ stat /etc/passwd
Output:
File: `/etc/passwd' Size: 1675 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 7899368 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2007-12-05 08:20:31.000000000 +0530 Modify: 2007-12-04 23:36:50.000000000 +0530 Change: 2007-12-04 23:36:50.000000000 +0530
Print access rights in octal format:
$ stat -c %a /etc/passwd
Output:
644
Print access rights in human readable format, enter:
$ stat -c %A /etc/passwd
Output:
-rw-r--r--
Store access rights in octal format to a shell variable:
$ VAR=$(stat -c %a /etc/passwd)
$ echo $VAR
Other valid format sequences for files
- %b : Number of blocks allocated (see %B)
- %B : The size in bytes of each block reported
- %d : Device number in decimal
- %C : SELinux security context
- %D : Device number in hex
- %f : Raw mode in hex
- %F : File type
- %g : Group ID of owner
- %G : Group name of owner
- %h : Number of hard links
- %i : Inode number
- %n : File name
- %N : Quoted file name with dereference if symbolic link
- %o : I/O block size
- %s : Total size, in bytes
- %t : Major device type in hex
- %T : Minor device type in hex
- %u : User ID of owner
- %U : User name of owner
- %x : Time of last access
- %X : Time of last access as seconds since Epoch
- %y : Time of last modification
- %Y : Time of last modification as seconds since Epoch
- %z : Time of last change
- %Z : Time of last change as seconds since Epoch
🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 2 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Could you please tell me what is the number after the permission
ls -ltr
drwxrwxrwx 7 root users 8192 Feb 25 13:58 src
^^^ what is the seven stand for?
that’s hard links number