How to Remove Files and Directories in Linux?

Photo of author

By admin

If you mostly use Windows or Mac systems, you might be unfamiliar with the Linux-based operating systems. If you are new to Linux and trying to remove files from your computer, you may need to know some easy commands that will help you to achieve the same. So, let’s talk about how to easily remove files and directories in Linux distributions. In general, your computer must have a file manager from where you can access and manage all the files stored in your system.

For example, Gnome’s Files or KDE’s Dolphin lets you delete files and directories from their interfaces only. However, you cannot use those tools while you are working on the headless server. So, opening the terminal and using a few command lines is the best option. Read the entire article to uncover some basic and advanced commands such as rmdir, rm, and find to remove files and directories in Linux.

Before You Start to Delete Files on Linux

Like Windows and macOS, you can move your files or directories to the trash and recover them whenever you want. This way, you can remove the files temporarily and also get them back whenever required. 

However, if you delete the files or directories using the command line, it is going to be deleted permanently. Once you delete the files with the command line, you cannot recover them back. 

Also, you should make sure that in most cases, removing files and directories from your Linux system requires you to write permission on the directory and the content. If you don’t have permission, it will show you the “Operation not permitted” error. 

Also, note that whenever you put a directory name that has a space in it, you need to replace that space with a backslash (/).

Ways to Delete Files Using the rm Command in Linux Terminal

Commands such as rm, unlink, and rmdir help you remove files and directories from the Linux terminal with ease. This section is to talk about the rm command. The easiest way to delete something, a directory or a file on your Linux system is to use the rm command, give a space, and type the name of the file/directory that you want to delete.

rm file_1.txt

If you don’t find the file in the current directory, you can provide the path of the file’s location. Check out the command below:

rm ./path/to/the/file/file_1.txt

If you type more than one file name in the rm text, it will delete all the files altogether at the same time.

rm file_2.txt file_3.txt

You can also use the “*” and “?” symbols to delete multiple files of the same file extension. The “*” symbol represents multiple files, while the “?” symbol represents a single character. If you are still in the current directory, the “*” symbol will delete all the files of the extension you put in. For example, the command shown below will delete all the png files located within a specific directory:

rm *.png

If you use the ‘rm *.?’ command, it would delete all the files that have a single character extension, i.e, it would only delete.png1 and .png2, but not .png12. If a file is protected with a code or something, the system will show you a prompt before you can delete it. Simply type y or n and press “Enter.” But you can use the rm commands with the -i (interactive) option instead of the wildcards option, which will give you an option to confirm or decline the deletion. 

rm -i *.dat

However, if you don’t want to find that last option before deleting your files, you can use the f (force) option instead. Unlike the interactive one, it does not deliver a confirmation prompt. There is another option which is the -v: Verbose option, that will show you what rm is doing on the screen.

Steps to Delete All Files in the Linux Directory

