The rm command in linux USES elaboration

  • 2020-05-13 04:02:28
  • OfStack

You learned about the mkdir command to create files and directories, and today you'll learn about the rm command to delete files and directories from linux. rm is a common command that deletes one or more files or directories in a directory. It can also delete a directory and all the files and subdirectories under it. For linked files, only the link is removed and the original file remains unchanged.

rm is a dangerous command, so be careful when using it, especially for beginners, or the whole system will be destroyed by this command (e.g., execute rm * -rf under/(root)). So, before we execute rm, we'd better confirm which directory 1 is in and what we want to delete, keeping a high level of clarity.

1. Command format:
rm [options] file...

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

3. Command parameters:
-f, --force ignores nonexistent files and never gives a hint.
-i, --interactive for interactive deletion
-r, -R, --recursive instructs rm to recursively delete all directories and subdirectories listed in the parameter.
-v, --verbose shows the steps in detail
--help displays this help and exits
--version outputs version information and exits

4. Command example:

Example 1: delete file file, the system will first ask whether to delete.
Command:
rm filename
Output:


[root@localhost test1]# ll
A total of 4
-rw-r--r-- 1 root root 56 10-26 14:31 log.log
root@localhost test1]# rm log.log
rm : whether to delete 1 A file " log.log " ? y
root@localhost test1]# ll
A total of 0[root@localhost test1]#

Description:
After entering the rm log.log command, the system will ask whether to delete. After entering y, the file will be deleted, and if not, the data n will be deleted.

Example 2: forced deletion of file, no longer prompted.
Command:
rm -f log1.log
Output:


[root@localhost test1]# ll
A total of 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
A total of 0[root@localhost test1]#

Example 3: delete any.log file; Ask for confirmation 1 by 1 before deleting
Command:
rm -i *.log
Output:


[root@localhost test1]# ll
A total of 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 : whether to delete 1 A file " log1.log " ? y
rm : whether to delete 1 A file " log2.log " ? y
[root@localhost test1]# ll
A total of 0[root@localhost test1]#

Example 4: delete the test1 subdirectory and all files in the subdirectory
Command:
rm -r test1
Output:


[root@localhost test]# ll
A total of 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 : whether to enter the directory " test1 " ? y
rm : whether to delete 1 A file " test1/log3.log " ? y
rm : whether to delete directory " test1 " ? y
[root@localhost test]# ll
A total of 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]#

Example 5: the rm-rf test2 command deletes the test2 subdirectory and all files in the subdirectory without 11 validation
Command:
rm -rf test2
Output:


[root@localhost test]# rm -rf test2
[root@localhost test]# ll
A total of 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]#

Example 6: delete files that start with -f
Command:
rm -- -f
Output:


[root@localhost test]# touch -- -f
[root@localhost test]# ls -- -f
-f[root@localhost test]# rm -- -f
rm : whether to delete 1 Like the empty file " -f " ? y
[root@localhost test]# ls -- -f
ls: -f: There is no file or directory
[root@localhost test]#
You can also use the following steps :
[root@localhost test]# touch ./-f
[root@localhost test]# ls ./-f
./-f[root@localhost test]# rm ./-f
rm : whether to delete 1 Like the empty file " ./-f " ? y
[root@localhost test]#

Example 7: custom recycle bin functionality
Command:
myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $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
A total of 16
-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
A total of 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]#

Description:
The above procedure mimics the effect of a recycle bin, where you delete files by placing them in a temporary directory so that they can be recovered if needed.


Related articles: