Linux Commands

How to Remove a Directory in Linux

Linux stores files and directories in a tree-like structure, making it easier to delete or remove any file or directory. You can also manage the files and directories in Linux using the desktop file manager like Gnome’s files or KDEs Dolphin. Apart from this, if you are running Linux on a headless server, you can delete or revoke the files and directories using the command-line interface. For this, you must have sound knowledge of command-line commands. With the command line, you will get the freedom to do anything, and one of the main commands is to delete something that you do not require.

But before you remove the directory with the help of the desktop file manager, you will see that the directory will get moved to the trash and recover it easily. But, you need to be very focused while running the delete or remove a command from the command-line interface as there is no way you can recover those files. But some Linux file systems will allow you to have adequate permissions for deleting the directory and its content. If you do not have the correct permission to do it, you will be the “operation not permitted” error.

We will learn various command-line commands for deleting the desired files and directories. Also, we will look for another option for deleting, like Gnome GUI.

Removing Files Using Command-Line Interface

You can use the command-line commands for removing or deleting the file from the Linux system. For this, you can use the “rm” or “unlink” command.

The difference between both the commands is that you can use the “unlink” command for deleting a single file, but with “rm,” you will be able to delete multiple files simultaneously. But make sure you run the command correctly using the correct file name, as removing files using the command line cannot be recovered easily.

For deleting a single file, use both the “rm” and “unlink” commands as follows.

unlink file_name
rm file_name

If the mentioned filename has write-protected permission, you will be asked for confirmation for deleting it, as shown below. But if the file does not have write-protected permission, then it will be deleted directly.

Output

rm: remove write-protected regular empty file 'file_name'?

For deleting multiple files simultaneously, run the “rm” command along with the multiple file names separated by space, as shown below.

rm file1 file2 file3

You can also use the (*) wildcard character for specifying multiple files. Suppose you are looking for deleting all the files with the .pdf extension, then you can use the below command.

rm *.pdf

If you want to confirm the deletion of each file before actually deleting it, you can use the “-i” option with the “rm” command, as shown below.

rm -i filename(s)

If you want to delete any file forcefully, even if it has the write-protected permission, use the “-f” option along with the “rm” command as shown below.

rm -f filename(s)

Also, you can combine some “rm” options to get the combined effect of options. Suppose you want to forcefully delta the file without a prompt in verbose form, use “-fv” together, as shown below.

rm -fv *.txt

Removing Directories Using Command Line

You can use two commands for deleting the directory in the Linux system. These are “rm” and “rmdir”. You can use the most commonly used “rmdir” command-line utility to delete an empty directory. Still, if you want to delete a directory and its content recursively, you can use the “rm” command.

For deleting a directory with the “rmdir”, you can use the directory name along with the command as shown below.

rmdir dir_n1

But if the mentioned directory is not empty and contains some file within it, you will be displayed with the below-mentioned error.

Output

rmdir: failed to remove 'dir_n1': No such file or directory

For this type of scenario, you should use the “rm” command for deleting the directory or delete the contents manually in the first place and then delete the empty directory.

The “rm” command will be beneficial in deleting both empty and non-empty directories. This command is a little complex and requires you to provide an option. You can use the “-d” option if the directory is empty. Also, you can use the “-r” option along with the “rm” command for deleting the directory recursively.

For deleting the directory along with its content, run the below command.

rm -r dir1

But if the directory or the file present within that directory has the write-protected permission, use the “-f” option to delete it forcefully. You can consider the below example for doing so.

rm -rf dir_n1

If you want to delete multiple directories, you can use the “rm” command following the names of the directories you wish to delete and separated by space.

rm -r dir_n1 dir_n2 dir_n3

If you want to confirm the deletion of each subdirectory or file within the directory, then you can make use of the “-i” option along with the “rm” command. But if you have several files, it is a little annoying, so you can only use the “I” option to get the confirmation once.

rm -rI dir1

You can also use the (*) wildcard character for matching and deleting multiple directories. Suppose you want to delete all the directories ending with _bak; run the following command.

rm -r *_bak

Deleting Directory Using Find Command

Find command is a common-line utility that allows you to match for files and directories based on the given expression and then perform the specified action to those files and directories. But the main use of the find command is to delete the file or directory. Considering a scenario where you wish to delete all the directories ending with “_cache”, you can run the following command in the current working directory.

find . -type d -name '*_cache' -exec rm -r {} +

Where
-type d – helps in restricting the search only to directories.
-name ‘*_cache’ – will search only for the directories that will end with _cache
-exec – it will help execute an external command with optional arguments; in this case, we are using the rm -r.
{} + – it will allow you to append the found files to the end of the rm command.

For removing all empty directories present in a directory tree, run the following command.

find /dir_n -type d -empty -delete

where
/dir_n – it will help in searching recursively in the /dir directory.
-type d – help in restricting the search only to directories.
-empty – helps in restricting the search only to empty directories.
-delete – it will delete all empty directories present in the subtree. It will delete only empty directories.

Make sure you use the -delete option with care as it can delete everything below the starting points you specified within the find command.

Conclusion

Deleting a file or directory in Linux is one of the most commonly implemented tasks. You can find various useful commands for deleting the file and directory and the various options for different actions to be performed. Here we have mentioned different ways to delete the files within the Linux system. For this, we suggest you have sound knowledge of command-line interface commands. Once you understand the working of commands, you can enjoy even running complex tasks.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.