Removing a Single File:
You can remove a single file with rm.
Let’s say, you want to remove an image file 529395.jpg in the Pictures/ directory in your users HOME directory.
To remove the file, you may run the following command:
The file should be removed.
Print What rm is Doing:
If you want rm to print what it’s doing on the console, then you can use the -v option.
For example, to remove a file test.txt from your current working directory, you may run the following command:
As you can see, the file is removed. rm also printed removed ‘test.txt’ on the console as it successfully removed the file test.txt.
Prompt Before Removing Files:
As rm is a very destructive command, you may accidentally remove important files. The good news is that, rm has an -i flag which prompts (to confirm) you before removing every file.
For example, let’s say, you want to remove the file hello.txt but you want rm to prompt you to confirm the file removal operation. Then, you would run rm as follows:
As you can see, you’re prompted to confirm the remove operation of the file hello.txt. To confirm, press y and then press <Enter>.
The file should be removed.
Prompt Before Removing More Than 3 Files:
You can tell rm to prompt for confirmation when you’re removing more than 3 files only with the -I option.
Let’s say, you have 4 files in your current working directory, test1, test2, test3, and test4. Now, you want to make sure you don’t remove more than 3 files without any notice.
To do that, run the following command:
Now, rm will prompt you to confirm whether you really want to delete these files. If you are sure that you don’t want these files anymore, then press y and then press <Enter> to continue.
As you can see, all these files are removed.
Removing Empty Directories with rm:
You can also remove empty directories on your Linux system with rm. To do that, you can use the -d option of rm.
Let’s say, you have an empty directory files/ in your current working directory. Now, you want to remove it. You can do so with the following command:
As you can see, the empty directory files/ is removed.
Removing Files and Directories Recursively with rm:
Let’s say, you have an entire directory of files and other subdirectories with files that you don’t need anymore. Now, you want to remove all of them to save up disk space.
You can remove every files and subdirectories of the directory (let’s say tmp/ in your current working directory) with the following command:
As you can see, all the files and subdirectories inside the files/ directory including the files/ directory is removed.
If you want rm to ask you to confirm before deleting every directories and files in the files/ directory, then you can run the above command as follows:
As you can see, rm is asking you to confirm before it does anything. If you’re okay with this, just press y and then press <Enter> to continue.
rm will keep asking you to confirm till it’s done with it’s work.
If you want to stop the rm command in the midway, just press <Ctrl> + c.
The rm command is very destructive. You should always be careful with it. Always be extra careful and double check everything before you run the rm command with super user privileges (sudo).
For example, you can delete important system files if you’re not careful with the rm command. Which may cause your computer to fail to boot or certain services to fail to start.
A good way to double check everything is to use the ls command.
For example, let’s say, you want to remove every files that start with tmp from your current working directory. You can of course remove all these files with the following command:
But for safety purpose, instead of using rm command first, check whether using tmp* lists all the files that you want to remove with the ls command as follows.
As you can see, the command lists all the files that match tmp*. If you’re sure that you don’t need them, then you can use the rm command as shown above to remove them all.
So, that’s all about the rm command. Thanks for reading this article.