rm command is one of the basic commands in Unix/Linux operating systems. It’s a fundamental tool for removing (deleting) files and directories.
Remove a file with rm
Simplest form of this command is rm . So if we have a file called try1:
we’ll remove like this:
and this ls command proves that the try1 file is really gone:
Remove multiple files with rm
Removing multiple files is done by either listing the files as separate command line parameters to rm:
or just using a filename mask:
Remove an empty directory with rm
Although you can use rmdir command for deleting directories, it’s possible (and possibly easier) to use rm -d command instead.
Let’s create a couple of directories with files for our next two experiments.
Now if we just try to remove the dir1 directory with rm, it won’t let us:
… that’s because we have to remove the files inside dir1 first:
…now let’s try removing the dir1 again, and it works just fine:
Use rm to remove a directory with all the files in it
We also have dir2 directory with files file2 and file3 in it from earlier, so let’s try removing it. This time though, we’ll use the rm with -r option to force rm command into deleting all the files in the dir2 recursively (and all the subdirectories if there are any):