Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to remove all files and subdirectories in a directory. I used rm -r to remove all files, but I want to remove all files and subdirectories, excluding the top directory itself.

For example, I have a top directory like images. It contains the files header.png, footer.png and a subdirectory.

Now I want to delete header.png, footer.png and the subdirectory, but not images.

How can I do this in linux?

share|improve this question
cd into the directory and do an rm -Rf of all the files and directories in there. – Noufal Ibrahim May 4 '11 at 7:39

migrated from stackoverflow.com May 4 '11 at 11:26

4 Answers

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

share|improve this answer

Try this version:

 rm -r test/*
share|improve this answer
Thanks demas... Its working........ – White rose May 4 '11 at 8:37

To delete hidden files, you have to specify:

rm -r images/* images/.*

This will lead to an error like

rm: cannot remove `.' directory `images/.'
rm: cannot remove `..' directory `images/..'

but it will delete hidden files.

An approach without errormessage is, to use find/delete with mindepth. This is gnu-find.

find images -mindepth 1 -delete

Your find may lack a -delete switch.

share|improve this answer

rm's syntax is:

rm [OPTION]... FILE...

So, you have to state the appropriate path explicitly, e.g.

rm -r sub_dir/
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.