How to Remove (Delete) Directory in Linux

There are a number of alternative ways to take away directories in Linux techniques. In case you use a Desktop file supervisor akin to Gnome’s Information or KDE’s Dolphin, then you’ll be able to delete information and directories utilizing the supervisor’s graphical person interface. However, if you’re engaged on a headless server or wish to take away a number of directories directly, your only option is to delete the directories (folders) from the command line.

On this article, we’ll clarify tips on how to delete directories in Linux utilizing the rmdirrm, and discover instructions.

Earlier than You Start

When eradicating a listing utilizing a desktop file supervisor, the listing is definitely moved to the Trash and could be simply recovered.

Be additional cautious when eradicating information or directories from the command line as a result of as soon as the listing is deleted utilizing the instructions defined on this article, it can’t be totally recovered.

On most Linux filesystems, deleting a listing requires write permission on the listing and its content material. In any other case, you’re going to get “Operation not permitted” error.

Listing names with an area in them should be escaped with a backslash (/).

Eradicating Directories with rmdir

rmdir is a command-line utility for deleting empty directories. It’s helpful if you wish to delete a listing solely whether it is empty, while not having to test whether or not the listing is empty or not.

To delete a listing with rmdir, sort the command adopted by the title of the listing you wish to take away. For instance, to delete a listing named dir1 you’d sort:

$ rmdir dir1

If the listing just isn’t empty, you’re going to get the next error:

rmdir: didn't take away 'dir1': No such file or listing

On this case, you’ll need to make use of the rm command or manually take away the listing contents earlier than you’ll be able to delete it.

Eradicating Directories with rm

rm is a command-line utility for deleting information and directories. Not like rmdir the rm command can delete each empty and non-empty directories.

By default, when used with none possibility rm doesn’t take away directories. To delete an empty listing, use the -d (--dir) possibility and to delete a non-empty listing, and all of its contents use the -r (--recursive or -R) possibility.

For instance to delete a listing named dir1 together with all of its contents you’d sort:

$ rm -r dir1

If a listing or a file throughout the listing is write-protected, you can be prompted to verify the deletion. To take away a listing with out being prompted, use the -f possibility:

$ rm -rf dir1

To take away a number of directories directly, invoke the rm command, adopted by the names of the directories separated by house. The command under will take away every listed listing and their contents:

$ rm -r dir1 dir2 dir3

The -i possibility tells rm to immediate you to verify the deletion of every subdirectory and file. If the listing incorporates lots of information, this generally is a little annoying, so chances are you’ll think about using the -I possibility what is going to immediate you solely as soon as earlier than continuing with the deletion.

$ rm -rI dir1

To take away the listing sort y and hit Enter.

rm: take away 1 argument recursively? y

You may as well use common expansions to match and delete a number of directories. For instance, to take away all first-level directories within the present listing that ends with _bak, you’d use the next command:

$ rm -r *_bak

Utilizing common expansions when eradicating directories could also be dangerous. It is strongly recommended first to listing the directories with the ls command as a way to see what directories shall be deleted earlier than working the rm command.

Eradicating Directories with discover

find is a command-line utility that lets you seek for information and directories based mostly on a given expression and carry out an motion on every matched file or listing.

The commonest situation is to make use of the discover command to delete directories based mostly on a sample. For instance, to delete all directories that finish with _cache within the present working listing, you’d run:

$ discover . -type d -name '*_cache' -exec rm -r {} +

Let’s analyze the command above:

  • /dir – recursively search within the present working listing (.).
  • -type d – restricts the search to directories.
  • -name '*_cache' – search solely directories that finish with _cache
  • -exec – executes an exterior command with optionally available arguments, on this case, that’s rm -r.
  • {} + – appends the discovered information to the top of the rm command.

Eradicating all empty directories

To take away all empty directories in a listing tree you’d run:

$ discover /dir -type d -empty -delete

Right here is a proof for the choices used:

  • /dir – recursively search within the /dir listing.
  • -type d – restricts the search to directories.
  • -empty – restricts the search solely to empty directories.
  • -delete – deletes all discovered empty directories within the subtree. -delete can delete solely empty directories.

Use the -delete possibility with excessive warning. The discover command line is evaluated as an expression, and should you add the -delete possibility first, the command will delete every thing under the beginning factors you specified.

At all times take a look at the command first with out the -delete possibility and use -delete because the final possibility.

/bin/rm: Argument listing too lengthy

This error message seems if you use the rm command to take away a listing that incorporates an enormous variety of information. This occurs as a result of the variety of information is bigger than the system restrict on the dimensions of the command line argument.

There are a number of totally different options to this downside. For instance, you’ll be able to cd to the listing and manually or utilizing a loop to take away sub-directories one after the other.

The best answer is first to delete all information throughout the listing with the discover command after which delete the listing:

$ discover /dir -type f -delete && rm -r /dir


With rm and discover you’ll be able to delete directories based mostly on totally different standards quick and environment friendly.

Deleting directories is a straightforward and straightforward course of, however you should be cautious to not delete vital information.

You probably have any questions or suggestions, be happy to go away a remark.


Submit a Comment

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

ten + 15 =

Related Articles