Removing or deleting files and directories is a common task for anybody who deals with computing devices. When it comes to Linux, we can delete files and directories using the the rm command on the terminal.
While the rm command is simple, it’s important to properly understand its careful usage to avoid unintended data loss. In this guide will walk you through everything you need to know about effectively and safely using the rm command:
Basic Syntax of the rm command
The rm command has a straightforward and simple syntax, which could be exploited by any Linux newbie. Just take a look below:
rm filename
The above syntax means that you type the rm command followed by the name of the file you want to delete. Once you hit enter, the file will be deleted instantly without prompting you confirm whether to delete or not.
Deleting a single file using the rm command
Using the rm command, you can delete a single file by specifying its name. See the example below:
rm oldfile.txt
Removing multiple Files using the rm command
Similarly, you can also remove multiple files at once by listing all their names after the rm command. For example, the following command will delete files: file1.txt, file2.txt, file3.txt:
rm file1.txt file2.txt file3.txt
Using Wildcards with the rm command
The rm command is compatible with wildcards in Linux and can allow you to delete multiple files matching a pattern. For example:
Removing files based on extension pattern using the rm command and Wildcards
Supposing you want to remove or delete all files with .txt extension, you could append the asterisk wildcard. This is represented as follows:
rm *.txt
The above command means that any file with .txt extension will be deleted. For example, files like report.txt, list.txt, cars.txt, students.txt, income.txt, etc could be deleted if there are present in the current working directory..
The same applies to any file extension pattern like .png, .jpeg, .pdf, .MP3, .MP4 and more.
Removing files with certain a common naming pattern using the rm command and Wildcards
Supposing you have files that start with the word “data” in their filenames, you could define this pattern to delete all of them. Here is the command:
rm data*
This command means that files like: data.txt, database.sql, data_backup.zip, datafile, data-2023-09-13.csv, data_log_01.log, data.bak, data_config.json and more could be deleted.
Similarly, you could define any name patterns in the filenames targeting specific files for deletion. For example if you want to delete files that start with capital F, here is the command:
rm F*
Removing files with spaces in their names using the rm command
There are two ways to remove files with spaces in their names:
Using quotes and the rm command
Supposing you have a file named My School Report and you want to delete it, you could enclose the filename into quotes to be able to delete it using the rm command: Here is how it’s done:
rm "My School Report"
Enclosing the filename with spaces into quotes will tell the system that it’s a single name, which would otherwise be interpreted as each word representing an independent file.
Using the Backslash character with the rm command
If you don’t want to use the double quotes while deleting filenames with spaces, you can escape each space in the filename using the Backslash character. This is done by typing each word in the filename followed by the backslash character then space followed by the next word and the Backslash character again. You do this until all spaces in the filename are escaped. Here is an example of how it’s done on Linux terminal:
rm My\ School\ Report
The above command is deleting a file with the name: My School Report
Dealing with Case-sensitivity using the rm command
For beginners, filenames are Case-sensitive in Linux. For example, file.txt is not the same as File.txt. While in english these are the same words, in Linux, there are two different files. This is because file.txt starts with the lower case f and File.txt starts with the uppercase F.
So, when using the rm command, you need to be specific which file are you targeting. The Case-sensitive matters as ignoring it could mislead you into deleting a wrong file.
If you want to delete a file with lowercase name, make sure it’s what you’re targeting by typing it the way it appears in the current working directory. Let’s say you want to delete file.txt, the command you would run would be as follows:
rm file.txt
Running the above command is not the same as deleting File.txt as the two are completely different files. If you wanted to delete File.txt, you would run the following command:
rm File.txt
Confirmation before each deletion using the rm command
By default, the rm command can just delete the specified file without needing your confirmation. However, using the -i (interactive) option or flag will prompt you to confirm if you Yes or No to delete the specified files or directories.
rm -i file.txt
Removing Directories using the rm command
Using the directories we introduce a flag or option to flag. It will depend on the nature of the directory you want to delete. For example:
Removing an empty directory using the rm command
If you want to delete an empty directory, you will need to add the -d option or flag right after the rm command and then followed by the directory name. For example, the following command will delete an empty directory named emptydir
rm -d emptydir
Removing non-empty directories using the rm command
To remove non-empty directories, we need to use the -r (recursive) option. For example, the following command will delete a non-empty directory called fulldir:
rm -r fulldir
Safety Precaution while using the rm command to delete files
If you’re deleting highly-sensitive files, you need to trigger a Yes or No confirmation prompt by using the -i option. Fo example, if you’re deleting multiple files at once, the -i option will prompt you to confirm if you’re sure about the deletion action you’re taking:
rm -i *.txt
Here is the most dangerous options combination you should fully avoid if you’re not sure of what you’re doing:
rm -rf /`**
You should avoid the above command as it could delete your entire filesystem. This command attempts to recursively delete all files and directories starting from the root directory, which would effectively destroy the entire file system.
Running such a command, even accidentally, could lead to irreversible data loss and render the system inoperable. It’s crucial to be very careful when using powerful commands that modify the file system, especially those involving wildcards or operating at the root level.