How to Delete Files and Directories in Linux from Command Line

How to Delete Files and Directories in Linux from Command Line

This guide will teach you how to quickly and easily delete files and directories in Linux using the command line.

One of Linux’s basic file system administration tasks involves creating, modifying, and deleting different types of files and directories. Therefore, knowing some essential tools and concepts for file deletion is handy and can save you time.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

This article will show you several ways to delete files and directories in Linux. We will also provide brief information on the various flags and options you can use while deleting files and directories from your Linux system.

Delete files Using the rm Command

To delete a file, you need to use the rm (remove) command and tell it what file or files you want it to delete. It has the following general syntax:


For example, to delete a single file named file.jpg, type the following in the command line:

rm file.jpg

The rm command displays a confirmation dialog for write-protected files. Otherwise, the command will directly delete the file. To make rm always prompt before deleting a file, you can use -i flag:

rm -i file.jpg

The rm command in Linux can also delete more than one file. Bypassing multiple filenames separated by a space as arguments to rm, you can delete multiple files:

rm file1.jpg file2.jpg file3.jpg

You can use the -f (force) flag to delete write-protected files without asking for confirmation:

rm -f file.jpg

In addition, the rm command also supports regular expressions. If you want to delete all three files (file1.jpg, file2.jpg, and file3.jpg), you can use:

rm file*.jpg

If you need it, here’s the man page for the rm command.

The unlink command also deletes a given file. This is another though not so popular, way of deleting a file in Linux.

You can use the unlink command to permanently delete a single file named file.jpg by typing the following:

unlink file.jpg

You are probably wondering what the difference between rm and unlink is.

Above all, both commands are wrappers to the same fundamental function, an unlink() system call. But the unlink command suffers from the following restrictions:

  • Unable to delete directories.
  • Unable to recurse.
  • Can only take one argument at a time.
  • Has no options other than --help and --version.
  • Less sanity checking.

For more about the unlink command in Linux, consult its manual page.

Delete Directories Using the rm Command

By adding the -r (recursive) option to the rm command in Linux, you can delete a directory and all its contents (files, subdirectories, etc.).

For example, to remove a directory named myfiles, type the following in the command line:

rm -r myfiles/

The rm command would ask you to validate the procedure if the specified directory or a file inside it is write-protected. To remove a directory without confirmation: 

rm -rf myfiles/

To delete multiple directories (for example, myfiles1, myfiles2, and myfiles3), type rm -rf followed by the directory names or path to directories, separated by a space, as follows:

rm -rf myfiles1/ myfiles2/ myfiles3/

Delete Directories Using the rmdir Command

Something important to note here is that the rmdir command is used when deleting empty directories in Linux. If you need to remove a non-empty directory, use the rm command.

If a specified directory is not empty, the output will display an error, as shown below.

rmdir: failed to remove 'myfiles/': Directory not empty

To remove a single empty directory, type rmdir followed by the directory name or path to the directory as follows:

rmdir myfiles/

To remove multiple directories (for example, myfiles1myfiles2, and myfiles3), type rmdir followed by the directory names or path to directories, separated by a space, as follows:

rmdir myfiles1/ myfiles2/ myfiles3/

If the command finds content in any listed directories, it will skip it and move on to the next one.

With -p options added to the rmdir command, each directory argument is treated as a pathname, of which all components will be removed if they are already empty, starting from the last component.

For example, the following command will delete both: the parent myfiles directory and its subdirectory subdir.

rmdir -p myfiles/subdir/

If you need it, here’s the man page for the unlink command.


By now, you should clearly understand how to delete files and directories in Linux from the command line.

It is important to remember that when you delete a file or directory in Linux using rm, unlink, and rmdir, it is instantly removed instead of moving towards Trash. Therefore, you will need to be careful while using these commands as you will not recover the removed files.

Practice the examples mentioned in this article, and you should be ready.

Bobby Borisov
Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.


  1. Thank you very much for the explanation and well written article. I had goofed and cancelled a Timeshift backup and was looking around elsewhere that indicated trying to use rmdir. So glad I was able to just pop in a simple sudo rm -rf / of the two I wanted deleted from the /timeshift/snapshots directory instead!

Leave a Reply

Your email address will not be published. Required fields are marked *