5 UNIX / Linux lsattr Command Examples to List File Attributes

lsattr command comes along with the e2fsprogs package, which will display the file attributes from a Linux second extended filesystem.

In this article, we’ll review few examples on how to use this command.

1. Basic Example

By default, when you use lsattr command without any argument, it will display all the files and directories in the current directory along with their file attributes as shown below.

# lsattr
-------------e- ./anaconda-ks.cfg
-------------e- ./autoconf.ac
-------------e- ./users.txt

You can also pass one or more file name as an argument to the lsattr command, which will list file attributes only for the given file names as shown below.

# lsattr users.txt
-------------e- users.txt

# lsattr /etc/passwd /etc/group
-------------e- /etc/passwd
-------------e- /etc/group

2. Display Version Number

When you use -v option, lsattr command will display the version number (also called as generation number) for each and every file and directory along with the file attributes as shown below.

# lsattr -v
18446744071785796889 -------------e- ./anaconda-ks.cfg
18446744071785796891 -------------e- ./autoconf.ac
18446744071785796892 -------------e- ./users.txt

3. Display Directory Only

When you pass a directory name as an argument to the lsattr command, it will display all the contents of the directory along with its file attributes as shown below.

# lsattr /etc/ssh/
-------------e- /etc/ssh/ssh_host_rsa_key.pub
-------------e- /etc/ssh/ssh_host_rsa_key
-------------e- /etc/ssh/moduli
-------------e- /etc/ssh/sshd_config
-------------e- /etc/ssh/ssh_host_dsa_key
-------------e- /etc/ssh/ssh_host_key.pub
-------------e- /etc/ssh/ssh_host_key
-------------e- /etc/ssh/ssh_host_dsa_key.pub
-------------e- /etc/ssh/ssh_config

But, when you pass -d option, it will treat the given directory as a file, and display only its attributes as shown below, instead of displaying its content.

# lsattr -d /etc/ssh/
-------------e- /etc/ssh/

4. Display Files and Directories Recursively

When you use -R option, lsattr will display all the files recursively. i.e If it finds a directory, it will display the content of the directory recursively.

# lsattr -R /etc/sysconfig/
-------------e- /etc/sysconfig/networking

/etc/sysconfig/networking:
-------------e- /etc/sysconfig/networking/devices

/etc/sysconfig/networking/devices:
-------------e- /etc/sysconfig/networking/profiles

/etc/sysconfig/networking/profiles:
-------------e- /etc/sysconfig/networking/profiles/default

/etc/sysconfig/networking/profiles/default:
-------------e- /etc/sysconfig/crond
-------------e- /etc/sysconfig/iptables.old
-------------e- /etc/sysconfig/udev
..
..

5. Display Hidden Files also

Just like the regular ls command, you can also pass -a option to the lsattr command, which will also display hidden files (file starting with a .) along with its file attributes as shown below.

# lsattr -a ~
-------------e- /root/anaconda-ks.cfg
-------------e- /root/.ssh
-------------e- /root/myxinetd.conf
-------------e- /root/httpd-pwd-file
-------------e- /root/bin
--------------- /root/..
-------------e- /root/.
-------------e- /root/.tcshrc
-------------e- /root/my-key.pub
-------------e- /root/.bashrc
-------------e- /root/.bash_profile
-------------e- /root/.bash_history
-------------e- /root/badblocks.log