Linux rm command: Delete a file or directory.
Linux rm command Function Description
The rm command can delete one or more files or directories in a directory, or it can delete a directory and all files under it and its subdirectories. For linked files, only the entire linked file is deleted, while the original file remains unchanged.
Be careful with the rm command. This is because once a file is deleted, there is no way to recover it. Therefore, before deleting a file, it is a good idea to look at the contents of the file again to determine if you really want to delete it. rm command can be used with the -i option, which is especially useful when deleting multiple files using the file extension character. With this option, the system will ask you to determine if you want to delete them one by one. At this point, you must enter y and press Enter to delete the file. If you only press Enter or another character, the file will not be deleted.
Linux rm command Syntax
rm [option] [file|directory]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-f |
Forced deletion. Ignore non-existent files, no message is given |
-r |
Recursive deletion of directories and their contents |
-i |
Confirm the information before deleting it |
Linux rm command Examples
Delete the file4 file from the current directory
[root@rhel ~]# rm file4
rm: Does it delete the ordinary empty file "file4"? y //Enter y to confirm deletion of the file
Delete the directory /root/apidemos
rm -rf /root/apidemos
Output:
Interactively delete the files test and example from the current directory
rm -i test example
Remove test ?n(Do not delete the file test)
Remove example ?y(Delete the file example)
Delete all files and subdirectories in the current directory except for the implied files
# rm -r *
It should be noted that it is very dangerous to do so!
Delete the package-lock.json file in the current directory
find . -name "package-lock.json" -exec rm -rf {} \;
Find the file ending with *.html and delete it
find ./docs -name "*.html" -exec rm -rf {} \;
Delete files ending with *.html under the current project
rm -rf *.html
Remove the node_modules directory from the current directory
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
delete directory
rm -r [directory name] -r means to recursively delete all files and directories in the directory. -f indicates forced deletion
rm -rf testdir
rm -r testdir
Confirmation prompt before delete operation
rm -i [file/directory]
rm -r -i testdir
Batch delete the data folder in the subfolder of the icons
folder
rm -rf icons/**/data
rm Ignore non-existent files or directories
The -f option (force) makes this operation forceful, ignoring error prompts
rm -f [files...]
Confirm deletion in certain scenarios only
The -I option ensures that only one confirmation is prompted when deleting more than 3 files or when deleting recursively (e.g. deleting a directory).
rm -I file1 file2 file3
rm Show details of the current delete operation
rm -v [file/directory]