Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

Delete a file or directory. Delete a file or directory. The rm command is commonly used to delete one or more files or directories in a directory. It can also delete all files and subdirectories in a directory. For linked files, only the link is deleted, and the original files remain unchanged. Rm is a dangerous command and should be used with extreme care, especially for beginners, otherwise the entire system will be destroyed by this command (e.g. Rm * -rf under/(root)). Therefore, before executing rm, it is best to confirm which directory to delete and keep a high level of sanity.

1. Command format:

Rm [options] file…

2. Run the following command:

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.

3. Command parameters:

-f, -force Ignores non-existent files and never displays a prompt. -r, -r, -recursive instructs RM to recursively delete all directories and subdirectories listed in the parameter. -v, – verbose displays the detailed steps. – help displays the help information and exits. – version Displays the version information and exits

4. Command example:

Example 1: If you delete file file, the system will ask you whether to delete it. Command: rm File name Output:

[root@localhost test1]# root root 56 10-26 14:31 log.log root@localhost test1]# rm log.log rm: Do you want to delete the common file log.log? Y root@localhost test1 [root@localhost test1]#1234567Copy the code

Note: After you run the rm log.log command, the system will ask you whether to delete the file. After you enter y, the file will be deleted. Example 2: Delete file forcibly, and the system no longer prompts you. Command: rm -f log1.log Output: [root@localhost test1]# ll Total 4

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

Example 3: Delete any.log files; Run the rm -i *. Log command one by one to confirm the deletion.

[root@localhost test1]# ll 8-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: Delete the log1.log file. Y rm: Determine whether to delete the common log2.log file. Y [root@localhost test1]# ll total 0[root@localhost test1]#123456789Copy the code

Example 4: Delete the test1 subdirectory and all files in the subdirectory: rm -r test1

[root@localhost test]# ll total 24drwxr-xr-x 7 root root 4096 10-25 18:07 SCF 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: Check whether the test1 directory is displayed. Y rm: Determine whether to delete the common file test1/log3.log. Y rm: Determine whether to delete directory test1. Y [root@localhost test]# ll total 20drwxr-xr-x 7 root root 4096 10-25 18:07 SCF 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]#123456789101112131415161718Copy the code

Example 5: the rm -rf test2 command will delete all files in the test2 subdirectory and all files in the subdirectory without confirmation.

[root@localhost test]# rm -rf test2 [root@localhost test]# ll total 16drwxr-xr-x 7 root root 4096 10-25 18:07 SCF 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]#1234567Copy the code

Example 6: Delete files starting with -f. Run the rm — -f command.

[root@localhost test]# touch -- -f[root@localhost test]# ls -- -f -- f[root@localhost test]# rm -- -f rm: Do you want to delete the empty file -f? Y [root@localhost test]# ls -- -f ls: -f: no such file or directory [root@localhost test]#1234567Copy the code

You can also use the following steps:

/-f./-f./-f[root@localhost test]# rm./-f rm: Do you want to delete the empty file./-f? y [root@localhost test]#12345Copy the code

Myrm (){D=/ TMP /(date+D; Mv “@”D && echo” Moved to $D OK “; }

Output:

[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 1.log 2.log 3.log [root@localhost test]# ll -rw-r--r-- 1 root root 0 10-26 15:08 1.log -rw-r--r-- 1 root root 0 10-26 15:08 2.log -rw-r--r-- 1 root root 0 10-26 15:08 3.log drwxr-xr-x 7 root root 4096 10-25 18:07 scf 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 [123].log moved to / TMP /20121026150901 OK [root@localhost test]# ll 16drwxr-xr-x 7 root root 4096 10-25 18:07 SCF 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]# ls /tmp/20121026150901/ 1.log 2.log 3.log [root@localhost test]#12345678910111213141516171819202122Copy the code

Note: The above procedure mimics the effect of a recycle bin, that is, deleting a file only puts it in a temporary directory so that it can be recovered when needed.

Resources: blog.csdn.net/zwj10307112… codingstandards.iteye.com/blog/983531