Prevent Files And Folders From Accidental Deletion Or Modification In Linux

Prevent Files And Folders From Accidental Deletion Or Modification In Linux
Prevent Files And Folders From Accidental Deletion Or Modification In Linux

Fuente: https://www.ostechnix.com/prevent-files-folders-accidental-deletion-modification-linux/

Some times, I accidentally “SHIFT+DELETE” my data. Yes, I am an idiot who don’t double check what I am exactly going to delete. And, I am too dumb or lazy to backup the data. Result? Data loss! They are gone in a fraction of second. I do it every now and then. If you’re anything like me, I’ve got a good news. There is a simple, yet useful commandline utility called “chattr” (abbreviation of Change Attribute) which can be used to prevent files and folders from accidental deletion or modification in Unix-like distributions. It applies/removes certain attributes to a file or folder in your Linux system. So nobody can delete or modify the files and folders either accidentally or intentionally, even as root user. Sounds useful, isn’t it? Indeed!
In this brief tutorial, we are going to see how to use chattr in real time in-order to prevent files and folders from accidental deletion in Linux.

Prevent Files And Folders From Accidental Deletion Or Modification In Linux

By default, Chattr is available in most modern Linux operating systems. Let us see some examples.
The default syntax of chattr command is:
chattr [operator] [switch] [filename]
chattr has the following operators.
  • The operator ‘+’ causes the selected attributes to be added to the existing attributes of the files;
  • The operator ‘-‘ causes them to be removed;
  • The operator ‘=’ causes them to be the only attributes that the files have.
Chattr has different attributes namely – aAcCdDeijsStTu. Each letter applies a particular attributes to a file.
  • a – append only,
  • A – no atime updates,
  • c – compressed,
  • C – no copy on write,
  • d – no dump,
  • D – synchronous directory updates,
  • e – extent format,
  • i – immutable,
  • j – data journalling,
  • P – project hierarchy,
  • s – secure deletion,
  • S – synchronous updates,
  • t – no tail-merging,
  • T – top of directory hierarchy,
  • u – undeletable.
In this tutorial, we are going to discuss the usage of two attributes, namely a, i which are used to prevent the deletion of files and folders. That’s what our topic today, isn’t? Indeed!

Prevent files from accidental deletion

Let me create a file called file.txt in my current directory.
$ touch file.txt
Now, I am going to apply “i” attribute which makes the file immutable. It means you can’t delete, modify the file, even if you’re the file owner and the root user.
$ sudo chattr +i file.txt
You can check the file attributes using command:
$ lsattr file.txt
Sample output:
----i---------e---- file.txt
Now, try to remove the file either as a normal user or with sudo privileges.
$ rm file.txt
Sample output:
rm: cannot remove 'file.txt': Operation not permitted
Let me try with sudo command:
$ sudo rm file.txt
Sample output:
rm: cannot remove 'file.txt': Operation not permitted
Let us try to append some contents in the text file.
$ echo 'Hello World!' >> file.txt
Sample output:
bash: file.txt: Operation not permitted
Try with sudo privilege:
$ sudo echo 'Hello World!' >> file.txt
Sample output:
bash: file.txt: Operation not permitted
As you noticed in the above outputs, We can’t delete or modify the file even as root user or the file owner.
To revoke attributes, just use “-i” switch as shown below.
$ sudo chattr -i file.txt
Now, the immutable attribute has been removed. You can now delete or modify the file.
$ rm file.txt
Similarly, you can restrict the directories from accidental deletion or modification as described in the next section.

Prevent folders from accidental deletion and modification

Create a directory called dir1 and a file called file.txt inside this directory.
$ mkdir dir1 && touch dir1/file.txt
Now, make this directory and its contents (file.txt) immutable using command:
$ sudo chattr -R +i dir1
Where,
  • -R – will make the dir1 and its contents immutable recursively.
  • +i – makes the directory immutable.
Now, try to delete the directory either as normal user or using sudo user.
$ rm -fr dir1
$ sudo rm -fr dir1
You will get the following output:
rm: cannot remove 'dir1/file.txt': Operation not permitted
Try to append some contents in the file using “echo” command. Did you make it? Of course, you couldn’t!
To revoke the attributes back, run:
$ sudo chattr -R -i dir1
Now, you can delete or modify the contents of this directory as usual.

Prevent files and folders from accidental deletion, but allow append operation

We know now how to prevent files and folders from accidental deletion and modification. Next, we are going to prevent files and folders from deletion, but allow the file for writing in append mode only. That means you can’t edit, modify the existing data in the file, rename the file, and delete the file. You can only open the file for writing in append mode.
To set append mode attribution to a file/directory, we do the following.
For files:
$ sudo chattr +a file.txt
For directories: 
$ sudo chattr -R +a dir1
A file/folder with the ‘a’ attribute set can only be open in append mode for writing.
Add some contents to the file(s) to check whether it works or not.
$ echo 'Hello World!' >> file.txt
$ echo 'Hello World!' >> dir1/file.txt
Check the file contents using cat command:
$ cat file.txt
$ cat dir1/file.txt
Sample output:
Hello World!
You will see that you can now be able to append the contents. It means we can modify the files and folders.
Let us try to delete the file or folder now.
$ rm file.txt
Output:
rm: cannot remove 'file.txt': Operation not permitted
Let us try to delete the folder:
$ rm -fr dir1/
Sample output:
rm: cannot remove 'dir1/file.txt': Operation not permitted
To remove the attributes, run the following commands:
For files:
$ sudo chattr -R -a file.txt
For directories: 
$ sudo chattr -R -a dir1/
Now, you can delete or modify the files and folders as usual.
For more details, refer the man pages.
man chattr

