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:
-
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. - It’s the best practice to secure all your files by frequently backing them up.
- 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>
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>
You can also describe the whole path if you are not in the same directory.
$ rm <path-to-file>
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>
To delete multiple files all at once using the rm command, you can use the following syntax.
$ rm <file 1> <file 2> ..
You can use a wildcard (*) to delete files that have the same extension.
$ rm *.txt
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
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].*
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
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>
To remove multiple directories, you can do so using the following command:
$ rmdir <empty-directory-1> <empty-directory-2> ...
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>
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
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
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>
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
Or
$ rm -r <non-empty-directory>
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.
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:
- Check disk space using DF command in Linux
- Find Disk Usage using DU command in Linux
- How to List Users in Linux?
- How to use the mkdir command in Linux?
- Compress and Extract Files Using tar Command on Linux
- How to Learn Linux?
- How to install Google Chrome on Ubuntu 20.04?
- SED command in Linux
- How to create users in Linux?
- How to Rename Files and Directories in Linux?
Leave a Comment on this Post