rm command of common linux commands

rm is a common 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, or the whole system will be destroyed by this command (for example, execute rm * -rf under / (root directory)). Therefore, before executing rm, we'd better confirm the directory and what to delete, and keep a clear mind during the operation.

I Command format:

rm [option] file 

II Command function:

Delete one or more files or directories in a directory. If the - r parameter 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.

III Command parameter options

parameter

describe

-f

--force, ignore nonexistent files and never give a prompt.

-i

--interactive to delete interactively

-r (-R)

--recursive, instructs rm to recursively delete all directories and subdirectories listed in the parameter.

-v

--verbose, which shows the steps in detail

--help

Display this help message and exit

--version

Output version information and exit

IV Command instance

1. Delete file

Command:

rm file name

Output:

hc@hc-virtual-machine:~/test2/test5$ ls
file1  scf  test5-1
hc@hc-virtual-machine:~/test2/test5$ rm file1 
rm: Delete normal empty file 'file1'? y
hc@hc-virtual-machine:~/test2/test5$ ls
scf  test5-1

explain:

After entering rm file1 command, the system will ask whether to delete it. After entering y, the file will be deleted. If you don't want to delete it, the data n. If there is no inquiry, it is recommended to add. After all, the deletion operation needs to be cautious!

method:

vi ~/.bashrc

Then add it

alias rm='rm -i'

It means that the rm command actually uses the rm -i interactive mode, which needs to be confirmed. Note that there can be no space between rm and =, otherwise there will be a prompt that the rm command cannot be found,

Then execute this command at the terminal to make the modification take effect immediately:

source ~/.bashrc

2. Forcibly delete the file, and the system will not prompt for confirmation.

Command:

rm -f file name

Output:

hc@hc-virtual-machine:~/test2/test5$ ls
file2  scf  test5-1
hc@hc-virtual-machine:~/test2/test5$ rm -f file2 
hc@hc-virtual-machine:~/test2/test5$ ls
scf  test5-1

3. Delete all in the current directory The files at the end of log should be asked for confirmation one by one before deletion

Command:

rm -i *.log

Output:

hc@hc-virtual-machine:~/test2/test5/test5-1$ ls
log1.log  log2.log  log3.log
hc@hc-virtual-machine:~/test2/test5/test5-1$ rm -i *.log
rm: Delete normal empty file 'log1.log'? y
rm: Delete normal empty file 'log2.log'? y
rm: Delete normal empty file 'log3.log'? y
hc@hc-virtual-machine:~/test2/test5/test5-1$ ls
hc@hc-virtual-machine:~/test2/test5/test5-1$ 

explain:

touch is the command to create a file

mkdir is the command to create a directory

touch 1.log 2.log 3.log

If you create multiple consecutive files / directories at one time, {1.. 3}

For example: create 3 at a time log file

touch {1..3}.log

4. Delete all contents in the test5 directory and its subdirectories

Command:

rm -r test5

Output:

hc@hc-virtual-machine:~/test2$ ls
test22  test3  test4  test5
hc@hc-virtual-machine:~/test2$ tree test5
test5
├── log.log
├── scf
│   ├── bin
│   ├── doc
│   │   ├── info
│   │   └── product
│   ├── lib
│   ├── logs
│   │   ├── info
│   │   └── product
│   └── service
│       └── deploy
│           ├── info
│           └── product
└── test5-1
    └── log4.log

14 directories, 2 files
hc@hc-virtual-machine:~/test2$ ls
test22  test3  test4  test5
hc@hc-virtual-machine:~/test2$ rm -r test5
rm: Enter directory'test5'? y
rm: Enter directory'test5/scf'? y
rm: Enter directory'test5/scf/logs'? y
rm: Delete directory 'test5/scf/logs/info'? y
rm: Delete directory 'test5/scf/logs/product'? y
rm: Delete directory 'test5/scf/logs'? y
rm: Enter directory'test5/scf/service'? y
rm: Enter directory'test5/scf/service/deploy'? y
rm: Delete directory 'test5/scf/service/deploy/info'? y
rm: Delete directory 'test5/scf/service/deploy/product'? y
rm: Delete directory 'test5/scf/service/deploy'? y
rm: Delete directory 'test5/scf/service'? y
rm: Delete directory 'test5/scf/bin'? y
rm: Enter directory'test5/scf/doc'? y
rm: Delete directory 'test5/scf/doc/info'? y
rm: Delete directory 'test5/scf/doc/product'? y
rm: Delete directory 'test5/scf/doc'? y
rm: Delete directory 'test5/scf/lib'? y
rm: Delete directory 'test5/scf'? y
rm: Enter directory'test5/test5-1'? y
rm: Delete normal empty file 'test5/test5-1/log4.log'? y
rm: Delete directory 'test5/test5-1'? y
rm: Delete normal empty file 'test5/log.log'? y
rm: Delete directory 'test5'? y
hc@hc-virtual-machine:~/test2$ ls
test22  test3  test4
hc@hc-virtual-machine:~/test2$ 

