attr Command in Linux



In Linux, "attr" is a legacy command line utility that manages the extended attributes on filesystem objects. The extended file attributes are used to associate the metadata (additional information) with the files. These attributes inform the operating system about actions such as updating the access time, synchronizing the file data back to the disk, and managing other logistical aspects.

In this tutorial, we’ll show you how to use the attr command to add, retrieve, update, and remove file attributes.

Table of Contents

How to Install the attr Command in Linux?

The "attr" command doesn’t come pre-installed in Linux; however, it can be easily installed using a package manager according to your specific distribution. For instance, to install the attr command on Debian-based systems, you can use the apt command as follows −

sudo apt install attr 
attr Command Linux 1

To install attr command on Arch Linux, use the following syntax −

sudo pacman -S attr

Similarly, you can install "attr" on Red Hat-based Linux distributions by executing one of the following commands −

sudo yum install attr
sudo dnf install attr

You can verify the "attr" package’s installation on your system by simply executing the below-given command −

attr --version

How to use the attr Command in Linux?

To use the attr command in Linux, you must follow the below-mentioned syntax −

attr flag attrName fileName

Syntax Explanation

  • The flag can be one of the following: [-s] [-g] [-r] [-l] [-V value].
  • Where "-s" is used to set an attribute’s value, -g is used to get the value of a specific attribute, r is used to remove a specific attribute, and -V is used with -s to set a new value.
  • The attrName represents the name of an extended attribute while the fileName represents the file’s name to which the attribute applies.

Examples of attr Command in Linux

Let’s discuss a few examples of the attr command to see how to add, remove, update, or list extended attributes of a file in Linux.

How to Add an Attribute to a File in Linux?

In Linux, the "attr" or "setfattr" commands are used to add an attribute to an XFS filesystem file. Both these commands are part of the attr package. You can use the following syntax to add an attribute to a file using the attr command −

attr -s author -V "Dean Jones" exampleFile.txt

This command will add an "author" attribute to the "exampleFile.txt" file, as shown in the following output −

attr Command Linux 2

Similarly, you can execute the "setfattr" command to add/set an extended attribute to a file, as shown below −

setfattr --name user.author --value "Dean Jones" exampleFile.txt

Here, it is important to note that the setfattr command requires that you specify the target namespace −

attr Command Linux 3

How to List/Check Extended File Attributes in Linux?

To check the extended file attributes in Linux, you can execute the attr command with the -g option −

attr -g author exampleFile.txt

Alternatively, you can use the getfattr command of the attr package to get the extended file attributes −

getfattr --name user.author examples.txt

The output of both commands is shown below −

attr Command Linux 4

How to Get/List all Extended Attributes in Linux?

You can get all extended attributes of a file by executing the "attr" command with the "-l" option or "getfattr" command with the "-d" option, as shown below −

attr -l exampleFile.txt
getfattr -d examples.txt
attr Command Linux 5

How to Update an Extended File Attribute in Linux?

You can update/modify any extended file attribute using "attr" or "setfattr" commands, just like creating a new attribute −

attr -s author -V "Dean Jones" exampleFile.txt
setfattr --name user.author --value "Dean Jones" exampleFile.txt

These commands will update the existing attributes with the new extended file attributes.

How to Remove an Extended File Attribute in Linux?

You can use the "-r" option with the "attr" command to remove any extended file attribute. For this purpose, you use the following syntax −

attr -r  author examples.txt

On successful completion of the command, the cursor will be moved to the next line −

attr Command Linux 6

You can verify the attribute’s removal by listing the extended file attributes for the selected file as follows −

attr -g author examples.txt
attr Command Linux 7

However, if the attribute to be removed doesn’t exist, you’ll encounter the following output −

attr Command Linux 8

How Can You Preserve Your Extended Attributes in Linux?

One common problem with extended file attributes is their specificity to a filesystem. This means you can lose these attributes if you copy a file from one drive/partition to another, even if the new location supports extended attributes. Therefore, to keep extended attributes intact, use a tool like rsync that supports preserving them. For this purpose, use the following syntax −

rsync --archive --xattrs ~/fileName.txt /tmp/

Note − Transferring a file to a filesystem that doesn't support extended attributes will result in those attributes being lost (regardless of the tool you use).

Conclusion

The "attr" command is a very handy command line utility that belongs to the attr package. This command is not installed on your system by default. However, you can install it quickly and use it to manage the extended attributes of a file. The attr command supports different options/flags that can be used to handle the extended file attributes in Linux.

You can use flags like "-l", "-g attribute_name", "-s attribute_name", "-r attribute_name", or "-V value" to retrieve the names and values of all extended attributes, get the value of the specified attribute, set the value of the given attribute, remove a specific attribute, or set a new value of an attribute, respectively.

Advertisements