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:

rm [OPTIONS] FILENAMECode language: CSS (css)

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

rm file.jpgCode language: CSS (css)

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.jpgCode language: CSS (css)

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.jpgCode language: CSS (css)

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

rm -f file.jpgCode language: CSS (css)

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*.jpgCode language: CSS (css)

If 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.jpgCode language: CSS (css)

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 emptyCode language: PHP (php)

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.

Conclusion

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.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%