rm

Used to delete given files and directories

Supplementary instructions

rm Command can delete one or more files or directories in a directory, or delete a directory and all its subordinate files and subdirectories. For linked files, only the entire linked file is deleted, while the original file remains unchanged.

Note: Use the rm command with extreme caution. Because once you delete a file, you can't recover it. Therefore, before deleting a file, it is best to take a look at the contents of the file to determine whether it really needs to be deleted. The -i option can be used with the rm command, which is particularly useful when deleting multiple files using file extension characters. Using this option, you will be asked to confirm one by one whether you want to delete them. At this time, you must enter y and press Enter to delete the file. If you just press enter or other characters, the file will not be deleted.

grammar

rm (options) (parameters)

Options

-d: Directly delete the hard link data of the directory to be deleted to 0, and delete the directory;
-f: forcefully delete files or directories;
-i: Ask the user before deleting existing files or directories;
-r or -R: Recursive processing, processing all files and subdirectories in the specified directory together;
--preserve-root: Do not perform recursive operations on the root directory;
-v: Display the detailed execution process of the command.

Parameters

File: Specify the list of files to be deleted. If the parameter contains a directory, the -r or -R option must be added.

Example

Interactively delete the files test and example in the current directory

rm -i test example
Remove test ?n (do not delete the file test)
Remove example ?y (delete file example)

Delete all files and subdirectories in the current directory except hidden files

#rm -r *

It should be noted that this is very dangerous!

Delete the package-lock.json file in the current directory

find . -name "package-lock.json" -exec rm -rf {} \;

*Find files ending with .html and delete

find ./docs -name "*.html" -exec rm -rf {} \;

*Delete files ending in .html under the current project

rm -rf *.html

Delete the node_modules directory in the current directory

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

Delete Files

#rm file1 file2...
rm testfile.txt

Delete Directory

rm -r [directory name] -r means to recursively delete all files and directories in the directory. -f means force deletion
rm -rf testdir
rm -r testdir

There is a confirmation prompt before the deletion operation

rm -i [file/directory]
rm -r -i testdir

Batch delete data folders in subfolders in the icons folder

rm -rf icons/**/data

rm ignore non-existent files or directories

The -f option (LCTT annotation: "force") forces this operation to be performed and ignores error messages
rm -f [file...]

Confirm deletion only in certain scenarios

Option -I ensures that only one confirmation will be prompted when deleting more than 3 files or recursively deleting (LCTT annotation: such as deleting a directory).
rm -I file1 file2 file3

Delete root directory

Of course, deleting the root directory (/) is the last operation Linux users want, which is why the default rm command does not support recursive deletion operations on the root directory. However, if you must do this, you need to use the --no-preserve-root option. When this option is provided, rm will not treat the root directory (/) specially.
I won’t give any examples. The operating system has been deleted by you. You are too bad😆

rm displays details of the current deletion operation

rm -v [file/directory]