Linux or Unix find and remove files with one find command on fly

by Vivek Gite [Last updated: October 1, 2008]

Q. How do I find and delete files under Linux / UNIX operating systems?

A. Some time it is necessary to find out files and remove them. However, rm command does not support search criteria.

However, with find command you can search for files in a directory and remove them on fly.

You need to combine find and rm command together.

Fortunately find command makes this operation quite easy. You can use find command as follows:

Linux or UNIX - Find and remove file syntax

To remove multiple files such as *.jpg or *.sh with one command find, use

find . -name "FILE-TO-FIND"-exec rm -rf {} \;

OR

find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

The only difference between above two syntax is that first command can remove directories as well where second command only removes files.

More Examples of find command

(a) Find all files having .bak (*.bak) extension in current directory and remove them:
$ find . -type f -name "*.bak" -exec rm -f {} \;

(b) Find all core files and remove them:
# find / -name core -exec rm -f {} \;

(c) Find all *.bak files in current directory and removes them with confirmation from user:
$ find . -type f -name "*.bak" -exec rm -i {} \;

Output:

rm: remove regular empty file `./data0002.bak'? y
rm: remove regular empty file `./d234234234fsdf.bak'? y
rm: remove regular empty file `./backup-20-10-2005.bak'? n

Caution: Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to Linux/Unix system.

Above examples are specific to this topic only.

See also : Other find command usage

For detailed information on find command please see Finding/locating files with find command part # 1, Part # 2

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 11 comments… read them below or add one }

1 Carl 03.08.07 at 9:11 pm

Awesome, this was exactly what I needed to delete a huge amount of files when I ran out of inodes. Just doing an rm * would result in an error, but this deletes the files one at a time. Thanks

2 Rob 03.29.07 at 3:19 pm

do any of the above commands delete .bak files recursively throughout an entire directory tree, or would I need to cd to each directory to delete its .bak files?

3 nixcraft 03.30.07 at 3:24 am

Rob,

> would I need to cd to each directory to delete its .bak files?
No

find command will go to each sub directory. For example delete all *.bak from /data2 dir, use

find /data2 -type f -name "*.bak" -exec rm -f {} \;

4 Test 09.12.07 at 12:37 am

Why does it require a backslash to terminate with the ;? I need to place this in an applescript under “do shell script” but it hates that backslash. Is there any way around it?

5 vivek 09.12.07 at 4:53 am

A backslash is required, otherwise shell will treat ;? as part of shell command.

6 Atul 02.20.08 at 3:33 pm

for removing directories in same manner, use command find . -type d -name “DIRNAME” -exec rm -rf {} \;

7 Atul 02.20.08 at 3:34 pm

to recursively search and delete directories use
find . -type d -name “DIRNAME” -exec rm -rf {} \;

8 Nick 03.25.08 at 6:33 pm

Why not use the -delete option of find?

9 Dr Thangpa Serto 04.15.08 at 6:01 am

[root@localhost ieee80211-1.2.18]# make
Checking in /lib/modules/2.6.18-53.el5xen for ieee80211 components…
make -C /lib/modules/2.6.18-53.el5xen/build M=/root/Desktop/ieee80211-1.2.18 modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-xen-i686′
CC [M] /root/Desktop/ieee80211-1.2.18/ieee80211_module.o
In file included from /root/Desktop/ieee80211-1.2.18/ieee80211_module.c:52:
/root/Desktop/ieee80211-1.2.18/compat.h:113:
Hi!

I am a linux newbie. I installed RHEL5 on a compaq Presario V3000 laptop.

Now i tried to install the wifi drivers ipw3445… but i am stuck her

error: redefinition of ‘kmemdup’
include/linux/slab.h:208: error: previous definition of ‘kmemdup’ was here
make[2]: *** [/root/Desktop/ieee80211-1.2.18/ieee80211_module.o] Error 1
make[1]: *** [_module_/root/Desktop/ieee80211-1.2.18] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-xen-i686′
make: *** [modules] Error 2
[root@localhost ieee80211-1.2.18]#

Please help

10 Dani 09.18.08 at 6:26 pm

This page has just save 3 hours of stressful, worthless work in my life. Thank you so much.

11 Anonymous 11.07.08 at 11:27 pm

How can I remove files that contain a certain string of text? For example: if file contains any of these symbols, remove the file:

{ }
[ ]
( )

Just to name a few, but there might be others. I need a bash script to do this.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

Previous post: How to find out Router mac address

Next post: Open rar file or Extract rar files under Linux or UNIX