Tuesday, September 11, 2018

Linux lsattr Command Tutorial for Beginners (with Examples)

https://www.howtoforge.com/linux-lsattr-command

We recently discussed chattr, a command that you can use to change file attributes on a Linux file system. To list file attributes, there's a separate command, dubbed lsattr. In this tutorial, we will discuss this tool using some easy to understand examples.
But before we do that, it's worth mentioning that all examples mentioned in this article have been tested on an Ubuntu 18.04 LTS machine.

Linux lsattr command

As already mentioned in the introduction part above, the lsattr command in Linux lists file attributes on stdout. Following is its syntax:
lsattr [ -RVadlpv ] [ files...  ]
Here's how the tool's man page defines it:
       lsattr lists the file attributes on a second extended file system.
Following are some Q&A-styled examples that should give you a good idea on how the command works.

Q1. How to use lsattr command?

Basic usage is quite simple. Just execute 'lsattr' without any command line options. Of course, you need to provide a file name as input.
Here's an example:
lsattr file1.txt
And here's the output:
--------------e--- file1.txt
In addition to 'e' (in the output above), there can be several other letters in the output. Following excerpt (taken from chattr man page) should give you a better idea:
       The  letters 'aAcCdDeijPsStTu' select the new attributes for the files:
       append only (a), no atime updates (A), compressed (c), no copy on write
       (C), no dump (d), synchronous directory updates (D), extent format (e),
       immutable (i), data journalling  (j),  project  hierarchy  (P),  secure
       deletion  (s),  synchronous  updates  (S),  no tail-merging (t), top of
       directory hierarchy (T), and undeletable (u).

       The following attributes are read-only, and may be listed by  lsattr(1)
       but  not  modified by chattr: encrypted (E), indexed directory (I), and
       inline data (N).

Q2. How to make lsattr recursively work on directories?

This can be done using the -R command line option.
For example:
lsattr -R Downloads/HTF-review/
Here's the output the above command produced on my system:
How to make lsattr recursively work on directories
Note that if you want to display all files in directories (including .) use the -a command line option.

Q3. How to make lsattr treat directories as normal files?

By default, if you provide a directory name/path as input to lsattr, it produces information related to files contained in that directory.
How to make lsattr treat directories as normal files
However, if you want, you can force lsattr to treat directory as a file, and produce file attribute information for it. This you can do using the -d command line option.
lsattr -d option

Q4. How to make lsattr list file's project and version number?

This can be done using the -p and -v command line options. Following screenshot shows both these options in action:
How to make lsattr list file's project and version number

Conclusion

Agreed, lsattr might not fall into the category of most used commands, but if you use chattr, then it's a must-know command. Here, in this tutorial, we have discussed the majority of the command line options it offers. To learn more about the lsattr command, head to its man page.

No comments:

Post a Comment