5. Delete all contents in the test5 directory and its subdirectories without asking for confirmation

Command:

rm -rf test5

6. Create and delete files starting with -

Command:

Create files named - a and - b in the current directory

Method 1: touch ./-a
 Method 2: touch -- -b

Delete the files named - a and - b in the current directory

Method 1: rm -- -a
 Method 2: rm ./-b

Output:

hc@hc-virtual-machine:~/test2/test4$ ls
hc@hc-virtual-machine:~/test2/test4$ touch -a
touch: Missing file operand
Try 'touch --help' for more information.
hc@hc-virtual-machine:~/test2/test4$ touch ./-a
hc@hc-virtual-machine:~/test2/test4$ ls
-a
hc@hc-virtual-machine:~/test2/test4$ touch -- -b
hc@hc-virtual-machine:~/test2/test4$ ls
-a  -b
hc@hc-virtual-machine:~/test2/test4$ rm -a
rm: Not applicable options -- a
Try 'rm ./-a' to remove the file '-a'.
Try 'rm --help' for more information.
hc@hc-virtual-machine:~/test2/test4$ rm -- -a
rm: Delete normal empty file '-a'? y
hc@hc-virtual-machine:~/test2/test4$ ls
-b
hc@hc-virtual-machine:~/test2/test4$ rm ./-b
rm: Delete normal empty file './-b'? y
hc@hc-virtual-machine:~/test2/test4$ ls
hc@hc-virtual-machine:~/test2/test4$ 

explain:

It is better not to start the file name with "-" because "-" is followed by options. Therefore, simply using the command of "rm -a" system will misjudge. Therefore, we can only use the method of avoiding the first byte is "-"! It's just to add this catalogue! If man rm checks the usage method, there is another method, which is "rm -- -f" (another method).

7. Custom recycle bin function

Command:

myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }

explain:

myrm()
{ 
D=/tmp/$(date +%Y%m%d%H%M%S); 
#Create a file named "current date" in the / tmp folder;
#Where "date+%Y%m%d%H%M%S" specifies the output format of the date;
mkdir -p $D; 
#Create a folder with the path in variable D.
mv "$@" $D && echo "moved to $D ok"; 
#Move the file to be deleted into the folder in variable D. after the file is successfully moved, the output will be moved successfully.
}

alias rm='myrm'
#Command alias definition method. After this definition is successful, the system will do the same regardless of entering rm or myrm.

Output:

hc@hc-virtual-machine:/tmp$ myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D;  mv "$@" $D && echo "moved to $D ok"; }

hc@hc-virtual-machine:/tmp$ alias rm='myrm'

hc@hc-virtual-machine:/tmp$ touch {1..4}.log

hc@hc-virtual-machine:/tmp$ ls
1.log  2.log  3.log 4.log
hc@hc-virtual-machine:/tmp$ rm [1234].log
moved to /tmp/20181026111028 ok
hc@hc-virtual-machine:/tmp$ ls
20181026111028
hc@hc-virtual-machine:/tmp$  cd 20181026111028/
hc@hc-virtual-machine:/tmp/20181026111028$ ls
1.log  2.log  3.log  4.log

explain:

When a file is deleted, it can be put in a temporary recycle bin to simulate the effect of file recovery.

Since we have currently bound rm as myrm, we cannot perform the delete operation

Temporarily set the rm command alias to myrm

alias rm='myrm'

At this time, if you want to delete the files in the recycle bin, because they are temporary, the binding will be invalid after changing a command line window, that is, change a command line window and execute the rm -r command to delete

If you don't want to change the command-line window, you can delete the files in the recycle bin with the sudo rm -rf directory name

Or unbind temporarily

unalias rm

Output:

Added by johncrook on Wed, 02 Mar 2022 08:49:35 +0200