Change file attributes in linux

6 Useful chattr command examples in Linux

In this article, I will take you through 6 Useful chattr command examples in Linux. Sometimes you might have encountered a scenario where multiple users working in a file or in a directory accidentally alter the file or delete the file hence causing data loss. This happens because multiple users has read and write access on that file. So you might think of a solution where users can only perform certain required operations on that file and only superuser should have the complete control over that file or directory.

This can be easily accomplished with chattr command where you can modify the attribute of a file or directory as per your requirement. We will see some real world examples of chattr command in below section. More on chattr Man Page.

Below are the list of the operators which can be used with the selected attributes.

  • + : The operator ‘+’ causes the selected attributes to be added to the existing attributes of the files.
  • — : The operator ‘-‘ causes the selected attributes to be removed from the file.
  • = : The operator ‘=’ makes the selected attributes to be the only attributes that the files have.

Below are the list of attributes that can be set/unset on a file using chattr command.

Synopsis

chattr [ -RVf ] [ -v version ] [ -p project ] [ mode ] files.

6 Useful chattr command examples in Linux<Change File Attributes data-lazy-src=

Now to add immutable flag on the above file, you can use chattr +i nifi-1.13.2-bin.tar.gz command as shown below.

root@localhost:~# chattr +i nifi-1.13.2-bin.tar.gz

If you check the set attributes again, you can see both i and e flag are set as shown below.

root@localhost:~# lsattr nifi-1.13.2-bin.tar.gz ----i---------e----- nifi-1.13.2-bin.tar.gz

Example 2: How to Recursively Change File Attributes in Linux

Like we have modified the attribute of a file in above example, it is possible to recursively modify the attribute of list of the files in a directory. In this example, we will recursively add immutable flag on all the files in nifi-1.13.2 directory. But first we need to verify the currently set attribute on all the files in this directory by using lsattr utility.

root@localhost:~# lsattr nifi-1.13.2 --------------e----- nifi-1.13.2/LICENSE --------------e----- nifi-1.13.2/flowfile_repository --------------e----- nifi-1.13.2/extensions -----------I--e----- nifi-1.13.2/content_repository --------------e----- nifi-1.13.2/logs --------------e----- nifi-1.13.2/state --------------e----- nifi-1.13.2/provenance_repository --------------e----- nifi-1.13.2/database_repository --------------e----- nifi-1.13.2/work --------------e----- nifi-1.13.2/bin --------------e----- nifi-1.13.2/docs --------------e----- nifi-1.13.2/NOTICE --------------e----- nifi-1.13.2/README -----------I--e----- nifi-1.13.2/lib --------------e----- nifi-1.13.2/conf --------------e----- nifi-1.13.2/run

To recursively add immutable flag on all of the above files, you need to use -R option with chattr command as shown below.

root@localhost:~# chattr -R +i nifi-1.13.2

Now if you again check the list of files, then it should look like below.

root@localhost:~# lsattr nifi-1.13.2 ----i---------e----- nifi-1.13.2/LICENSE ----i---------e----- nifi-1.13.2/flowfile_repository ----i---------e----- nifi-1.13.2/extensions ----i------I--e----- nifi-1.13.2/content_repository ----i---------e----- nifi-1.13.2/logs ----i---------e----- nifi-1.13.2/state ----i---------e----- nifi-1.13.2/provenance_repository ----i---------e----- nifi-1.13.2/database_repository ----i---------e----- nifi-1.13.2/work ----i---------e----- nifi-1.13.2/bin ----i---------e----- nifi-1.13.2/docs ----i---------e----- nifi-1.13.2/NOTICE ----i---------e----- nifi-1.13.2/README ----i------I--e----- nifi-1.13.2/lib ----i---------e----- nifi-1.13.2/conf ----i---------e----- nifi-1.13.2/run

Example 3: How to Remove an attribute from a File

You can also remove an attribute by using — operator with attribute flag. In this example, we are removing immutable flag from message.txt file using chattr -i message.txt command as shown below.

root@localhost:~# lsattr message.txt ----i---------e----- message.txt root@localhost:~# chattr -i message.txt root@localhost:~# lsattr message.txt --------------e----- message.txt

Example 4: How to set append attribute on a File

If you have a file which you only want to be appended and not modified then you can set append attribute +a on that file. In this example, we have a message.txt file which initially does have not have any attribute and then are setting append attribute using chattr +a message.txt command.

root@localhost:~# lsattr message.txt -------------------- message.txt root@localhost:~# chattr +a message.txt root@localhost:~# lsattr message.txt -----a-------------- message.txt

Now if you try to modify the contents of the message.txt file then it will show below «Can’t open file for writing» message.

root@localhost:~# vi message.txt "message.txt" "message.txt" E212: Can't open file for writing Press ENTER or type command to continue

Example 5: How to make a File undeletable

Let’s say you have some important file which cannot be deleted from System but since this file are accessed by multiple users so there is a chance it can be deleted accidentally by some user. So to avoid this you can set the undeletable attribute +u on this file. In this example, we have a sample file called message.txt. To make sure this file cannot be deleted, we are setting undeletable +u attribute using chattr +u message.txt command.

root@localhost:~# lsattr message.txt -----a-------------- message.txt root@localhost:~# chattr +u message.txt root@localhost:~# lsattr message.txt -u---a-------------- message.txt

Now if you try to delete this file using rm -rf message.txt command, then it will display «Operation not permitted» message as shown below.

root@localhost:~# rm -rf message.txt rm: cannot remove 'message.txt': Operation not permitted

Example 6: How to Check Man Page of chattr command

If you want to check the man page of chattr command then you need to use man chattr command as shown below.

root@localhost:~# man chattr CHATTR(1) General Commands Manual CHATTR(1) NAME chattr - change file attributes on a Linux file system SYNOPSIS chattr [ -RVf ] [ -v version ] [ -p project ] [ mode ] files. DESCRIPTION chattr changes the file attributes on a Linux file system. The format of a symbolic mode is +-=[aAcCdDeFijPsStTu]. The operator '+' causes the selected attributes to be added to the existing attributes of the files; '-' causes them to be removed; and '=' causes them to be the only attributes that the files have.

Источник

Оцените статью
Adblock
detector