Here is the process for deleting all the files in a selected Linux directory:

  • Open the terminal application in your Linux system.
  • Run the rm /path/to/dir/* command in a directory that will delete all the files and everything in a particular directory. Just be assured to replace the terms with the actual value. For example, replace the path with the actual location of the folder. 
  • To remove all sub-directories and files in the location, work with the rm -r /path/to/dir/* command line. 

Deleting Hidden and Non-Hidden Files 

In the Linux operating system, if you find a file or a directory that starts with a (.) dot character is a dot file. The dot file is basically treated as a hidden file in the directory. If you want to see if the hidden files pass the -a to the ls, you can run the following commands:

ls -a
ls -la

If you want to remove all the files in the directory but want to keep the hidden ones, you can use the below commands.

rm /path/to/dir/*
rm -rf /path/to/dir/*
rm *

You can also delete all the files in your selected directory along with the hidden ones. Just  run the below command:

rm -rf /path/to/dir1/{*,.*}
rm -rfv /path/to/dir1/{*,.*}

Bash Can Remove All Hidden and Non-Hidden Files

Bash is a scripting language that can delete all the hidden and non-hidden files from a directory if the dotglob option is set. It will delete the files that begin with a ‘.’ from the results of pathname expansion. You can turn on the dotglob option to delete files easily.

# Bash shell and may not work on other shells
# Turn on dotglob (set) #
shopt -s dotglob
# Remove all files including hidden .files #
rm -v ~/project/oldfiles/*
rm -vrf ~/project/oldfiles/*
# Turn off dotglob (unset) #
shopt -u dotglob

How Can You Delete Directories with the rm command?

‘rm’ can help you delete filled and empty directories on your Linux system. However, to remove empty directories, you will have to use the -d or (–dir) option or if you want to delete a filled directory with all its content inside, you will have to use the -r (–recursive or -R) option. For example, if the directory name is dir1 and you want to delete it along with all its content, you will have to use the following command:

rm -r dir1

If the files or the directory itself is protected with something or is write-protected, the system will show you a confirmation prompt. If you don’t want to see the prompt, as we said in the previous section, use the  -f option as shown below:

rm -rf dir1

Delete Multiple Directories At Once

You can remove multiple directories at once with the rm command along with the names of the directories separated by spaces after the rm command. Follow the direction stated below:

rm -r dir1 dir2 dir3

The -i or the interactive option will tell rm to show you the confirmation prompt where you can confirm to remove every subdirectory and file within a directory. But if there are multiple subdirectories in the location, it will show you multiple confirmations prompts, which will become quite annoying. You can press y and hit the Enter button if you want to remove the entire directory:

rm: remove 1 argument recursively? y

However, users can also use the regular expansion option to match and delete different directories at the same time. If there are _bak directories in the current directory, you can use the following command:

rm -r *_bak

However, this way of deleting directories is risky as the match system may not work all the time accurately. First, list the directories that you want to remove with the ‘ls’ command and then delete them using the ‘rm’ command. 

How to Remove Files and Directories with the Find Command?

Make use of the Find command-line utility to search for files and directories and delete them. You can use the find command to delete files and directories that run on a particular pattern of expression. Suppose there are several _cache files in your current directory; simply use the Find command to search for them and finally delete them. For this, you should run the following command:

find . -type d -name '*_cache' -exec rm -r {} +
  • /dir – recursively will search the current working directory (.)
  • -type d will restrict the search to the directories
  • -name ‘*_cache’ will only search the directories that have the _cache name at the end
  • -exec will open up an external command with rm -r
  • {} + will write more data to the end of the rm command line

How to Remove Directories with rmdir

With the rmdir command, you can only remove empty directories. So, whenever you are required to delete files, you can use the rm command and the rest of the time, use the rmdir command to remove empty directories from your Linux system. You can transfer the name of the directory to rmdir with the rmdir directory command. Or you can delete multiple empty directories by passing a list of directory names to rmdir:

rmdir directory1 directory2 directory3

If you want to delete a directory outside of the current directory, you can put the full name of that directory.

rmdir /path/to/directory

If you try to delete a directory that is not empty, it will show you an error message. The error message may read like: –ignore-fail-on-non-empty.

How to Remove Files Using the Unlink Command in Linux? 

If you have not used the rm command to delete files from a Linux directory, you can use the unlink command. However, with it, you can only remove a single selective file, but with the rm command, you can delete multiple files at once, which is easy. But the former solution lets you keep the important files and remove the ones you don’t need, so the unlink command is helpful. To delete the particular file, use the unlink command and mention the name of the item next to it:

unlink filename

If the file that you are trying to delete is write-protected, it will show you a confirmation message. To delete the same file, you can type y and hit Enter. However, if the file is not write-protected, it will be deleted immediately. 

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


This article showed you how to remove files and directories in a Linux system with different commands. If you need more help, check out our other articles or drop us a comment below.

Leave a Comment