How to remove a directory in linux with command line? It's one of the most asked question by Linux beginners. So here's how you can remove directory linux.
Of course using a file manager like Nautilus or Dolphin is the easiest way to do that in Linux, but it's not always possible. So, let's get started with the command line.
In this quick tip, we'll learn how to delete a directory in linux, and move them to trash with various command line tools.
We'll use the
trash-cli commands to accomplish our task.
ContentsShow / Hide
Remove empty directories with rmdir command
rmdir command can only remove empty directories. Here's how you can use this command.
rmdir my_directory # remove a single directory
You've to add the full path wile deleting directories not located in your current working directory(pwd).
To remove multiple empty directories at once, just add the folder names one by one, and you're done.
rmdir test_dir1 test_dir2 test_dir3
Also don't forget to include the full path when you need.
-p option is also useful when you need to remove an empty directory deep inside other directory, but all of them are also empty.
rmdir -p /home/username/test1/test2/test3/test4/test5
The above command will remove the test1 directory if all other subdirectories are empty.
Remove directories with rm command
rm command is versetile, you can remove multiple non-empty directory and files with this command, examples below.
To remove an entire directory and all of it contents with
rm, use the command below.
rm -r test_dir1
You can also use the
-f option to ignore nonexistent files, arguments and permissions, be careful with this.
rm -rf /tmp/test_dir1
If youu're facing a Permission denied error, then use the sudo command, it will promt you to type the super user password.
sudo rm -rf test_dir1
However you should not always use the
-f option with
rm command, as it never prompts before deleting anything.
Move directories and files to trash with command line
Moving something to trash is a safer bet than deleting permanantly, and here we'll use the
Most probabbly you've already installed gvfs-trash if running Ubuntu, if not install it with apt.
sudo apt-get install gvfs-daemons gvfs-bin sudo apt-get install trash-cli
The gvfs-trash is the part of GVfs project, while trach-cli is independent python based tool, and the trash-cli is a bit easier to use.
To move a directory and all contents under it with gvfs-trash, use
The same thing applies on multiple directories, the location of trashed files is typically at
To move a directory and all contents under it with trash-cli, use
To view the contents of the Trash folder, use
And to restore a specific file or directory, use
restore-trash command, it will prompt you to select the file/folder you want to restore.
Synopsis - remove directory linux
Now here's the round-up of all commands used above to delete directory in Linux.
- Use the rmdir command to remove empty directories,
- Use the rm command to recursively delete files and directories,
rm -rf dir_1 file_1
- To send files and directories to trash, use
gvfs-trash file_1 folder_1 folder_2
- If you need to restore something from the trash, use
restore-trashcommand, it will promt you.
So that's all about how to delete directories with basic linux commands, it is the first installment of linux for beginners series.
Read the second and third articles below.
If you've any question or suggestion, please feel free to ask ! And don't forget to share this tutorial with your linux loving friends