How to Delete Files and Folders in Linux

Linux command line fundamentals are absolutely essential for every future system administrator and advanced Linux user. Today we’ll cover another basic function – deleting files and directories in Linux using the command line.

The rmdir Command

The command used to delete empty directories in Linux is rmdir.

The basic syntax of this command is easy to grasp. here’s an example:

rmdir [option] DirectoryName
  • rmdir is the command
  • [option] is an optional modifier that changes the way the command behaves
  • DirectoryName is the directory you want removed

If no option is provided, rmdir simply deletes the directory whose name is provided as the destination. Before using this command, you’ll have to log into your VPS server using SSH. Here’s an article to help you out.

Remove Files in Linux Using the rmdir Command

Before using the rmdir command we suggest you check the files present in a directory with the ls command. In our case, we have a directory named Dir1.

rmdir Dir1

This command will delete the empty directory named Dir1. Simple enough, right?

You can also delete multiple directories by separating their names by spaces. For instance:

rmdir Dir1 Dir2 Dir3

After executing this command, the directories named Dir1, Dir2 and Dir3 will be deleted.

Let say we have a directory named Dir3. Dir3 has subdirectories and files inside of it. Now if we use following command:

rmdir Dir3

We will receive an error like this:

rmdir: failed to remove ‘dir3’: Directory not empty

As you may have guessed from the output, rmdir only works with empty directories.

The rmdir is a smart utility, and only allows you to delete empty directories as a built-in safeguard to prevent accidental loss of data. Remember it’s almost impossible to recover deleted data on any Linux distribution.

The -p option lets you delete the directory as well as its parent directories.

rmdir -p Dir1/Dir2/Dir3

This above command will delete Dir3 and its parent directories Dir2 and Dir1.

The -v option outputs a diagnostic text for every directory processed. Using this option will output a confirmation listing all the directories that were deleted.

The rm Command

The rmdir command is great for safely removing unused and empty directories. If you want to remove files or directories containing files, you would have to use the rm command.

The basic syntax of this command is similar to rmdir:

rm [option] FileName

Remove Files in Linux Using the rm Command

Use the rm command to remove the file named article.txt:

rm article.txt

If we have a directory with the name Dir1 containing subdirectories and files we will have to attach the -r modifier. The command would look like this:

rm -r Dir1

The -r option recursively removes directories and their content.

Another helpful option is -i. This will ask you to confirm the files that will be deleted individually. That way you can avoid any unpleasant mistakes.

You can also delete empty directories using -d option. The following command will delete an empty directory with name Dir1:

rm -d Dir1

You can use a wildcard (*) and a regular expansions to match multiple files. For instance, the following command will delete all pdf files placed in the current directory.

rm *.pdf

You can use variation of all the commands listed above to delete files with other extensions like .txt, .doc, .odt, etc.

The -f option lets you forcefully delete everything placed in a directory. The command would look like this:

rm -rf Dir1

The above command will delete everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.

You can also delete more than one directory at a time. The following command will delete three directories Dir1, Dir2 and Dir3 in a single command.

rm -rf Dir1 Dir2 Dir3

Congratulations, you successfully mastered all the basic functions of the rm and rmdir commands!

Wrap Up

In Linux deleting a single file by accident can lead to major issues. That’s why it’s essential to master the two main commands for file and directory removal – rm and rmdir. In this article we cover these two commands and the various options that can be used with them.

We hope you found this article useful! Remember, once you delete a file or directory from Linux, you can’t recover it, so be extra careful! Good luck.

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Rename Files in Linux