How do I remove files and directories in Linux?
This is the common question beginners ask when migrating from Windows to Linux.
In Windows, it is very easy. Simply, select the file, right-click, and choose the delete option. The file moves to recycle bin and from there also you can remove using the same method. Alternatively, use the keyboard key Delete or Shift-delete to permanently delete the file skipping recycle bin.
This is also possible in Linux if you are using a desktop environment. You are going to learn that as well.
At the end of this article, you will learn how to remove files/directories using rm, unlink, rmdir commands. What are the different options? How to delete single, multiple files and recursively delete directories, etc.
Remove files on Linux
Linux provides rm and unlink commands to delete files. Remember, unlink deletes a single file while rm allows you to delete multiple files at once. You can refer to this discussion on stackexchage to understand the difference.
The basic syntax of rm is rm [OPTION]… FILE… while unlink is a function. You can use rm –help or unlink –help on the terminal to know more about it.
Let’s create some dummy files using the touch command.
touch file1.txt file2.txt file3.txt file4.txt
List down all the files using the ls command.
ls -lrt file*
Files are created successfully. All files are blank here as touch creates an empty file only. You can have non-empty files as well.
1. Delete a single file in Linux
Use either the rm or unlink command with the filename as a parameter to delete a single file. Make sure to provide a complete filename with the extension else you will get
rm: cannot remove 'file': No such file or directory error.
e.g. delete file1.txt using the rm and file2.txt using the unlink command.
If you do not get any feedback/error on the terminal that means the file is removed successfully.
Verify using the ls command.
ls file1.txt file2.txt
ls: cannot access 'file1.txt': No such file or directory ls: cannot access 'file2.txt': No such file or directory
There is no file with names file1.txt and file2.txt.
ls *.txt file3.txt file4.txt
There are now only 2 files remaining.
Note:- rm or unlink command removes the pointer to the file. Data is still there and you can recover it only using advanced data recovery tools. So please be careful while using these commands. If you want to completely delete a file that no one can recover use the shred command.
The shred command actually overwrites the file multiple times and then removes it. That makes file content unrecoverable e.g
shred -vzu -n5 file3.txt
shred: file3.txt: removing
shred: file3.txt: renamed to 000000000
shred: file3.txt: removed
Note: shred is beyond the scope of this article. You use it when you want to securely remove files. Refer to this article on freecodecamp for more details.
Now, you learned basic file removal. Let’s deep dive and see how options change the behavior of the rm command.
2. Delete the write-protected file in Linux
A write-protected file does not allow it to change or delete. When you delete a write protect file, the command asks for confirmation if you really delete it e.g
rm file3.txt rm: remove write-protected regular empty file 'file3.txt'?
Type ‘Y’ or ‘y’ to continue and Linux removes the write-protected file.
The unlink command does not ask for any confirmation and simply removes the file.
You can remove the write-protected file if you are the owner of the file, else become a super user to remove the file.
This message “rm: remove write-protected regular empty file ‘file3.txt’? ” is useful but could be annoying when you are deleting multiple files and each time you have to confirm.
This is inconvenient.
You can use the rm command with the -f option. Here, -f stands for force means ignore nonexistent files and arguments, never prompt.
Let’s try it.
rm -f file4.txt
See, in this case, there is no confirmation. If the file exists command deletes or simply ignores it.
3. Delete multiple-files in Linux
To delete multiple files from the Linux terminal, use the rm command with multiple file names separated by space as shown below.
rm file1.txt file2.txt
4. Delete the file by specifying the full path in Linux
All examples above assume that you are running the rm command in the same directory where files are present. What if you want to delete a file from some other folder?
Specify the full path as an argument to the rm command as shown below.
It also accepts multiple file names with full paths as arguments.
rm ~/Pictures/file1.txt /home/user/sample.txt
5. Interactively deleting files in Linux
Most of the commands in Linux neither confirm nor provide any feedback to the user. rm is no exception. If you want it to ask before deleting, then use
-i the option. Here, -i stands for interactive.
rm -i file1.txt
rm: remove regular file 'file1.txt'?
Press Y and enter to delete the file or N to cancel.
6. Delete the file using a wildcard pattern match in Linux
rm command also supports wildcards pattern match. This makes it easier for automated scripts and increases productivity.
An example of this is that to delete all text files having .txt extension, use
You can also use * to match starting or ending of the file name. Let’s say delete all log files which start with the name log
Remove file which starts with abc and ends with extension .log
This is a very useful feature that allows you to selectively remove files using pattern matching.
Note: Pattern matching is case-sensitive. The pattern search strings abc* and ABC * are not the same.
Deleting directories from the Linux
In Linux and other Unix-like operating systems, a directory is a file system object that contains a list of filenames and the corresponding inodes for each of those files. Essentially, a directory is a “folder” that can hold files, other directories, and links. It helps in organizing files and provides a hierarchical structure to the filesystem.
The mkdir command allows you to create a directory. Create some for demonstration.
mkdir dir1 dir2 dir3 dir4
List down all directories.
dir1: dir2: dir3: dir4:
You can use rm and rmdir commands to delete directories in Linux, but there is a catch. You have to use rm command with -r or -d option while rmdir allows you to delete the empty directories only.
Let’s understand more about it in the examples given below.
1. Delete Empty Directory in Linux
To delete empty directories, you can use the
rm command with the
-d or the
rmdir command as
rm -d <directory_name> and
rmdir <directory_name> respectively.
Remove empty directory dir1 with rm and dir2 with the
rm -d directory_name
2. Delete non-empty Directory in Linux
To remove the non-empty directory, use rm command with -r option. Here, – r stands for recursive.
rm -r dir1
Here, you can not use rm -d or rmdir command. If you try, you will get errors like
rm: cannot remove 'dir1': Directory not empty ,
rmdir: failed to remove 'dir1': Directory not empty error on the terminal.
rm -d dir1
rm: cannot remove 'dir1': Directory not empty
rmdir: failed to remove 'dir1': Directory not empty
3. Delete multiple directories in Linux
You can also remove several directories at the same time using rm command. Specify all directories as input parameters separated by space as
rm -r directory_name directory2_name
In our case,
rm -r dir1 dir2 dir3
What about absolute paths?
Yes, you can also use the absolute path of a directory or multiple directories in different paths.
rm -r /home/sandi/dir1
rm -r /home/sandi/dir2 /home/sandi/Downloads/dir5
3. Delete files and directories together in Linux
You may have observed rm command also deletes files. So can you use rm command to delete files and directories together?
Yes, that is possible. Simple mention file names and directories as input parameters.
rm – r file1 file2 dir1 dir2
Unlike the graphical interface, here there is no recycle garbage can or trash can and every time you delete files or directories with the
rm command they will be permanently deleted.
4. Interactively deleting folders in Linux
rm and rmdir command does not ask for or provide any feedback. If you want to interactively delete directories, you have to use the -i option for the rm command.
rm -ri dir4
rm: remove directory 'dir4'? Y
Enter Y to proceed with deletion and N to cancel.
5. deleting write-protected(read-only) directory in Linux
When you try to delete write protected directory you get
rm: remove write-protected directory 'dir1'? a confirmation message. Enter Y or N to delete or cancel.
rm -r dir3
rm: descend into write-protected directory 'dir3'? y rm: remove write-protected directory 'dir3'? y
You can use -f option (force) to suppress this confimation message as
rm -rf dir3
This will remove the directory if it exists without prompting or simply ignores it if the directory does not exist. You are not going to see any prompt.
6. Delete the Directory using a wildcard pattern match in Linux
You can also delete directories using a wildcard pattern that match the way we did for files.
Delete directories starting with the log
rm -r log*
Remove directories ending with the archive
rm -r *archieve
Delete directories starting with abc and ending with log.
rm -r abc*log
This is a very useful feature that allows you to selectively remove directories using pattern matching. Please note, the search pattern is case sensitive.
7. Recursively Delete Directory in Linux
This point needs more discussion. You are using the rm with the -r option to remove directories. Here – r stands recursive.
What does that mean?
You can have a hierarchy of directories which means directory inside directories along with files. How to get rid of all files and directories under it.
Consider below directory structure.
│ └── child3
Now you can delete all directories and files under dir1 using the simple command as,
rm -r dir1
There are chances that some files are write protected. You may get an error on the terminal. To avoid this use -f option as
rm -rf dir1
Using Linux GUI to delete files and folders
A functional desktop operating system could not be functional without a modern and intuitive graphical interface to help both novice and expert users with its operation.
For this example, let’s take Linux Mint as an example because it is one of the main distributions. However, the process will be very similar for every file browser on Linux.
The first thing to do is to open the file browser and navigate to the folder or file you want to delete. In this case, I have created the
example folder with five text files and a folder with another text file. Thus, the image with the working directory.
To delete a file or folder using the GUI on Linux, just click on it and when it is selected, press the right mouse button to display the context menu.
There you will see the option to move to the Rubbish Bin.
This option does not permanently delete the file but sends it to a special directory from where you will have a time limit to retrieve it.
So open the recycle trash can from the file browser favorites.
You will see the deleted files, from there you can either recover them or delete them forever. In this case, you can select the file you want to delete permanently and right-click to find the Delete permanently option.
If there are several of them, then you can select them with the mouse or press the option to empty the trash.
Now the file is permanently deleted. There is no option to recover it.
This same process can be done with directories, obtaining the same results.
Note: In some file managers, it is possible to permanently delete files by selecting the file or folder and pressing SHIFT + DELETE.
In the realm of Linux, efficient management of files and directories is crucial. Whether you’re performing routine cleanup or configuring a system, knowing how to safely remove files and directories is an essential skill.
While commands like
rmdir offer powerful options to handle these tasks, caution is paramount to avoid unintentional data loss. It’s always recommended to use flags like
-i for a more interactive removal process, especially when getting started.
As with many Linux commands, mastering the art of file and directory removal requires practice, attention to detail, and a solid understanding of the command’s capabilities. Always ensure you have backups of important data, and familiarize yourself with the tools and options available, so you can tailor your approach to suit specific tasks.
Linux provides flexibility and power for all levels of users; it’s up to the user to harness it responsibly.
I hope you liked this post and help our site grow by sharing it with your friends.