How to Use the rm Command to Remove Files in Linux

Fumbani Banda Feb 02, 2024
  1. the rm Command in Linux
  2. Use the rm Command to Remove a File in Linux
  3. Use the rm Command to Remove Multiple Files in Linux
  4. Use the rm Command to Remove Files Interactively in Linux
  5. Use the rm Command to Remove Directory in Linux
  6. Use the rm Command to Remove All Files in Current Directory in Linux
  7. Use the rm Command to Remove Root Directory in Linux
How to Use the rm Command to Remove Files in Linux

This tutorial demonstrates using the rm command in Linux to remove files and directories.

the rm Command in Linux

The rm command is a Linux command used to remove objects. These objects can be files, directories, symbolic links, pipes and sockets.

The rm command does not move the deleted objects to Trash. It deletes the objects permanently, and you cannot recover the deleted objects unless you make a backup.

The rm command uses the following syntax.

rm [options] [file]

Use the rm Command to Remove a File in Linux

The image below demonstrates removing a file using the rm command. We are currently in the foo directory, and we use the ls command to list the contents of the current directory.

The foo directory has two files, file1.txt and file2.txt. To delete the file1.txt, we use the rm command and pass in the name of file1.txt as the argument.

To confirm that the file has been deleted, we list the contents of the foo directory using the ls command, and we only see the file2.txt; there is no file1.txt.

We have permanently deleted file1.txt.

Remove a File in Linux

Use the rm Command to Remove Multiple Files in Linux

Here, we demonstrate removing multiple files using the rm command. Use the ls command to list files inside the foo directory.

The foo directory has three files; file.txt, file1.txt and file2.txt. To remove file1.txt and file2.txt, we execute the rm command and pass the file names as arguments, as shown in the image below.

We execute the ls command to check the contents of the foo directory and see that file1.txt and file2.txt have been removed.

Remove Multiple Files in Linux

Use the rm Command to Remove Files Interactively in Linux

We use the rm command with the -i option to remove files interactively. It tells the rm command to ask before removing a file.

If there are multiple files, the rm command will ask for each file before removing it. Using the -I will ask once for every three files it deletes.

In the image below, we are in the foo directory. We use the ls command to list the files inside the current directory.

The foo folder has four files; file.txt, file1.txt, file2.txt and file3.txt. To remove file1.txt and file2.txt interactively, we execute the rm command using the -i option.

We have also included the -v option for verbose. The verbose option displays what the rm command does to the terminal.

The image below shows that the rm command prompts confirmation before removing the files; we use rm with the -i option. Once the file has been removed, a message is displayed that the file has been removed because of the verbose option.

Remove Files Interactively in Linux

Use the rm Command to Remove Directory in Linux

Use the rm command with the -r option to remove a directory that has subdirectories inside it. The -r option tells the rm command to recursively remove directories and their contents.

In the image below, we use the ls command with the -R and -l options to list the contents of the foo directory.

The -R option is used to recursively list the subdirectories and their contents, while the -l option tells the ls command to list the contents in a long list format. We see a directory named test inside the foo directory.

To remove the foo directory and all the subdirectories inside it, we use the rm command with the -r option to recursively remove directories and their contents. We also used the -v option in the image, displaying what that rm command does to the terminal.

Remove Directory in Linux

Use the rm Command to Remove All Files in Current Directory in Linux

Here, we demonstrate removing all the files and folders in the current directory. We are inside the foo directory, and we used the ls command with the -l and -R options to list the contents of the current directory.

The -l option tells the ls command to display the output to the standard output in a long list format, and the -R option tells the ls command to recursively list the contents of the subdirectories. The standard output shows the foo folder has a subdirectory named test.

To remove all the files in the current directory, we use the rm command with the wild card character, *. Below we have used the rm command with the -v and -r option and the wild card character, *.

The -v option tells the rm command to display what it is doing and hence the messages being displayed on the terminal once a file is removed. The -r option tells the rm command to recursively delete subdirectories and their files.

In this case, the rm command deletes the subdirectory test and the two files. The wild card character, *, means everything.

This context tells the rm command to remove everything in the current directory.

Remove All Files in Current Directory in Linux

Use the rm Command to Remove Root Directory in Linux

In Unix-like operating systems, the root directory is the directory that as all the directories and files on the operating system. A forward slash, /, represents the root directory.

Removing the root directory using the rm command means deleting all the directories and files kept on the system.

We run the command below to remove the root directory in Linux using the rm command.

rm -v -r --no-preserve-root /

The -v option is for verbose. The -v option tells the rm command to display the rm command. The -r option tells the rm command to recursively delete directories and their contents.

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Linux File