How To Remove Files and Directories on Linux Terminal

Remove Files and Directories on Linux Terminal

In this tutorial, we will show you how to remove files and directories in the Linux terminal. Deleting files and folders is also one of the most essential. If you never get rid of anything, soon enough all those extra gigabytes will take a toll on your Linux system processing power, RAM, and hard drive, not to mention your digital life will resemble a dreadful episode of Hoarders.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges.

Remove Files and Directories on Linux Terminal

  • Remove Files with rm

To remove (or delete) a file in Linux from the command line, use either the rm (remove). The simplest case is deleting a single file in the current directory. Type the rm command, a space, and then the name of the file you want to delete:

rm file.txt

You can pass more than one filename to rm. Doing so deletes all of the specified files:

rm file_1.txt file_2.txt
  • Remove Directories with rm

To remove an empty directory, use the -d (directory) option. You can use wildcards (*) in directory names just as you can with filenames:

rm -d directory

More than one directory name deletes all of the specified empty directories:

rm -d directory directory1 /path/to/directory2

 To delete directories that are not empty and to suppress these prompts, use the -r (recursive) and -f (force) options together:

rm -rf directory
  • Remove Directories with rmdir

In Linux, you can remove/delete directories with the rmdir and rm. The difference between rm and rmdir is that rmdir can only delete directories that are empty. It will never delete files.

Delete a single directory in the current directory:

rmdir directory

Delete multiple directories:

rmdir directory1 directory2 directory3

Delete a directory not in the current directory by specifying the full path to that directory:

rmdir /path/to/directory

Deleting files and directories in Linux was made easy. Congratulations!  You have successfully learned how to remove or delete all the files in a directory using the rm command.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Tags: