≡ Menu

Linux / UNIX: HowTo Empty Directory

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:

	/tmp/
	|
	|------foo/
		|---file1
		|---file2
		|---file3

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

OR
$ rm /tmp/foo/*

Delete All Files Using the Find Command

Consider the following directory structure:

	/tmp/
	|
	|------bar/
		|	
		|---file1.txt
		|---file2.txt
		|
		|---subdir1/
		|	|---file1.doc
		|	|---file2.doc
		|
		|---subdir2/
			|---image1.jpg
			|---image2.png

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

OR
$ 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.

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 9 comments… add one }
  • Dan September 22, 2011, 5:26 pm

    How about:

    rm -R /tmp/foo/*

    To delete all files and subfolders?

    • nixCraft September 25, 2011, 10:41 am

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

      • xDaizu August 10, 2015, 12:22 pm

        It’s a nice addition, though. It was exactly what I needed: this page was the first to show up when I googled “linux command empty folder”

  • Ryan October 14, 2011, 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!

    Thanks

  • Julien December 23, 2011, 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 ?

    • H4fidzi December 29, 2011, 7:07 am

      rm -rf /tmp/foo/*

      just add f option tu force delete the files.

  • Adam Ziaja December 31, 2011, 1:40 am

    rm -fR /tmp/foo/

    • Julien January 2, 2012, 4:21 pm

      Thanks, it really helps !

  • Sivasankaran October 25, 2013, 1:26 pm

    I got it. what i need.. thx

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , ,