Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Chattr Command in Linux (File Attributes) - DesignLinux

designlinux 0 Comments

In Linux, file attributes are meta-data properties that describe the file’s behavior. For example, an attribute can indicate whether a file is compressed or specify if the file can be deleted.

Some attributes like immutability can be set or cleared, while others like encryption are read-only and can only be viewed. The support for certain attributes depends on the filesystem being used.

This article explains how to use the chattr command to change file attributes on Linux file systems.

chattr Syntax #

The chattr command takes the following general form:

chattr [OPTIONS] [OPERATOR][ATTRIBUTES] FILE...

The value of the [OPERATOR] part can be one of the following symbols:

  • + – The plus operator tells the chattr to add specified attributes to the existing ones.
  • - – The minus operator tells the chattr to remove specified attributes from the existing ones.
  • = – The equal operator tells the chattr to set specified attributes as the only attributes.

The operator is followed by one or more [ATTRIBUTES] flags that you want to add or remove from the file attributes. Below is a list of a few common attributes and associated flags:

  • a – When this attribute is set, the file can only be opened in append mode for writing.
  • A – When a file with this attribute set is open, its atime record is not changed. atime (access time) is the last time the file was accessed/opened by some command or application.
  • e – This attribute denotes that the file is using extents for mapping the blocks on disk. The e attribute cannot be modified with chattr.
  • i – This attribute indicates that the file is immutable, which means that the file cannot be deleted or renamed.

For a complete list of all file attributes and flags, type man chattr in your terminal.

By default, file attributes are not preserved when copying a file with commands like cp or rsync .

chattr Example #

One of the common uses of chattr is to set the immutable flag to a file or directory to prevent users from deleting or renaming the file.

You can view the file attributes with the lsattr command:

lsattr todo.txt

The output below shows that only the e flag is set:

--------------e----- todo.txt

To make the file immutable, add the i flag with the + operator to the existing attributes:

sudo chattr +i todo.txt

We’re using sudo because only root can change the immutable flag.

Confirm that the attribute is added:

lsattr todo.txt
----i---------e----- todo.txt

To revert the changes and remove the immutable flag, use the - operator:

sudo chattr +i todo.txt

With chattr, you can add or remove multiple attributes at once. For example, to make the file immutable and tell the kernel not to track the last accessed time, you would use:

sudo chattr +iA todo.txt

The last operator that you can use is the = operator. For example, to set the e attribute as the only attribute, you would run:

sudo chattr "=e" todo.txt

Notice that the operator and flag are enclosed in quotes, to avoid shell interpretation of the + character.

Conclusion #

chattr is a command-line tool for changing the file attributes on a Linux file system.

If you have any questions or feedback, feel free to leave a comment.

chattr terminal

Related

Tags: chattr, terminal

How to Install PHP 8.0 on Ubuntu 20.04 / 18.04

Prev Post

How to Install PHP 8 on CentOS/RHEL 8/7

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 490
  • 574,980

DesignLinux.com © All rights reserved

Go to mobile version