rm
Description
rm is a standard Unix utility that removes files and directories.
It can be modified with flags to further improve its functionality.
The rm command syntax is:
rm [flags] [file / directory]
Flags
Some of the popular flags that are used with rm command are as follows:
-d: deletes empty directories.-f: deletes an item even if it is locked or protected without prompting for confirmation.-i: prompts before every removal.-r: removes directories and their contents recursively.-v: lists what is being deleted.--help: get more information about the command and uncommon flags.
-d
Description:
By default, rm command primarily deletes just files and not folders. We use another command rmdir for removing empty directories. This same functionality can also be done with the rm command and the -d flag. In summary, -d flag is used to delete empty directories.
Usage:
Consider we have an empty folder test. If we want to delete the folder, we need to use the -d flag as:
rm -d test
Output:
No Output, use ls command to verify.
-f
Description:
With the -f flag, the rm command will forcefully delete a file without prompting for confirmation even when it is protected.
Usage:
Let us assume that we are in a folder test which contains a file file1.txt which is write-protected.
Without the -f flag:
rm file1.txt
Output:
rm: remove write-protected regular empty file 'file1.txt'?
With the -f flag:
rm -f file1.txt
Output:
No Output, use ls command to verify.
-i
Description:
With the -i flag, the rm command will prompt the user before deleting any file.
Usage: Taking the example from above we can enter the following into the terminal
rm -i file1.txt
Output:
rm: remove regular empty file 'file1.txt'?
If you press Y then the file will be deleted and if you press N then the file will not be deleted.
-r
Description:
With the -r option, the rm command will recursively delete everything inside a directory. This effectively extends the functionality of rm command from just files to both files and directories.
Usage:
Taking the example where we have files .file.txt, file1.txt and file2.txt in a folder called test. The flag will be more clear if we pair the -r flag with the -i flag as:
rm -ri test
Output:
rm: descend into directory 'test'? Y
rm: remove regular empty file 'test/file2.txt'? N
rm: remove regular empty file 'test/.file.txt'? N
rm: remove regular empty file 'test/file1.txt'? N
rm: remove directory 'test'? N
This shows exactly how the -r flag traverses into the directory. Without the -i flag, there will be no output and the files are simply deleted.
Note: -d flag and -r are different in the sense that -d is only used for removing empty directories whereas -r removes the directory along with the items inside it.
If we used the -d flag in the above example, we'd simply get an error as the test folder is not empty.
-v
Description:
With the -v option, the rm command will show details for the deleted file.
Usage:
Taking the example where we have files .file.txt, file1.txt and file2.txt in a folder called test. The flag will be more clear if we pair the -r flag with the -v flag as:
rm -rv test
Output:
removed 'test/file2.txt'
removed 'test/.file.txt'
removed 'test/file1.txt'
removed directory 'test'
Without the -v flag, there will be no output and the files are simply deleted.
--help
Description:
With the --help option, the rm command will show other flags and options for the command that are not commonly used.
Usage:
rm --help
Examples
To remove
filefrom a folderSample:rm Sample/fileTo remove a protected
filefrom a folderSample:rm -f Sample/fileTo list the files being deleted from a folder
Sample:rm -rv Sample
Additional Information
Caution:
This command should be used carefully as the items deleted with rm are permanently deleted and do not remain in trash folder.
Wildcards:
Wildcards such as * or ? can be used with the rm command. For example: Let us consider that we have a Pictures folder that contains 50 pictures of the type personal1.png, personal2.png and so on. It also contains 50 more pictures of the type work1.png, work2.png and so on. If you're tasked with deleting the work pictures only then it can be tedious to remove it one by one. In that case we use the ? wildcard as shown below:
rm work?.png
Note: * matches one or more occurrences of any character, including no character and ? matches a single occurrence of any character.
Exercises
Create a folder
testcontaining filesnormalFile.txt,.hiddenFile.txt. Remove the write permission ofnormalFile.txtand try deleting it with thermcommand and observe the result. Hint: You may need to usemkdir,touchandchmodcommands as well.Use the above exercise and find a suitable flag and delete
normalFile.txt.Use the
verboseoption to delete the.hiddenFile.txt.Delete the now empty
testfolder with thermcommand and suitable flag.