How to Remove Files and Directories Using Linux Terminal?

Mostly Linux-based operating systems assist Command Line Interface (terminal) to perform various tasks such as installing, removing, and upgrading packages. This utility is quite useful for managing files and directories, especially for removal purposes. Considering its importance, this guide will demonstrate various methods to remove files and directories using the Linux terminal.

The content that is carried out in the tutorial is as follows:

How to Remove Files and Directories Using Linux Terminal?

In Ubuntu (Linux-based OS), the terminal makes life easier to remove single or multiple files by executing a single command. The syntax is provided below to remove a file through the terminal:

Syntax for rm Command:

To delete files:

$ rm filename

To delete directories using rm command:

$ rm -r directory

Syntax for rmdir Command:

In the above syntaxes, the filename, and the directory represents the name of the file/directory or their path.

The general syntaxes of the most used variants of “rm” and “rmdir” commands are described below:

Command’s SyntaxesDescription
filenameIt refers to removing multiple files by requiring their names.  
rm -f filename filename1The command forcefully removes multiple files by matching their names
rm -r directoryIt represents deleting a single directory along with all content.
rm -r directory directory1For removing multiple directories with content.
rm -d directoryIt removes the empty directory in the operating system.
rmdir directoryIt has the same functionality to remove the empty directory from the Linux system.  

Let’s practice these commands.

Example 1: How to Remove Single File Using Linux Terminal?

An example is carried out to remove a single file from the terminal of the Linux operating system. The step-by-step instructions are provided here for removing a file:

Display Current Files and Directories

To visualize current files and directories, the “ls” command is used here:

$ ls

It displays multiple directories and files after executing the command in the terminal.

Note: The “pwd” command is utilized to extract the location of the present directory.

Remove a Specific File

In Ubuntu, the “rm” command is utilized to remove a specific file. In our case, an existing file name is written after the “rm” command: 

$ rm resetter_3.0.0-stable_all.deb

After executing the command, you can verify that “resetter_3.0.0-stable_all.deb” has been removed.

Example 2: How to Remove a Single Directory Using Linux Terminal?

The “rm” command is written with the directory name the user wants to remove. The practical usage of the “rm” command is presented below:

Display Files and Directories

To visualize files and directories, the “ls” command can be utilized as seen below:

$ ls

After executing the command, users can display several files and directories in the Home directory.

Remove a Specific Directory

Target a specific directory, “Images”, for removal purposes. For this, execute the “rm” command with the “-r” command utility to remove the directory and all the content present in it:

$ rm -r Images

Finally, users can verify that the “Images” directory has been successfully removed from the Linux operating system.

Example 3: How to Remove an Empty Directory?

The “rmdir” command removes the empty directories in Linux. In the terminal, the command deletes the directory given below:

$ sudo rmdir -v Dir2

The output verifies that the particular directory “Dir2” has been deleted successfully.

Example 4: How to Remove Multiple Directories With All Content?

Let’s explore another example of removing multiple directories and their content. It is quite useful to remove unnecessary data in a short time.

Display Multiple Directories

The “ls” command displays all directories and files kept in the Home directory. We target two directories at the same time, which is highlighted below:

$ ls

Remove Multiple Directories

The “rm” command is used with the “-r” utility and specifies the name of directories that users want to remove from the system:

$ rm -r 'Directory 1' 'Directory 2'

Users can confirm that ‘Directory 1‘ and ‘Directory 2′ have been successfully removed using the Linux terminal.

Example 5: How to Remove Directory Content Using Path?

This example is followed to remove the specific content stored in the directory via the “rm” command. To do so, the script is given below:

$ rm Dir1/itslinuxfoss.txt

Now, you can verify through the “ls” command that the particular file has been removed from the “Dir1” directory.

That’s it from this article.

Conclusion

 In the Linux terminal, the “rm” or “rmdir” commands are utilized to remove files and directories specifying their exact names. The “rmdir” is mostly utilized to remove empty directories to boost system performance. Additionally, users can utilize multiple variants of “rm” commands, such as “-d” for the removal of the empty directory and “-r” to forcefully delete all content in the directory. This guide has explained multiple examples of removing files and directories from the Linux terminal.