How to Delete Files and Directories in Linux from Command Line

How to Delete Files and Directories in Linux from Command Line

In this guide, you will learn several ways how to quickly and easily delete files and directories in Linux using the command line.

One of the basic file system administration tasks in Linux involves creating, modifying, and deleting different types of files and directories. Knowing some basic tools and concepts for file deletion comes in handy and can save you time.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

In this article, we will show you several ways related to deleting files and directories in Linux. We will also provide brief information on the various flags and options that you can use while deleting files and directories from your Linux station.

Delete files Using the rm Command

To delete a file, you need to use is 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 be also used to delete more than one file at a time. 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

Just in case you need it, here’s the man page for the rm command.

Delete files Using the unlink 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 is the difference between rm and unlink?

Above all, both commands are a wrapper to the same fundamental function which is 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 along with 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

There is something important to note here: the rmdir command is used only 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 of the listed directories, it will skip it and move on to the next one.

With -p options added to the rmdir command, each of the directory arguments 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/

Just in case you need it, here’s the man page for the unlink command.


By now you should have a clear understanding of 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.

Now practice the examples mentioned in this article, and you should be good to go.


  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