A Linux command a day (5): rm command

post thumb
Devops
by Admin/ on 17 Sep 2021

A Linux command a day (5): rm command


We’ve learned the command mkdir to create files and directories, and today we’ll learn the command rm to delete files and directories in linux.

rm is a commonly used command to delete one or more files or directories in a directory, it can also delete a directory and all the files and subdirectories under it. In the case of linked files, only the links are removed and the original files are left intact.

rm is a dangerous command and should be used with care, especially for novices, otherwise the whole system will be destroyed by this command (e.g. by executing rm * -rf in / (root directory)). Therefore, before executing rm, we’d better check which directory we’re in and what exactly we want to delete, so that we can keep a high level of clarity during the operation.

1. Command format

rm [option] file...

2. Command function.

Deletes one or more files or directories in a directory. rm does not delete directories 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 Ignore non-existent files and never prompt for them.  
  -i, --interactive for interactive deletion  
  -r, -R, --recursive instructs rm to recursively delete all the directories and subdirectories listed in the arguments.  
  --v, --verbose show the steps in detail  
     --help Show this help and exit  
     --version output version information and exit`

4. Examples of commands.

Example 1: Delete the file file, the system will first ask if it is deleted.

Command.

rm filename

Output.

[root@localhost test1]# ll  
Total 4

-rw-r--r-- 1 root root 56 10-26 14:31 log.log

[root@localhost test1]# rm log.log   
rm: whether to delete the general file "log.log"? y

[root@localhost test1]# ll  
Total 0


Description.

After entering the command rm log.log, the system will ask whether to delete, and the file will be deleted after entering y. If you don’t want to delete, data n.

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

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

Total 0

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

Command.

rm -i *.log

Output.

[root@localhost test1]# ll  
Total 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 the general file "log1.log"? y  
rm: whether to delete the general file "log2.log"? y

[root@localhost test1]# ll  
Total 0

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

Command.

rm -r test1

Output.

[root@localhost test]# ll

Total 24

drwxr-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: does it go to the directory "test1"? y  
rm: whether to delete the general file "test1/log3.log"? y  
rm: whether to delete the directory "test1"? y

[root@localhost test]# ll

Total 20

drwxr-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 command rm -rf test2 will delete the test2 subdirectory and all the files in the subdirectory, and you don’t have to check them all

Command.

rm -rf test2

Output.

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

Total 16

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]#

Example 6: Deleting files starting with -f

Command.

rm -- -f

Output.

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

rm: whether to delete the general empty file "-f"? y

[root@localhost test]# ls -- -f  
ls: -f: no that file or directory

[root@localhost test]#

You can also use the following procedure.

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

rm: whether to delete the general empty file ". /-f"? y

[root@localhost test]#

Example 7: Customize the recycle bin function

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

Total 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

Total 16  
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]# ls /tmp/20121026150901/

1.log 2.log 3.log

[root@localhost test]#

Description.

The above procedure simulates the effect of the recycle bin, i.e. when deleting files it just puts them into a temporary directory so that they can still be restored when needed.

Reference:

www.cnblogs.com/peida/archive/2012/10/25/2738271.html

comments powered by Disqus