search
HomeOperation and MaintenanceLinux Operation and MaintenanceWhat are the ways to use the rm command in Linux?

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

May 12, 2023 am 11:13 AM
linuxrm command

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:亿速云. If there is any infringement, please contact admin@php.cn delete
The Building Blocks of Linux: Key Components ExplainedThe Building Blocks of Linux: Key Components ExplainedApr 30, 2025 am 12:26 AM

The core components of the Linux system include the kernel, file system, and user space. 1. The kernel manages hardware resources and provides basic services. 2. The file system is responsible for data storage and organization. 3. Run user programs and services in the user space.

Using Maintenance Mode: Troubleshooting and Repairing LinuxUsing Maintenance Mode: Troubleshooting and Repairing LinuxApr 29, 2025 am 12:28 AM

Maintenance mode is a special operating level entered in Linux systems through single-user mode or rescue mode, and is used for system maintenance and repair. 1. Enter maintenance mode and use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can check and repair the file system and use the command "fsck/dev/sda1". 3. Advanced usage includes resetting the root user password, mounting the file system in read and write mode and editing the password file.

Linux Maintenance Mode: Understanding the PurposeLinux Maintenance Mode: Understanding the PurposeApr 28, 2025 am 12:01 AM

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux Operations: Networking and Network ConfigurationLinux Operations: Networking and Network ConfigurationApr 27, 2025 am 12:09 AM

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.

Maintenance Mode in Linux: A System Administrator's GuideMaintenance Mode in Linux: A System Administrator's GuideApr 26, 2025 am 12:20 AM

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

Maintenance Mode in Linux: When and Why to Use ItMaintenance Mode in Linux: When and Why to Use ItApr 25, 2025 am 12:15 AM

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

Linux: Essential Commands and OperationsLinux: Essential Commands and OperationsApr 24, 2025 am 12:20 AM

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

Linux Operations: Managing Files, Directories, and PermissionsLinux Operations: Managing Files, Directories, and PermissionsApr 23, 2025 am 12:19 AM

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools