>

How to Delete a Directory in Linux Using GUI & Command Lines

NetworksLog

Updated on:

Confusing to delete directory Linux as it is not that easy that we delete folders in windows. To delete Directory in Linux, you have to follow some simple methods.

It can done through Graphical User Interface (GUI) and also through Command Line Interface (CLI). Through this article, you can learn How to delete a directory in Linux using GUI and and CLI that includes empty and non empty Directory.

Delete a Directory in Linux

Directory in Linux

If you are thinking about the word Directory, it is just a folder in Linux. Linux is one of the Operating Systems and quite different as compared to windows.

In Linux anything is considered as Files and folders are considered as Directory. There are different types of command( rm, rmdr, trash-cli or shred ) lines to delete Directory and also through GUI.

Delete Directory Linux using GUI method

Using GUI method is one of the easiest method to delete Directory in Linux. But to delete using Graphical Interface method, you have to install File manager and desktop environment. Using file manager like Nautilus or Konqueror is preferable. To delete folders just follow the simple steps :

  1. Open any file manager as suggested above
  2. Go to the path where you want to delete the Directory
  3. Choose the folder that you want to delete
  4. Press the Delete button form your keyboard
  5. Or right click on the selected folder and select the option move to trash from dropdown menu
  6. This deleted folder not deletes permanently as it will store in specific location called Trash that similar to Recycle Bin in Windows.
  7. If you want to delete that directory permanently, just select the directory and press “Shift + Delete” on keyboard at a time.
  8. Again it will ask you for confirmation, tap on delete in dialogue box.

How to Delete a Directory in Linux using Command Lines

As already discussed, we can also use Command Line Interface to delete folders in Linux. Using Command Lines is the fastest way to delete directory.

Also by using this method, we can delete folders permanently. There are different commands to use empty and non empty folders. So let’s check this :

Note : As we discussed, deleted directories or folders or files can store in Trash can if you use Graphical User Interface method to delete. Also make sure that your Trash can is empty or have some storage to store deleted files before deletion. But if you delete using command lines, the folder will be deleted permanently. You never get it back.

Linux Delete Directory using rmdir command

Mostly, Linux users use rmdir command to delete empty Directory that contains no files. If the Directory contains any files, it will shows a error. If you want to check the files or folders that presents in Directory to delete it just use the command “Is”.

So to delete Directory using rmdir command just follow the steps and also for example assume the name of the directory that you want to delete is The_Directory :

  1. Open Terminal on your PC by pressing “Ctrl + Alt + T” at a time or search for Terminal in application launcher.
  2. Navigate the directory that you want to delete by using the command cd (Example : cd ~/The_Directory)
  3. Press Enter button
  4. Enter rmdir command and then the name of directory to delete it permanently. (Example : rmdir The_Directory) and press enter again
  5. To verify that the directory deleted or not just enter the command “Is~/” and press enter
  6. If you not get the “The_Directory” folder name in the list, then it will be deleted permanently form your PC

Note: If you delete a non empty file using rmdir command, then you will get error message and the directory not get deleted. Error message will seems like this :

Example : rmdir: failed to remove The_Directory : Directory not empty

Linux Remove Directory using rm command

Generally, Linux users use rm directory command to delete non-empty files. It deletes all the files and folders or sub directories that present in that Directory.

So be careful with this method before deletion and it is suggested to backup any important files before performing deletion through rm command. To do this just follow the steps given below:

  1. Open the terminal on your PC
  2. Navigate to the Directory that you want to delete using “cd” command
  3. Check the files present in the directory that you want to delete using Is command
  4. Use the “rm” command with -r option which indicates recursive to delete all files of the directory permanently.
  5. Example : rm -r The_Directory
  6. It will asked for a prompt to confirm deletion as this command delete non-empty files permanently
  7. Enter “Y” for yes or “N” for No based on your choice and press Enter button in keyboard
  8. Verify that the directory is deleted or not by giving command “Is~/” and press enter

