Linux / UNIX: HowTo Empty Directory

by on September 20, 2011 · 8 comments· LAST UPDATED September 20, 2011


How do I empty a directory (delete all files) under Linux / Unix without deleting directory itself?

You can use the following commands:

[a] rm command - Delete one or more files or directories.

[b] find command - Find and delete all files from a specific directory.

Linux Empty Directory Using the rm Command

Consider the following directory structure:


To delete all files from /tmp/foo/ directory (i.e. empty /tmp/foo/ directory), enter:
$ cd /tmp/foo/
$ rm *

$ rm /tmp/foo/*

Delete All Files Using the Find Command

Consider the following directory structure:

		|	|---file1.doc
		|	|---file2.doc

To delete all files from /tmp/bar/ directory (including all files from subdirectories such as /tmp/bar/dir1), enter:
$ cd /tmp/bar/
$ find . -type f -delete

$ find /tmp/bar/ -type f -delete
The above find command will delete all files from /tmp/bar/ directory. It will not delete any sub-directories.

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 8 comments… read them below or add one }

1 Dan September 22, 2011 at 5:26 pm

How about:

rm -R /tmp/foo/*

To delete all files and subfolders?


2 nixCraft September 25, 2011 at 10:41 am

Hmm.. it is not about deleting subfolders. Just remove all files from all directories including subdirectories.


3 Ryan October 14, 2011 at 6:57 pm

Exaclty what I needed thanks. This works perfectly in a deploy script to clear the cache directories, on a push or pull.

For example if you use cakephp:

git pull origin master
find app/tmp/ -type f -delete

and your good to go!



4 Julien December 23, 2011 at 12:43 pm

Also what I was looking for but I have to confirm that I really want to remove all the files one by one.
I found something to set this off with rmstarsilent or summat but it does not work. Any hint ?


5 H4fidzi December 29, 2011 at 7:07 am

rm -rf /tmp/foo/*

just add f option tu force delete the files.


6 Adam Ziaja December 31, 2011 at 1:40 am

rm -fR /tmp/foo/


7 Julien January 2, 2012 at 4:21 pm

Thanks, it really helps !


8 Sivasankaran October 25, 2013 at 1:26 pm

I got it. what i need.. thx


Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: