How to Delete Files and Directories in Linux

Last updated: March 15, 2022

Deleting unnecessary files and directories is required to keep your system organized and to save disk space. You can use the rm and rmdir commands to delete files and directories in Linux and other Unix-like operating systems.

In this guide, we learn how to delete files and directories in Linux using the command line.

Remove Files in Linux

Use the rm or unlink command to delete a file in Linux. These commands unlink the files from the file name and thus make the disk space, occupied by these files, available for further use. Once you delete a file, they are removed from your computer and you cannot restore them.

The following table describes some of the most commonly used command-line utilities to remove files in Linux:

CommandDescription
rm file.txtDelete the specified single file from the current working directory.
unlink file.txtDelete the specified single file from the current working directory.
rm file1.txt file2.txt file3.txtDelete multiple files in a single command.
rm /path-to-file/file.txtDelete the specified file from the given location without switching to the target directory.
rm *.pngDelete all the files that have the specified file extension in the current directory.
rm *sample*.*Delete all the files that have the specified string in the file name in the current directory.
rm *.?Delete all the files in the current directory that have only one character in the extension, for example, file.1 and file.2.
rm -f file.txtForcefully delete a write-protected file.
rm -i file.txtUse the interactive option to confirm before deleting a file.
sudo rm file.txtDelete a file that is owned by the root user and requires sudo privileges.

The unlink command can be used to delete a single file in one command, but the rm command can be used to remove multiple files in a single command.

Remove Directories in Linux

Use the rm and rmdir commands to delete directories in Linux. A directory can be either an empty directory or may contain files and other directories:

  • rmdir command - remove only the empty directories.
  • rm -d command - remove a directory along with the sub-directories and the files in the given directory.

The following table describes some of the most commonly used command-line utilities to remove directories in Linux:

CommandDescription
rm -d dirnameDelete an empty directory from the current directory.
rmdir dirnameDelete an empty directory from the current directory.
rm -r dirnameDelete a directory and all of its contents after confirmation.
rm -rf dirnameDelete a directory and all of its contents without confirmation.
rm -r dirname1 dirname2 dirname3Delete multiple directories in a single command.
rmdir dirname1 dirname2 dirname3Delete multiple empty directories in a single command. If any of the directories is not empty, the command will skip to the next directory.
rm /path/dirnameDelete a directory from the specified path without switching to the target directory.

Remove Files Examples

You can delete files in Linux in different ways using the rm command. The following examples show you the most common ways to delete files:

To delete a single file, specify the file name after the rm command:

rm file.txt

To delete multiple files in a single command, specify the list of file names after the rm command:

rm file1.txt file2.txt file3.txt
delete multiple files

To delete all the files that have .png file extension:

rm *.png
delete all files with .png extension

To delete all the files that have only one character in the extension:

rm *.?
delete all files with one character extension

Use the -i option to confirm before deleting a file:

rm -i filename
ask confirmation before deleting

If you do not want to confirm deleting a file, even the write-protected file, then use the rm -f command.

Remove Directories Examples

Deleting a directory is different from deleting a file. It matters if a directory is empty or not. The following examples show you how to delete directories using different command options:

To delete an empty directory, use the rmdir command followed by the directory name:

rmdir dir2
delete empty directory

If the directory is not empty, you will get an error stating that the directory is not empty. You can use the rm command to delete a non-empty directory.

When the rm command is used without any option, it can delete only a file. Use the rm -r command (recursive action) to remove a directory and its subdirectories, and all files they contain.

rm -r dir1
delete a directory and all its contents (including files and subdirectories)

You can also use the rm command to remove multiple directories at once in a single command:

rm -r dir3 dir4
delete multiple specific directories

To delete directories and all files without prompting, use rm -rf command. Should be very careful running this command because there is a chance of accidentally removing the wrong directory.

rm -rf dir5

The rm command for directories also supports wildcards and can be used in a similar fashion as used for the files.

Conclusion

In this tutorial, we explained how to delete files and directories using rm and rmdir. We suggest that you always check the file or directory before deleting it. It is a permanent operation that cannot be undone.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment