How to Remove Files and Directories Using Linux Command Line?

Posted in

How to Remove Files and Directories Using Linux Command Line?

Maaz Bin Asad
Last updated on May 22, 2024

    Some of the basic operations in the Linux filesystem include creating, modifying, renaming, and deleting the files and directories. In this article, we will discuss how to remove files and directories using the Linux command line. If you know some important and fundamental tools and concepts in Linux, you are good to go. In fact, handling files and directories through the command line might be quite easier than GUI sometimes. It helps you save a lot of time in searching files through the directories. In fact, there are tons of third-party file system managers and command-line tools that help you manage files and directories. However, in this guide, we will learn about the most fundamental ways of deleting or removing files and directories in Linux through the command line. Often, we need to remove a set of files and directories related to each other in some way or another, and searching such related files might not be easier through a GUI-based file system. The Linux command line allows you to use regex to simplify your search and delete those files and directories.

    A Word of Caution!

    You can use the two most common commands to delete or remove directories and files in Linux. These are the rm and rmdir commands. These are very powerful and need to be handled with utmost care to avoid losing any data. If you wish to remove files and directories using these two commands, please note that they remove the files and directories completely from the system. That means you will not find the deleted directories and files in the trash. You must follow some criteria before you decide to use the rm and rmdir commands on a day-to-day basis. These are:

    1. Use the rm and rmdir commands with special caution, especially when using regex and other search patterns. If you use an incorrect pattern that coincidently matches with some important config files, your system might crash, and you may lose important data.
    2. It’s the best practice to secure all your files by frequently backing them up.
    3. Utmost care and caution are required, especially when you use these commands as a root user.

    How to Remove Files and Directories Using the Linux Command Line?

    Let’s look at the most frequently used Linux commands to remove files and directories. We will begin with the basic file and directory removal commands in Linux. Please note that we will be creating and deleting folders and files in the home directory throughout the guide. Also, we will be using the tree package with the tree command that can be used to display the entire hierarchy of directories. You can install the tree package in your Linux system using the following command:

    $ sudo apt-get install tree

    1. Removing Files Using the Unlink Command

    This command is not so popular, but you can also use it to delete a file.

    $ unlink <file-to-be-deleted>

    Graph 1

    2. Removing Files Using the rm Command

    You can remove a single file using the rm command and may also need to include the path to the directory if the files you wish to delete are not in your current directory.

    $ rm <file-to-be-deleted>

    Graph 2

    You can also describe the whole path if you are not in the same directory.

    $ rm <path-to-file>

    Graph 3 To force delete a file, we can use the force or -f option. Even for write-protected files, you will not get prompted for a confirmation when you use the force option along with the rm command.

    $ rm -f <file-to-be-deleted>

    Graph 4

    To delete multiple files all at once using the rm command, you can use the following syntax.

    $ rm <file 1> <file 2> ..

    Graph 5

    You can use a wildcard (*) to delete files that have the same extension.

    $ rm *.txt

    Graph 6

    You can also remove files interactively using the -i option with the rm command. It will ask for a confirmation for all the files you want to delete.

    $ rm -i *.txt file3.dat

    Graph 7

    Please note that if a file is write-protected, it will generate a confirmation. You can confirm by writing ‘y’ and pressing enter. You can also use regular expressions to delete files using the rm command. For example, see the command below:

    $ rm file-name-[123].*

    Graph 8

    By default, the command line does not print any message or information about what it’s doing behind the scenes. You can use the -v or verbose flag to print a message every time you delete a file using the rm command.

    $ rm -v file-name-1.txt

    Graph 9

    3. Removing Directories Using the rmdir Command

    The rmdir command, which is short for remove directory in Linux, can delete or remove empty directories, i.e., directories without any files. You can remove a single directory, multiple directories, or specify a directory path along with the Linux command.

    $ rmdir <empty-directory>

    Graph 10

    To remove multiple directories, you can do so using the following command:

    $ rmdir <empty-directory-1> <empty-directory-2> ...

    Graph 11

    If you decide to delete a directory with a file or other directories inside it, you will encounter the following error:

    $ rmdir <non-empty-directory>

    Graph 12

    Suppose there are four directories, out of which three are empty and one is non-empty. Now, if you use the rmdir command to remove all these directories, it gives you an error on one of the non-empty directories. To ignore the error and keep moving on to delete the other directories, we can use the --ignore-fail-on-non-empty option along with the rmdir command.

    $ rmdir --ignore-fail-on-non-empty folder1 folder2 folder3 folder4

    Graph 13

    You can also use the -p (parent) option to delete the parent directories and the child directories. This option works because after deleting all the empty child directories, and the parent directory has no file or folder left inside it, it can also be deleted if you use the -p flag. After deleting the child directories, rmdir backtracks to the parent directory and checks whether it’s empty or not. If yes, it deletes the parent directory as well.

    $ rmdir -p sample/folder1

    Graph 14

    You can see that both the parent directory (/sample) and the child directory (/sample/folder1) have been deleted.

    4. Removing Directories Using the rm Command

    To remove an empty directory using the rm command, you can use the -d (directory) option.

    $ rm -d <empty-directory>

    Graph 15

    To remove a directory that is not empty and has contents inside it using the rm command, you can use the -r option, which stands for recursive .

    $ rm -r /path/to/non-empty-directory


    $ rm -r <non-empty-directory>

    Graph 16

    You can also use the -f (force) option along with the -r option to avoid prompts for write-protected files. The options that were supported for file removal using the rm command, such as -i (interactive) and -v (verbose) , are also supported by the rm command for directories when used with the -d (directory) option. Graph 17

    Wrapping Up!

    In this article, we discussed how to remove files and directories using the Linux command line. We discussed the unlink and rm commands to remove files with detailed examples. Next, we saw how to delete empty and non-empty directories using the rmdir and rm commands along with all the options that they support. We hope that by using this detailed guide on removing files and directories using Linux commands, you will be able to manipulate the file system in Linux easily. Happy Learning!

    People are also reading:

    Leave a Comment on this Post