Using a Linux operating system distribution is not just about simplifying and solving technical problems in a flawless manner but also unraveling the technical mysteries behind them. One of the technical mysteries this article seeks to bring to light is the prominent use of the Linux rm command.
As per its man page, the rm command is a member of the GNU Coreutils package and comes pre-installed in all Linux operating system distributions.
It serves a primary goal of getting rid of files and directories no longer required in the Linux file system. This command is manually executed by a Sudoer/root user of the Linux OS system, especially when dealing with files stored under the root file system.
The rm command syntax is straightforward as highlighted below:
$ rm [OPTION]... [FILE]...
Different File(s) Deletion Process in Linux
If you delete a file through the Linux Desktop GUI, this file is not completely lost as it is temporarily sent to Trash where a Linux user can decide to restore it later or permanently remove it from the Linux system.
When dealing with the rm command, there is no Trash icon to temporarily hold the file for later restoration or permanent deletion if needed.
Problem Statement
Let us assume we have an audio file called rock.aac on our computer running a Linux operating system. To view the file properties of this audio file we will run the command:
$ ls -l rock.aac
The deletion of this file using the rm command can be implemented in the following manner:
$ rm rock.aac
After the above rm command executes, we can try to view the file properties again.
$ ls -l rock.aac
The following screen capture summarizes these three command sequences:
As you have noted, the execution of the last command yields the outcome that cannot access ‘rock.aac’: No such file or directory.
This article is here to help us understand what happens between the execution of the command (rm rock.aac) and the succeeding command (ls -l rock.aac).
Understanding the Linux Filesystem
The good thing about using Linux as your primary operating system is that its OS footprints are transparent. There is no encapsulation. To understand the implication of executing the rm command discussed above, we need to highlight an important fact about the Linux filesystem.
The Linux filesystem is the housekeeper of data such as files stored on the computer. Some key attributes associated with the filesystem are access control and named files.
These filesystem attributes help describe stored data or files and without them, the Linux OS will just view stored data as a soup of bits. We would not have access to any metadata or even know the location of any stored data.
File Creation to Deletion
When we create or store a file in Linux, a link to that file is created. When we execute the rm command, the command does not necessarily get rid of the file but deletes the file link (link associated with the file).
This command removes the location reference to our file(s) and the Linux OS then marks the file’s associate storage block as free. The file’s inode (index node) and data block are labeled as unused. This step is irreversible as the file is completely unlinked from the system.
Before using the rm command, ensure you have a backup copy of your files as the command execution process is irreversible.
You might also like to read the following articles:
The article stopped just as it was getting to the interesting part.
There is no “the Linux Filesystem”; there are many, with different behavior. Another factor is whether your storage device has firmware that does remapping (standard on SSDs, also on enterprise HDDs).
So, things are simple for the ext4 filesystem on consumer HDD. Not so simple for say Btrfs on an SSD. CoW, TRIM, over-provisioning, etc.