Deleting Files Using rm Command in Linux
rm
This tutorial demonstrates the use of command to delete files and directories
in Linux .
rm
command is a Linux command used to delete objects. These objects can be files, directories, symbolic links, pipes, and sockets.
rm
The command does not move the deleted objects to Trash
. It permanently deletes the objects and there is no way to recover deleted objects unless you have a backup.
rm
The command uses the following syntax.
rm [options] [file]
The following figure demonstrates the use of rm
the command to delete a file. We are currently in foo
the directory and we use ls
the command to list the contents of the current directory.
foo
The directory has two files, file1.txt
and file2.txt
. To remove file1.txt
, we use rm
the command and pass file1.txt
in the name of as an argument.
To confirm that the files were deleted, we use ls
the command to list foo
the contents of the directory and we see only file2.txt
; nothing file1.txt
.
We have permanently deleted it file1.txt
.
Here, we have demonstrated the use of rm
command to delete multiple files. Use ls
command to list foo
the files in directory.
foo
The directory contains three files; file.txt
, , file1.txt
and file2.txt
. To delete file1.txt
and file2.txt
, we execute rm
the command and pass the file name as an argument, as shown in the following figure.
We executed ls
the command to check foo
the contents of the directory and found that file1.txt
and file2.txt
had been deleted.
We use the -i
-p command with -d option rm
to delete files interactively. It tells rm
the -p command to ask before deleting files.
If there are multiple files, rm
the command will ask about each file before deleting it. Using -I
will ask once for every three files it deletes.
In the image below, we are in foo
the directory. We use ls
the command to list the files in the current directory.
foo
The folder has four files; file.txt
, file1.txt
, file2.txt
and file3.txt
. To delete file1.txt
and interactively file2.txt
, we -i
execute rm
the command with the -p option.
We have also included the verbose -v
option. The verbose option shows you rm
what the command does to your terminal.
The following image shows rm
that the command prompts for confirmation before deleting a file; we used it with the -delete -i
option rm
. After deleting the file, a message is displayed stating that the file has been deleted due to the verbose option.
Use the -r
-d recursive command with the -p option rm
to remove a directory that has subdirectories within it. -r
The -d recursive option tells rm
the -d recursive command to recursively remove the directory and its contents.
In the following image, we use the command with the -R
and -l
options ls
to list foo
the contents of the directory.
-R
The -d option is used to recursively list subdirectories and their contents, while -l
the -e option tells ls
the -d command to list the contents in a long listing format. We foo
see a test
directory called -d in the -d directory.
To delete foo
the directory and all the subdirectories within it, we use the -d option with -r
the -p option rm
to recursively delete a directory and its contents. We have also used -v
the -d option in the image to show rm
what the -d command does from the terminal.
Here, we have demonstrated deleting all the files and folders in the current directory. We are in foo
the directory and we use ls
the command with the -l
and -R
options to list the contents of the current directory.
-l
The -d option tells ls
the command to display the output to standard output in a long listing format and -R
the -d option tells ls
the -d command to recursively list the contents of subdirectories. The standard output shows that foo
the folder has a test
subdirectory called .
To delete all the files in the current directory, we use the -r command with the wildcard character -r *
. rm
Below we have used the -r command with -v
the -r and -r -r
options along with wildcard characters .*
rm
-v
The -d option tells rm
the command to show what it is doing, so once a file is deleted, messages will be displayed on the terminal. -r
The -d option tells rm
the command to recursively delete subdirectories and their files.
In this case, rm
the command deletes the subdirectory test
and the two files. The wildcard *
means everything.
This context tells rm
the command to delete everything in the current directory.
In Unix-like operating systems, the root directory is the directory of all directories and files on the operating system. A forward slash /
indicates the root directory.
Removing the root directory using rm
the command means removing all directories and files remaining on the system.
We run the command below rm
to remove the root directory in Linux using command.
rm -v -r --no-preserve-root /
-v
Options are used to be detailed. -v
Options tell rm
the command to display rm
commands. -r
Options tell rm
the command to recursively remove directories and their contents.
This article is reproduced from: DelftStack
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u