PCsuggest

  • Quick tip
  • SECURITY
  • NETWORKING
  • OpenWrt
  • HARDWARE

Remove directory in Linux with command line

Updated - August 23, 2017 by Arnab Satapathi

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.

remove directory linux

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 rmdir, rm, gvfs-trash and trash-cli commands to accomplish our task.

Contents

  • Remove empty directories with rmdir command
  • Remove directories with rm command
  • Move directories and files to trash with command line
  • Synopsis - remove directory linux

Remove empty directories with rmdir command

The 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).

rmdir /path/to/my_directory

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.

The -p option is also useful when you need to remove an empty directory deep inside other directory, but all of them are also empty.

amazon prime logo
Try AmazonPrime for free
Enjoy free shipping and One-Day delivery, cancel any time.
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

The 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 sudo and -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 gvfs-trash and trash-cli commands.

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

gvfs-trash test_dir

The same thing applies on multiple directories, the location of trashed files is typically at ~/.local/share/Trash/ .

To move a directory and all contents under it with trash-cli, use

trash test_dir

To view the contents of the Trash folder, use gvfs-ls trash://command.

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.

  1. Use the rmdir command to remove empty directories, rmdir my_folder
  2. Use the rm command to recursively delete files and directories, rm -rf dir_1 file_1
  3. To send files and directories to trash, use gvfs-trash file_1 folder_1 folder_2
  4. If you need to restore something from the trash, use restore-trash command, 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.

  • How to rename files in Linux
  • How to rename directories in Linux

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

Filed Under: linux basics

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Copyright © PCsuggest.com · All rights reserved.

  • Home
  • About
  • Contact
  • Privacy Policy
  • Sitemap