Home  >  Article  >  Operation and Maintenance  >  What are the ways to use the rm command in Linux?

What are the ways to use the rm command in Linux?

王林
王林forward
2023-05-12 11:13:304576browse

rm is a commonly used command. The function of this command is to delete one or more files or directories in a directory. It can also delete a directory and all files and subdirectories under it. For linked files, only the link is deleted, and the original files remain unchanged.

rm is a dangerous command. Be careful when using it, especially for novices, otherwise the entire system will be destroyed by this command (for example, execute rm * -rf in / (root directory)). Therefore, before we execute rm, it is best to confirm which directory we are in and what we want to delete, and keep a clear mind during the operation.

1. Command format:

rm [option] file...

2. Command function:

Delete one or more files or directories in a directory. If the -r option is not used, rm will not delete the directory. If you use rm to delete a file, you can usually still restore the file to its original state.

3. Command parameters:

-f, --force Ignore files that do not exist and never give a prompt.
-i, --interactive Perform interactive deletion
-r, -r, --recursive Instruct rm to recursively delete all directories and subdirectories listed in the parameter.
-v, --verbose Display the steps in detail
--help Display this help information and exit
--version Output the version information and exit

4. Command example:

Example 1: To delete the file file, the system will first ask whether to delete it.

Command:

rm file name

Output:

Copy code The code is as follows:

[root@localhost test1 ]# ll

Total 4

Copy code The code is as follows:

-rw-r--r-- 1 root root 56 10-26 14:31 log.log
root@localhost test1]# rm log.log

rm: Do you want to delete the general file "log.log"? y

Copy code The code is as follows:

root@localhost test1]# ll

Total 0[root@localhost test1]

#Instructions:

Enter the rm log.log command Afterwards, the system will ask whether to delete. If you enter y, the file will be deleted. If you do not want to delete, the data will be n.

Example 2: Forcibly delete the file, the system will no longer prompt.

Command:

Copy code The code is as follows:

rm -f log1.log

Output:

Copy code The code is as follows:

[root@localhost test1]# ll

Total 4

Copy code The code is as follows:

-rw -r--r-- 1 root root 23 10-26 14:40 log1.log
[root@localhost test1]# rm -f log1.log
[root@localhost test1]# ll

Total 0[root@localhost test1]

#Example 3: Delete any .log files; ask for confirmation one by one before deleting

Command:

rm -i *.log

Output:

Copy code The code is as follows:

[root@localhost test1]# ll

Total 8

Copy code The code is as follows:

-rw-r--r-- 1 root root 11 10-26 14:45 log1.log
-rw- r--r-- 1 root root 24 10-26 14:45 log2.log
[root@localhost test1]# rm -i *.log

rm: Whether to delete general files "log1.log"? y

rm: Delete the general file "log2.log"? y

[root@localhost test1]# ll

Total 0[root @localhost test1]

#Example 4: Delete the test1 subdirectory and all files in the subdirectory

Command:

Copy code The code is as follows:

rm -r test1

Output:

Copy code The code is as follows:

[root@localhost test]# ll

Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Copy code The code is as follows:

drwxr-xr-x 2 root root 4096 10- 26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm -r test1

rm: Do you want to enter the directory "test1"? y

rm: Do you want to delete the general file "test1/log3.log"? y

rm: Do you want to delete the directory "test1"? y

Copy code The code is as follows:

[root@localhost test]# ll

Total 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Copy code The code is as follows:

drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr -xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]

#Example 5: The rm -rf test2 command will delete the test2 subdirectory and all files in the subdirectory without confirming one by one.

Command:

Copy Code The code is as follows:

rm -rf test2

Output:

Copy code The code is as follows:

[root@localhost test]# rm -rf test2
[root@localhost test]# ll

Total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Copy Code The code is as follows:

drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]

#Example 6: Delete files starting with -f

Command:

rm -- -f

Output:

Copy code The code is as follows:

[root@localhost test]# touch -- -f
[root@localhost test]# ls -- -f
-f[root@localhost test]# rm -- -f

rm: Do you want to delete the general empty file "-f"? y

Copy code The code is as follows:

[root@localhost test]# ls -- -f

ls: -f: There is no such file or directory

Copy code The code is as follows:

[ root@localhost test]

#You can also use the following steps:

Copy the code The code is as follows:

[root@localhost test]# touch ./ -f
[root@localhost test]# ls ./-f
./-f[root@localhost test]# rm ./-f

rm: Whether to delete general empty files" ./-f”? y

Copy code The code is as follows:

[root@localhost test]

#Example 7: Customized Recycle Bin Function

Command:

Copy code The code is as follows:

myrm(){ d=/tmp/$(date %y%m%d%h% m%s); mkdir -p $d; mv "$@" $d && echo "moved to $d ok"; }

Output:

Copy code The code is as follows :

[root@localhost test]# myrm(){ d=/tmp/$(date %y%m%d%h%m%s); mkdir -p $d; mv "$@ " $d && echo "moved to $d ok"; }
[root@localhost test]# alias rm='myrm'
[root@localhost test]# touch .log .log .log
[root@localhost test]# ll
Total
-rw-r--r-- root root - : .log
-rw-r--r-- root root - : .log
-rw-r--r-- root root - : .log
drwxr-xr-x root root - : scf
drwxrwxrwx root root - : test
drwxr-xr-x root root - : test
drwxr-xr-x root root - : test
[root@localhost test]# rm [].log
moved to /tmp/ ok
[root@localhost test]# ll
Total drwxr-xr-x root root - : scf
drwxrwxrwx root root - : test
drwxr-xr-x root root - : test
drwxr-xr-x root root - : test
[root@localhost test]# ls /tmp//
.log .log .log
[root@localhost test]

#Explanation:

Above The operation process simulates the effect of the recycle bin, that is, when deleting a file, it just puts the file in a temporary directory, so that it can be restored when needed.

Let me give you a detailed introduction to the name: rm command

Usage permissions: any user

Usage method: rm [options] name...

Description: Delete files and directories.

Parameters: 

-i Ask for confirmation one by one before deleting.
-f Even if the original file attribute is set to read-only, it will be deleted directly without confirming one by one.
-r Delete the files in the directory and below one by one.

Example:

Delete any c language program files; ask for confirmation one by one before deleting:

rm - i *.c

Delete the finished subdirectory and any files in the subdirectory:

rm -r finished

Function description: Delete Document or directory.

Syntax: rm [-dfirv][--help][--version][Document or directory...]

Supplementary instructions: Execute the rm command to Delete a document or directory. If you want to delete a directory, you must add the parameter "-r", otherwise only the document will be deleted by default.

Parameters:

-d or --directory Directly delete the hard link data of the directory to be deleted to 0 to delete the directory.

-f or --force Forcefully delete the document or directory.

-i or --interactive Ask the user before deleting existing documents or directories.

-r or -r or --recursive Recursive processing, any documents and subdirectories in the specified directory will be processed together.

-v or --verbose Display the instruction execution process.

--help Online help.

--version Display version information

The above is the detailed content of What are the ways to use the rm command in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete