Linux: How To Empty a 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

About author

admin
admin 87 posts

I have been working with Linux based systems since 2002. I hope the article published on this site will be useful to those new to server administration.

You might also like

Server Managment 0 Comments

Linux Centos 6 Change Server Hostname

I am a new CentOS Linux server user. How do I change hostname on CentOS Linux? How can I change server hostname on CentOS Linux  6? You need to use the

Linux Basics 0 Comments

Linux: Finding and locating files with find command part # 2

In the first part we talked about find command basic usage. Now let us see how to use find command (a) To gain lots of useful information about users and their files

Server Managment 0 Comments

HowTo: Linux List Disk Partitions Command

How do I list all hard disk partitions under Linux operating systems? Usually, your hard disk drive divided into one or more logical disks called partitions. This division is described