5 Practical Examples To Delete / Remove Directory in Linux

by Ramesh Natarajan on June 26, 2009

Question: How can I delete empty directory, directory with files and sub directories in Linux / Unix ? Also, how can I use an alias effectively for rm and rmdir command?
 
Answer: You can delete empty directory using rmdir command, or directory with content using rm command. Deletion can be done interactively, recursively, forcefully, or through alias. In this article, let us review 5 practical examples that shows how to delete the directories in Linux like systems.

1. How to Delete Empty Directories in Unix?

rmdir command will delete the empty directories. i.e directory without any sub-directories or files.

rmdir DIRNAME

 
To ensure that you are deleting an empty directory you should use rmdir command. If there is any
files / directories in that directory it will display the following error.

$ rmdir test
rmdir: failed to remove `test': Directory not empty

2. How to Delete Nested Empty Directories in Linux?

Use option -p, to delete nested directories as shown below.

$ rmdir -p dir1/dir2/dir3

 
Note: Don’t get panic that how a directory can be nested and also empty. It is nested when you are invoking the command, but it deletes the inner most directory first, and makes the next level directory empty then it deletes that directory. And it continue doing so.

 
The rmdir -p dir1/dir2/dir3 is equivalent to

$ rmdir dir1/dir2/dir3 dir1/dir2 dir1

3. Delete Directory Which has Content (i.e Directory with Files and Sub-directories)

Some times you may want to delete directory which has contents in it. You can do it with rm command as shown below.

$ rm -rf DIRNAME

 
This will delete the directory including all the files and sub-directories. This is very dangerous when you use it accidentally as you cannot recover those files easily. So it is strongly recommended that you pay attention and think twice before executing the rm -rf command.

4. Delete Interactively: Avoid using -f in rm at the early stages.

If you’ve ever accidentally executed rm -rf by mistake, you may want to seriously consider using -i option to delete the files and folders interactively as shown below (especially under root).

Deleting a directory recursively & interactively.

# rm -ir DIRNAME

Deleting a file interactively.

# rm -i FILENAME

If you are a Linux newbie, don’t use -f option in root until get super comfortable with the command line. Instead, try to use -i option as shown above.

5. Useful rm and rmdir Aliases

You can make the interactive rm option as your default rm command using alias as shown below.

Alias to make rm interactive

# alias rm="rm -i"

While using the rm command, it will be always execute rm -i and ask for confirmation before deleting any files. But be careful that when you give -f option then you wont get the interactive prompt even if you have the -i option in it.

 
For example, in the following command -i don’t have any effect.

$ rm -irf DIRNAME 

 
In this case, the above command (with the rm alias) is equivalent to the following command.

$ rm -rf DIRNAME

 
All the following rm options are valid and does the same functionality. i.e recursive and forceful delete.

  • rm -fr
  • rm -rf
  • rm -r -f
  • rm -f -r

Alias to make rm verbose

If you want rm or rmdir to print what it is doing then you can use the traditional verbose option.

alias rm="rm -v"
alias rmdir="rmdir -v"

 
Note: Make this setting permanent by adding alias command to either ~/.bash_profile (or) ~/.bashrc file.

 
You can remove files or directories, empty or non-empty, nested or single using rm and rmdir commands. But before invoking the command think twice about deleting as it will become hard to recover a file once you’ve deleted it by mistake.
 
Have you ever deleted files by mistake using rm or rm -r command? How did you handle that situation? Share your story with us in the comment section below.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 21 comments… read them below or add one }

1 Ronald June 26, 2009 at 10:25 am

I don’t like setting those aliases like “alias=’rm -v’”. It makes your shell much slower when removing a directory with a lot of files.

Use ‘time’ to test it out!

2 NARDI June 28, 2009 at 5:02 am

Roland: I agree with this, but on the other hand, I like being informed what files have already been deleted. In case you’ll want to delete dir with a lot of files, you can backslash the rm command like: \rm -r somedir

3 Ronald June 28, 2009 at 5:16 am

Nardi,

I can’t really figure out how that’s working. Maybe you can give me some examples to use that \ option? :)

4 NARDI June 28, 2009 at 4:59 pm

It works like local, or temporary unalias (sorry, not sure about english). I just wanted to mention you can turn the verbosity off in case you have it recursively aliased like:
alias rm=’rm -v’
and want to delete many files a bit more quickly.

example with ls command:

$ ls
dir1 dir2 dir3 file1 file2 file3
$ alias ls=’ls -l’
$ ls
total 12
drwxr-xr-x 2 nardi nardi 4096 2009-06-29 00:48 dir1
drwxr-xr-x 2 nardi nardi 4096 2009-06-29 00:48 dir2
drwxr-xr-x 2 nardi nardi 4096 2009-06-29 00:48 dir3
-rw-r–r– 1 nardi nardi 0 2009-06-29 00:48 file1
-rw-r–r– 1 nardi nardi 0 2009-06-29 00:48 file2
-rw-r–r– 1 nardi nardi 0 2009-06-29 00:48 file3
$ \ls
dir1 dir2 dir3 file1 file2 file3

Hope this makes it clear.

5 Ronald June 29, 2009 at 3:21 am

Ah great, so the \ overrules the alias, makes sense :D
Thanks for the example!

6 Ah great, so the \ overrules the alias, makes sense July 6, 2009 at 2:02 am

Ah great, so the \ overrules the alias, makes sense
Thanks for the example!Ah great, so the \ overrules the alias, makes sense
Thanks for the example!

7 Ramesh Natarajan July 8, 2009 at 11:05 pm

@Nardi, Thanks for explaining in detail about how to ignore an alias command. You rock!

@Ronald, Thanks for starting the discussion on this topic to make it very meaningful and useful to others who are reading this.

8 pravin August 7, 2009 at 12:13 am

Hello all
i am having trouble in deleting a directory in ubuntu. the directory permission & ownership some how changed to like this ???? ?????? so i am not able to remove to this directory can any body help me.

Thanks,
Praivn Bari

9 aryan October 2, 2009 at 12:22 am

Hi
Actually I want to delete all the files and subdirectories in the directory(say ‘aryan’) without deleting the directory(‘aryan’) i.e. i want to empty the directory.

10 Hydrargentium January 20, 2010 at 10:37 am

To delete everything in a directory, WITHOUT deleting the directory itself (in answer to the previous post):

rm -r aryan/*

By using the the “/*” at the end, this path expands to “everything in the aryan directory”. Notice that this does not include the aryan directory itself. Therefore, the contents are deleted, but the directory itself is left intact.

Hg

11 nardi January 20, 2010 at 6:55 pm

@aryan
@Hydrargentium
There is one more thing. In case you have some hidden files in that directory (files beginning with ‘.’) they will be ignored while expanding the * wildcart.

WARNING: read whole sentence before trying to remove anything. =)
In fact using “aryan/.*” would help nither, because it also contains the “aryan/..” (try ls aryan/.*) which is your actual directory. You could ommit it by using “aryan/.[^.]*” but it seems way too compicated to me.

So in case you want to remove content of directory “aryan” including hidden files, I prefer to use rm -r `ls -A aryan`

OK. I tried it now with rm…. To correct myself you can simpy use: rm -r aryan/.* ayran/*
because rm will just complain about it like this:
rm: cannot remove `.’ directory `aryan/.’
rm: cannot remove `..’ directory `aryan/..’
but it will correctly erase all the content and nothing else. As I hate such errors i’d still use ls =)

Know it’s not too helpfull. Just FYI in case you use hidden files.

12 aryan January 20, 2010 at 7:47 pm

@Hydrargentium
@nardi

Yeah it works…rm -r aryan/*
thats gr8 to have your replies….

13 Richard March 7, 2010 at 6:50 am

Hello,

I hope someone can give me an answer to a question I have now for a few day’s…
: How can I Remove a complete Directory with sub folders and files when a Directory has a name with a space in between, some Directory called “Work Permit” to remove in my home Directory? Every time I get the message that it’s not recognized, and I also cannot CD to that Directory…

I hope someone can help me…. HELP!!!

14 nardi March 8, 2010 at 3:04 am

@Richard
you have to backslash the space.

$ cd Work\ Permit
$ rm -r Work\ Permit

quoting should work too.
$ cd “Work Permit”
$ rm -r “Work Permit”

15 Hamid Rekibi February 12, 2012 at 6:38 am

To delete the entire directory with all its subdirectories and files, you can do this if you have superuser access. Here is the command

# rm -rf / somedirectory

Be aware before using this powerful and dangerous command if you are not sure about your action! this will delete all the files RECURSIVELY and wipe the entire directory structures as well.

Don’t try it even if you are a normal user! Why?

Because if you are a normal user you would not have the permissions to delete root directory, but because the command has the -r flag this will work down the entire directory tree with no possibility to stop it when permissions are denied. This will results deletion of files and subdirectories.

16 Hamid Rekibi February 21, 2012 at 5:02 pm

**Elucidation**

// I just want to make it clearer about the Normal User and his/her directory, when using the rm -rf command//

Even if I were a normal user I should NOT take any risks by trying the command. Why?
Because when I’m logged as a normal user, I’m the owner of the Home directory and I’m allowed to add permissions to files and directories.

When using the rm command in combination with -rf flags, this results in a dangerous action and will work down the entire directory tree with no possibility to stop it when permissions are denied. This will results in deletion of files and subdirectories in Home directory which I’m allowed as a normal user.

17 chiku June 25, 2012 at 10:00 am

How to delete *.jar file in Unix. e.g
I have Mig.jar file under loop directory, how i should delete it.
Please help.

18 anita August 22, 2012 at 9:35 am

I want to delete files with extensions like exe ,inf. can you help.

19 raghuveer August 28, 2012 at 11:10 am

@anita : command is : rm *.exe *.inf , if i am wrong plz correct..

20 m September 20, 2012 at 1:24 pm

find -E / -regex “.*\exe|.*\.inf” -exec ls -l {} \; 2>/dev/null

if this works, replace -exec ls -l with -exec rm -f

2>/dev/null will redirect STDERR to /dev/null so you won’t see it in your terminal. Your STDERR messages are generally about files you don’t have permission to execute or write, but not always, run without this first.

Please never run -exec rm from find until you know what it will do – try ls or something else non destructive.

I am using find on OS X, which should be fine on BSD, Linux possibly (I can check if anyone cares)

The fact that someone is asking about find for Windows files is a bit disconcerting as find is a different program on that platform.

21 sadakar July 23, 2013 at 11:59 pm

How can we give write permission to copy the files ?
I am unable to copy the files either from PSFTP or WinSCP ?

Leave a Comment

Previous post:

Next post: