10+ chattr command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

Introduction to chattr command

In Linux, the file attributes are the settings that determine the file's behaviour. An attribute can determine whether a file can be modified, deleted, renamed, compressed, etc. chattr changes the attributes of files or directories in a Linux system. The attributes can be changed to make files secured so other users except superusers cannot modify the data in the file.  You can view the file attributes using lsattr command.

 

Some Common Files Attributes

Following are some of the attributes that can be set to the file.

  • A: The time record is not modified when you access the file.
  • a: The file can only be open in append mode for writing.
  • c: The file is automatically compressed on the disk by the kernel.
  • D: The changes are written synchronously on the disk.
  • d: A file is not backed up when the dump program is used.
  • E: It indicates a compressed file has a compression error.
  • e: The file is using extents for mapping the blocks on disk.
  • I: It is used by the htree code to indicate that a directory is being indexed using hashed trees.
  • i: A file cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file.
  • j: A file having the 'j' attribute has all of its data written to the ext3 journal before being written to the file itself.
  • s: When a file with the 's' attribute set is deleted, its blocks are zeroed and written back to the disk.
  • S: When a file is modified, the changes are written synchronously on the disk.

 

Syntax of chattr and lsattr command

The syntax of chattr command is:

$ sudo chattr [option] [mode] files

The format of a symbolic mode is +-=[acdeijstuADST]. '+'  is used to add the selected attributes to the existing attributes and '-' is used to remove the selected attributes. Whereas '=' causes the selected attributes to be the only attribute left. chattr allows you to add or remove multiple attributes at once.

Some options available in chattr command:

  • -R: Change attributes of directories and their contents recursively
  • -V: Display the verbose output and program version
  • -f: Suppress the error message

The syntax for lsattr command is:

$ lsattr [option] file

These are some options available in lsattr command.

  • -R: List directories and their contents recursively
  • -V: Display the program version
  • -a: List all files in directories

 

Different examples to use chattr and lsattr command

1. lsattr command to list the attributes of file or directory

You use lsattr command followed by a file or directory name to view the attributes.

$ lsattr file

Sample Output:

ubuntu@golinux:~$ lsattr test.txt
--------------e----- test.txt

When no file or directory is specified, lsattr command lists the attributes of all files and directories present in the working directory.

$ lsattr

lsattr command

 

2. chattr command to allow file to be opened in append mode only

The attribute 'a' should be added to make the file to be opened in only append mode.

$ sudo chattr +a file

Sample Output:

chattr command to open file only in append mode

 

3. chattr command to make any file immutable

You can add the 'i' attribute to make a file immutable. So you can use +i attribute to make sure a file cannot be deleted or modified or renamed.

$ chattr +i  file

Sample Output:

chattr command to make file immutable

 

4. chattr command to remove any attribute

You can remove the attribute using '-' followed by the attribute symbol.

$ sudo chattr -attribute file

Sample Output:

To remove the 'i' attribute, you can use the below command.

chattr command to remove any attribute

As you can see, a file can be modified after removing the 'i' attribute.

 

5. chattr command to set only selected attributes

The '=' symbol is used to set only the selected attributes to the file. Other attributes will be removed from the file.

$ sudo chattr =attr file

Sample Output:

chattr command to set only selected attributes

 

6. lsattr command to list the attributes of directories recursively

The -R option lists the attributes of directories and their files and sub-directories.

$ lsattr -R dir

Sample Output:

lsattr command to list attributes of directories recursively

 

7. chattr command to change attributes of directories recursively

You can also use -R option with chattr to change the attributes of directories and their contents recursively.

$ sudo chattr +a -R dir

Sample Output:

chattr command to change attributes of directories recursively

 

8. List all files in directories with lsattr command

The -a option can be used to list all files directories, including files that start with '.'.

$ lsattr -a [dir]

Sample Output:

lsattr command to list all files in directory

 

9. List directory like other files with lsattr command

Normally, lsattr lists all files in the directory. But with -d option, lsattr only lists the directory rather than listing its contents.

$ lsattr -d dir

Sample Output:

ubuntu@golinux:~$ lsattr -d Record
-----a--------e----- Record

 

10. Suppress error messages of chattr command

You can suppress or hide the error messages in the output displayed by the chattr command. It does not hide all error messages.

$ chattr -f +a file

Sample Output:

chattr command to suppress error message

 

11. chattr command to display the verbose output

Generally, chattr command does not display any output. You can use -V option to view the verbose output of chattr command. It also prints the chattr version.

$ sudo chattr -V +a file

Sample Output:

ubuntu@golinux:~$ sudo chattr -V +a system.txt
chattr 1.45.5 (07-Jan-2020)
Flags of system.txt set as -----a--------e-----

 

12. lsattr command to list the file's version/generation number

You can use -v option with lsattr to view the file's version/generation number.

$ lsattr -v file

Sample Output:

ubuntu@golinux:~$ lsattr -v system.txt
851053182 -----a--------e----- system.txt

 

Conclusion

This tutorial teaches you to use chattr and lsattr command in Linux. chattr helps to change attributes and lsattr helps to list attributes of files and directories in the Linux system. If you still have any confusion, please let us know in the comment section.

 

What's Next

15+ setfacl & getfacl command examples in Linux [Cheat Sheet]

 

Further Reading

man page for chattr command
man page for lsattr command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment