rm
The remove utility, called rm
, can be used for deleting one or more files or directories.
For instance, we can delete all of the csv
files from an oldproject
folder as follows:
rm oldproject/*.csv
If we try to delete a folder and the contents inside the folder, we need the -r
flag. For instance, we could delete all of the previous semester’s work as follows:
rm -r lastsemesterfolder
It is often helpful, when using the -r
option, to also use the -f
option, but it is powerful. It will automatically force the removal of all files and directories inside the specified location:
rm -rf lastsemesterfolder
There are no undo remove actions
Simply put: If you delete a file, you cannot easily get it back.
On the Anvil computer, it is possible to use snapshots to retrieve some files, but it is not a guarantee. Also, many Unix computers will not have snapshots. So please remove files very, very carefully. The next section has two warnings that you should carefully avoid.
Be Careful
DO NOT DO THIS: A VERY CRUEL JOKE (which you SHOULD NOT DO) is that sometimes a person will tell someone to run rm -rf $HOME
but PLEASE DO NOT DO THIS because it will delete everything in one’s home directory and everywhere inside all directories in the home directory, i.e., it will work recursively to force the deletion of everything in one’s home directory. DO NOT DO THIS.
Here is another warning. Suppose that you are working in a directory and you intended to type to remove all of the pdf files in a directory
rm *.pdf
BUT YOU ACCIDENTALLY TYPE THIS (PLEASE DO NOT DO THIS):
rm * .pdf
In this second case, you will delete two things. You will delete and you will delete
.pdf
. BUT the first one of these, , means all files in the directory. So you will ACCIDENTALLY DELETE everything in the directory. Please do not do this. Type carefully when you are removing files.
Safe Methods
Before Dr Ward types things like this:
rm *.pdf
he will first type this with ls
instead of rm
, like this:
ls *.pdf
so that he can see that he is only listing the .pdf
files, and then he will hit the "up arrow" on the keyboard, to reload the command ls *.pdf
and he will change the ls
to rm
so that he is only removing the very same files that he listed earlier:
rm *.pdf
This helps promote the safe usage of the rm
command.