Linux / UNIX: How To Empty Directory

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

You can use the following commands to empty directory in Linux and Unix like systems.

ADVERTISEMENTS

How To Empty Directory In Linux and Unix

  1. rm command – Delete one or more files or directories.
  2. find command – Find and delete all files from a specific directory.

Linux Empty Directory Using the rm Command

First, consider the following directory structure displayed using the tree command

	/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 sub-directories 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. To remove both files and directories, try:
find /path/to/target/dir/ -delete
The find commands options are as follows:

  • -type f : Delete on files only.
  • -type d : Remove folders only.
  • -delete : Delete all files from given directory name.

How to remove a full directory and all files in Linux

To remove a directory that contains other files or sub-directories, use the following rm command command. In the example, I am going to empty directory named “docs” using the rm -rf command as follows:
rm -rf /tmp/docs/*
Get verbose outputs:
rm -rfv /tmp/docs/*
How To Empty Directory in Linux and Unix
The rm command options are as follows:

  • -r : Delete directories and their contents recursively on Linux or Unix-like systems.
  • -f : Forceful removal. In other words, ignore nonexistent files and delete whatever found.
  • -v : Verbose outputs. For example, explain what is being done on screen.

Conclusion

You learned how to use the rm and find command to delete all files and sub-directories on Linux/macOS/*BSD and Unix-like systems. In other words, this is useful to empty folders on Linux. For more information see rm command help page here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
9 comments… add one
  • Dan Sep 22, 2011 @ 17:26

    How about:

    rm -R /tmp/foo/*

    To delete all files and subfolders?

    • 🐧 nixCraft Sep 25, 2011 @ 10:41

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

      • xDaizu Aug 10, 2015 @ 12:22

        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 Oct 14, 2011 @ 18:57

    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 Dec 23, 2011 @ 12:43

    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 Dec 29, 2011 @ 7:07

      rm -rf /tmp/foo/*

      just add f option tu force delete the files.

  • Adam Ziaja Dec 31, 2011 @ 1:40

    rm -fR /tmp/foo/

    • Julien Jan 2, 2012 @ 16:21

      Thanks, it really helps !

  • Sivasankaran Oct 25, 2013 @ 13:26

    I got it. what i need.. thx

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.