Wrapping up

Data protection is one of the main job of a System administrator. There are numerous free and commercial data protection software are available on the market. Luckily, we’ve got this built-in tool that helps us to protect the data from accidental deletion or modification. Chattr can be used as additional tool to protect the important system files and data in your Linux system.
And, that’s all for today. Hope this helps. I will be soon here with another useful article. Until then, stay tuned with OSTechNix!
Cheers!

LATINCHAIN PLATFORM



news.latin-chain.com/
LatinChain Platform: Pi Hackathon winner 2021.
Welcome to LatinChain Ecosystem portal on Pi Network cryptocurrency ecosystem. Get utility usage bonus by using Mainnet and Testnet.


Understanding the LatinChain Ecosystem

Your gateway to decentralized applications on the Pi Network.


Welcome to the LatinChain Ecosystem portal. As a decentralized application (dApp) built on the Pi Network, our goal is to provide real utility and seamless interaction for the Pi community. Whether you are a developer, a pioneer, or a casual user, understanding the difference between our Mainnet and Testnet environments is crucial for maximizing your experience.



Staking Pi to Boost Rank

Did you know you can directly support the growth of LatinChain Mainnet?

By Staking Pi on our app within the Pi Browser, you help boost our ranking in the ecosystem directory. A higher rank means more visibility, bringing more Pioneers into our community and increasing the value of the network for everyone.

Support us today and help LatinChain reach the top!



Why Choose the Mainnet?

The LatinChain Mainnet (latinchain.pinet.com) is the live production environment where real value is exchanged. By accessing the Mainnet, you are interacting with the fully operational version of our blockchain application.

  • Real Utility: Every action contributes to the genuine activity of the network.
  • Verified Status: Users who choose to support the ecosystem via donations can unlock "Verified" status. This signals your commitment to the community.
  • Double Utility Bonus: Active usage on the Mainnet contributes to your Pi utility usage bonus.


The Role of the Testnet

The Testnet serves as our sandbox environment. It is designed for users who want to explore new features, test transaction flows, or understand the mechanics of the LatinChain token without using real Pi. It is the perfect training ground for new pioneers to get comfortable with Web3 interactions before moving to the Mainnet.



How the LatinChain Token Works

LatinChain is designed to foster a digital economy within the Pi Browser. By participating in either network, you are helping to stress-test and improve a growing financial ecosystem.



Select an option above to begin your journey.

The Evolution of Cryptocurrency: From Bitcoin's Genesis to Pi Network's Ecosystem (2008-nowadays)

The world of finance was forever changed in the aftermath of the 2008 global financial crisis. What started as an obscure proposal on a cryptography mailing list has transformed into a global technological revolution. Here is the journey of cryptocurrency from its inception to the present day.



The Genesis: Bitcoin and the Decentralized Dream (2008-2010)

In October 2008, an anonymous entity named Satoshi Nakamoto published a whitepaper titled "Bitcoin: A Peer-to-Peer Electronic Cash System." It introduced a revolutionary concept: a decentralized digital currency that required no central bank or administrator.

  • The Genesis Block: On January 3, 2009, the first block of the Bitcoin network was mined, setting the foundation for a new financial paradigm.
  • Proof of Work: This brilliant consensus mechanism solved the double-spending problem, ensuring complete trust in a trustless digital environment.



Programmable Money: Ethereum and the DeFi Explosion (2015-2021)

While Bitcoin proved the concept of digital scarcity, developers soon realized blockchain technology could do much more. In 2015, Ethereum launched, introducing "smart contracts"—self-executing code that lives directly on the blockchain.

  • Decentralized Finance (DeFi): Ethereum enabled the creation of lending platforms, decentralized exchanges, and automated market makers.
  • Mainstream Awareness: By 2021, the crypto market reached unprecedented heights, driven by global retail interest, digital art (NFTs), and the exciting promise of Web3.



Maturation and Institutional Adoption (2022-2024)

Following the euphoric highs of previous years, the industry entered a vital period of maturation. The global focus shifted from pure speculation to building robust, scalable infrastructure.

  • Layer 2 Solutions: New networks built on top of base blockchains made transactions significantly faster and cheaper.
  • Wall Street Entry: Major traditional financial institutions embraced Bitcoin ETFs, permanently cementing cryptocurrency as a legitimate and highly sought-after asset class.



The Accessibility Revolution: Pi Network (2025-nowadays)

Despite massive industry growth, a major barrier remained: traditional crypto mining was expensive and technically complex. Enter Pi Network, a visionary project designed to put cryptocurrency directly into the hands of everyday people. By nowadays, Pi Network has beautifully solidified its vision of creating a truly inclusive digital economy.

  • Mobile-First Innovation: Pi revolutionized accessibility by allowing users to mine directly from their smartphones without draining battery life or data.
  • Global Community: It successfully built one of the largest, most engaged, and widely distributed communities in the entire Web3 space.
  • Everyday Utility: With a robust ecosystem of decentralized applications (dApps) and an innovative native KYC solution, Pi Network empowers millions of everyday users to transact, build, and interact seamlessly.



Takeaway: The journey from 2008 to nowadays highlights a monumental shift from niche cryptography to universal accessibility, paving the way for a financial future that truly includes everyone.

Comments

Popular posts from this blog

Pi Network Apps published on Pi Network Ecosystem

Proyecto para Red Social gigante (Giant Social Network project)

Sígueme en mis Redes Sociales (Follow me in my Social Networks). OpenSource Expert 🇻🇪