Skip to main content

File Properties Commands

lsattr Command Examples

The lsattr command lists the attributes of a file or directory in Linux.

The lsattr command in Linux is used to display attributes of a file or a directory.

When you execute the lsattr command over a file or a directory, it shows you the file attributes of a specified file/directory providing additional information about how a file is treated by the operating system.

So in this tutorial, I will walk you through what is the meaning of every attribute and examples of the lsattr command.

How to use the lsattr command

To use the lsattr command to its maximum potential, you're required to learn the syntax first. So I'll start off by sharing the syntax:

lsattr [OPTIONS] <File/Directory>

Here,

  • [OPTIONS]: here's where you specify one or more options to tweak the output of the lsattr command.
  • <File/Directory>: here's where you specify one or more files/directories to show attributes.

Now, let's take a look at a list of options and their use:

Option Description
-a List all files, including hidden files.
-d List directories like regular files, showing their attributes instead of their contents.
-l Display a long listing format, including additional information like user, group, permissions, and extended attributes (if available).
-p Display the project number associated with the file (if applicable).
-R Recursively list attributes of files and directories within the specified paths and their subdirectories.
-v Display the version information of the lsattr command.

In case you are wondering what happens when you use the lsattr command without any flags, then here's your answer:

Use the lsattr command without any options

I'm sure you want to know the meaning of the e in the above output. Well, that's one of many attributes that you may encounter while using the lsattr command.

Here's a list of every attribute you'll ever come across while using the lsattr command:

Abbreviation Name Description
- No attribute No attribute is set.
b Backup The file is included in routine backup operations like dump.
c Compressed The file is compressed using a filesystem-specific compression algorithm.
d No Dump The file is excluded from filesystem backup operations like dump.
h Hidden The file is hidden from most listing utilities by default.
i Immutable The file cannot be modified, deleted, or renamed.
s Synchronous Updates Data is physically written to the disk before the write operation is considered complete, ensuring data integrity in case of system crashes.
S System File The file is marked as a system file, indicating its importance for system operation.
T Top Directory (AIX) (AIX specific) This attribute designates the top directory in a filesystem hierarchy.
u Undeletable The file cannot be deleted using standard deletion commands, though it can still be overwritten with new data.
A No Access Time Updates The file's access time (the last time the file was read) is not updated, improving performance in scenarios where access time information isn't crucial.
X No Execute Permission The file cannot be executed as a program, even if it has executable permissions.
a Append-only New data can be written to the end of the file, but existing content cannot be modified.
e Extent Format The file data is stored using extents, which are large contiguous blocks on the disk, improving performance for large files.
j Data Journalling The filesystem maintains a journal to track changes made to the file, allowing for faster recovery in case of errors.
t Top Directory This attribute is similar to T but might have slightly different meanings depending on the filesystem.
P Project Hierarchy (Specific to some filesystems) This attribute is used for project-based quota management.
N Inline Data Small files are stored directly within the inode instead of separate data blocks, improving performance for very small files.
I Indexed Directory The directory uses an indexed structure for faster directory listing and searching.
E Encrypted The file contents are encrypted using a filesystem-specific encryption algorithm.

Now, let's take a look at practical examples of the lsattr command.

Practical examples of the lsattr command

In this section, I will walk you through some practical examples of the lsattr command so you can have a better idea of its application.

1. Get attributes of a file

To find file attributes of a single file, all you have to do is append the filename to the lsattr command as shown here:

lsattr <Filename>
Find file attributes using the lsattr command

Similarly, if you want to find the file attributes of multiple files, then you can append multiple files to the lsattr command separated by a space:

lsattr File1 File2 File3 FileN
Find file attributes of multiple files using the lsattr command

2. List attributes recursively

The term recursive means it will look for files within the target directory and every sub-directory possible. And you may want to do the same while using the lsattr command.

To use the lsattr command recursively, use the -R flag as shown here:

lsattr -R <Dir-name>
List file attributes recursively

3. Show attributes of the hidden files as well

When you list attributes of a directory, it won't show you the hidden files. To include hidden files in the list, you'd have to use the -a flag:

lsattr -a Directory
Show file attributes of the hidden files using the lsattr command

4. Treat the directory as a normal file

By default, when you mention the name of the directory, it will list the file attributes of the files inside the specified directory.

But what if you want to know the attribute of the directory itself?

You use the -d flag as shown here:

lsattr -d Directory_name
Treat directory as a normal file while using the lsattr command

5. Show attributes in a long list format

By default, the lsattr command will only mention the abbreviation of the file attribute but you can expand the output by including the name of every attribute using the -l flag:

lsattr -l File/Directory
Show file attributes in long list format by using the lsattr command

Want to change file attributes? Use chattr

While the lsattr command can only show you the file attributes, what if you want to change the file attributes themselves?

To change the file attributes, you can use the chattr command, and here's a detailed guide explaining how to use the chattr command on Linux:

Use chattr Command in Linux
With chattr command, you can make a file β€˜undeletable’ even by root. Here are some common usage of the chattr command in Linux.

I hope you will find this guide helpful.

Sagar Sharma