≡ Menu

5 Practical Examples To Delete / Remove Directory in Linux

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.


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.

Add your comment

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

Comments on this entry are closed.

  • Ronald June 26, 2009, 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!

  • NARDI June 28, 2009, 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

  • Ronald June 28, 2009, 5:16 am


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

  • NARDI June 28, 2009, 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.

  • Ronald June 29, 2009, 3:21 am

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

  • Ah great, so the \ overrules the alias, makes sense July 6, 2009, 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!

  • Ramesh Natarajan July 8, 2009, 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.

  • pravin August 7, 2009, 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.

    Praivn Bari

  • aryan October 2, 2009, 12:22 am

    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.

  • Hydrargentium January 20, 2010, 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.


  • nardi January 20, 2010, 6:55 pm

    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.

  • aryan January 20, 2010, 7:47 pm


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

  • Richard March 7, 2010, 6:50 am


    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!!!

  • nardi March 8, 2010, 3:04 am

    you have to backslash the space.

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

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

  • Hamid Rekibi February 12, 2012, 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.

  • Hamid Rekibi February 21, 2012, 5:02 pm


    // 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.

  • chiku June 25, 2012, 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.

  • anita August 22, 2012, 9:35 am

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

  • raghuveer August 28, 2012, 11:10 am

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

  • m September 20, 2012, 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.

  • sadakar July 23, 2013, 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 ?

  • rohit Gupta September 3, 2015, 6:25 am

    I want to remove all folder or file in directory exception on file or one dir
    whats the command do this