About Linux FAQ

Browse More FAQs:

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

Posted by Vivek Gite [Last updated: December 19, 2006]

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:

Discussion on This FAQ

  1. Carl Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

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

  6. Atul Says:

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

  7. Atul Says:

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

  8. Nick Says:

    Why not use the -delete option of find?

  9. Dr Thangpa Serto Says:

    [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

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.