Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory which I want to delete. The directory's name is a random string of letters and numbers. I know the path to its parents directory, so how would I recursively delete the contents of this directory?

There are no other files/directories in the parent directory.

I know I have to do something like this:

$ rm -r path_to_dir

but since I dont have the dir name I am not sure if this would work.

share|improve this question
Is there a reason that you can't delete the parent folder, then recreate it? – soandos Oct 24 '12 at 1:48
@soandos Hahaha now I feel stupid :$ – stackErr Oct 24 '12 at 1:50
up vote 1 down vote accepted

Assuming path_to_dir is the path to the parent directory, you can just do this:

rm -r path_to_dir/*

The asterisk (*) is a wildcard, so that recursively deletes everything under the parent directory. It's easier than deleting and recreating the entire parent directory, especially if for example it has some custom permissions that you want to preserve.

share|improve this answer
Thanks for the code and explanation! – stackErr Oct 24 '12 at 1:57
rmdir path_to_parent/* would work too and is safer because it removes only empty folders, but not recursively (since a folder with a folder in it isn't empty). – Ярослав Рахматуллин Oct 24 '12 at 1:59
@Ярослав Рахматуллин this would not work for me since the directory contains other files and folders. – stackErr Oct 24 '12 at 2:09

You should try to do something like:

rm -rf path_name
mkdir path_name

share|improve this answer
Thanks! Just never thought of this. – stackErr Oct 24 '12 at 1:52
I am going to accept Indrek's answer since his solution is what I was looking for. – stackErr Oct 24 '12 at 1:54
@Indrek has a cleaner solution (though mine will work) – soandos Oct 24 '12 at 1:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .