Linux or Unix find and remove files with one find command on fly
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
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Finding files in Linux filesystems
- Find files that do not have any owners or do not belong to any user under Linux/UNIX
- Locate files on linux, FreeBSD and UNIX system
- UNIX Find A File Command
- Linux: How can I find a file on my system?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


March 8th, 2007 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
March 29th, 2007 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?
March 30th, 2007 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 {} \;September 12th, 2007 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?
September 12th, 2007 at 4:53 am
A backslash is required, otherwise shell will treat ;? as part of shell command.
February 20th, 2008 at 3:33 pm
for removing directories in same manner, use command find . -type d -name “DIRNAME” -exec rm -rf {} \;
February 20th, 2008 at 3:34 pm
to recursively search and delete directories use
find . -type d -name “DIRNAME” -exec rm -rf {} \;
March 25th, 2008 at 6:33 pm
Why not use the -delete option of find?
April 15th, 2008 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