Note: You can also use other options as we used recursive option (-r) for deleting non-empty directory in rm command. This options are also called as flags, we will discuss it in detail :

  • -f – This flag is used to skip the confirmation prompt at the time of permanent deletion while using rm command
  • -i – This flag is used, if you want confirmation prompt for every deletion of file that you are deleting in Directory
  • -d – This flag is used to delete empty Directories while performing rm command
  • -v – By using this option, you get detail information of what you are performing currently
  • -r – This option is used to delete the directory permanently

How to do Force Delete of Directories in Linux

If you want to delete any non-empty directory by using rm command by bypassing confirmation prompt that is ‘y’ or ‘N’, we can use force delete option. To do this just add “-f” option while performing “rm” command. For example :

  • rm -rf The_Directory

How to Remove Directory in Linux using Find Command

We can use “Find” command to delete empty or non-empty directories in Linux. By using this, it can fetch the PC for directory you are looking for and delete it permanently from PC. Now let see the command line for deleting empty and non-empty directory using “Find” command.

For empty directory : find “path to navigate” -type d -name “directory name” -delete

For non-empty directory : find “path to navigate” -type d -name “directory name” -exec rm -r {} +

We used “-delete” flag and “-exec” flag for deleting directories using Find command with “rm” command and “-r” option.

Use shred command to remove a Directory in Linux

It is the most secure way to use “shred” command to delete files of directory in linux, but it takes long time as compared to other commands. By using this command, it can overwrites the file multiple times before deleting the directory. So just look at example to perform “shred” command to delete directory :

Example : shred -u “file name”

Use trash-cli command to delete directory Linux

It is the most safest way to delete files or folders or directory in linux as it stores the file in Trash first and then we can delete it from trash using another command. So to transfer any file to trash by using trash-cli command, let’s look at the example :

Example : “file name” trash-cli

By performing this command, your file transfers to trash and then you perform deletion from trash by using following command :

Example : trash “file name”

How to Remove Directory in Linux with Similar Pattern Filenames

If you want to delete any directories that contains filename with very similar pattern, using glob patterns in the command.

For example, let’s assume you have three files named create1, create2 and create3. If you want to delete it by performing “rm” or “rmdir” commands and also check for glob patterns command to :

For empty folders : rmdir create1 create2 create3

For non-empty folders : rm -r create1 create2 create3

Now by using, glob pattern you can simply perform above task as given below :

  • rmdir create*
  • rm -r create*

Conclusion

  • Be careful, if you are deleting the directories using command lines as it will delete your directories permanently
  • Mostly, users use rmdir command to delete empty files as if you try to delete non-empty using it shows an error
  • Also to delete non-empty files users use rm command
  • Make sure that you are deleting correct directory by rechecking the file name that you are deleting when you are using the -r option for deletion as it is irreversible.
  • Also verify the file permissions before deleting because some files need root access for deletion
  • If you want to know more about the command that you are using to delete, just enter the command “man” and followed by the command that you want to know about to get manual guide.

FAQs

What is the fastest way to delete directory Linux

The fastest way to delete directories in Linux is Command Line Interface (CLI) method. Among all command lines, rm and rmdir are preferable.

To delete empty files use “rmdir” command and for non-empty files use “rm” command with “-r” option. It deletes directories permanently. “shred” is also a secure way to delete files but it takes some longer time as it overwrites the file before deleting.

What is the reason for unable to delete directory Linux ?

As not all files have same properties. Some files requires permission at the time of deletion. That files require root access to delete it by performing command lines. But to delete this type of files, we have a method called force delete in command lines which bypass the permission. To do this, just follow the command : rm -rf “directory name”

What is the main use of CD command in Linux ?

CD command is used for multiple purposes in linux, but main purpose of using this command is to navigate and to modify the current directory that you are working. It can be done through terminal of linux.

What is the difference between mv command and rm command in Linux ?

In Linux, mv command is used to move files or sub directories from one directory to other directory. To use this command just follow this example : mv file1 file2 “path to new dir” . rm command is used to delete directory permanently in Linux. To use this command follow the example : rm -r “directory name”

Leave a Comment