Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-07-2016, 09:01 AM   #1
Registered: Oct 2005
Posts: 319

Rep: Reputation: 32
correct why in deleting files using find

I just came across to a site and read that it is not advisable to use the command below.

find / -name XXXX -exec rm {} \;
Instead, it should be

find / -name XXXX -exec rm {} +
I've been using rm {} \; for years but I am curious and maybe I am doing it wrong.

Is there an explanation what + means in exec?
Old 03-07-2016, 09:21 AM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Man page documentation:
       -exec command {} +
              This  variant  of the -exec action runs the specified command on
              the selected files, but the command line is built  by  appending
              each  selected file name at the end; the total number of invoca‐
              tions of the command will  be  much  less  than  the  number  of
              matched  files.   The command line is built in much the same way
              that xargs builds its command lines.  Only one instance of  `{}'
              is  allowed  within the command.  The command is executed in the
              starting directory.  If find encounters an error, this can some‐
              times  cause an immediate exit, so some pending commands may not
              be run at all.
The actual better choice might be the "-execdir" option.

Last edited by jpollard; 03-07-2016 at 09:26 AM.
Old 03-07-2016, 09:24 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
When in doubt, run to the man pages. Try searching for :- -exec command {} +
Old 03-07-2016, 02:50 PM   #4
Phantom Of The Linux
LQ Newbie
Registered: Mar 2016
Posts: 3

Rep: Reputation: 1
find / -name XXXX -exec rm {} \;
This will execute the rm command on each instance of the file and/or directory found in the search criteria.


find / -name XXXX -exec rm {} +
Will collect a list of filenames and/or directories up to ARG_MAX and then execute the rm command.

IMO, much better and doesn't waste system resources by calling rm each time it finds a match.

Last edited by Phantom Of The Linux; 03-07-2016 at 03:36 PM.
Old 03-08-2016, 12:28 PM   #5
Registered: Dec 2011
Location: Simplicity
Posts: 349

Rep: Reputation: 163Reputation: 163
For deleting files the -delete option/action is even faster.
find / -type f -name XXXX -delete
Warning: if not 100% sure what it will do, run -print or -ls first, then -delete!
Old 03-08-2016, 01:01 PM   #6
Senior Member
Registered: Sep 2013
Distribution: Void Linux / Slackware 14.2
Posts: 4,124

Rep: Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719


For a machine such as a macbook you won't find much difference in performance
between the two commands. However, if you look at the -exec version you can see a
subtle difference:

sudo find / -iname ".file-to-delete"  -exec rm {} \;

This means that you will find all those files with name ".file-to-delete". However
this search might return some unwanted false positives. When doing something with
sudo you should be a bit more careful. The advantage of using -exec rm {} is that
you can pass arguments to rm like this:
sudo find / -iname "*~"  -exec rm -i {} \;
In this example I want to remove those backup files that emacs makes. However that
tilde could be in some obscure file that I don't know about and could be
important. Plus I want to confirm the delete. So I put the option '-i' on the rm
command. This will give me an interactive delete.

Also you can refine the usage of rm to delete directories as well as files:

find /usr/local/share/ -iname "useless" -exec rm -r {} \;
In brief, the -exec gives you a bit more control over the actual command that
removes the found item. The advantage is that you use one tool to find the files,
another tool to remove them. Also not every version of the find utility has the
-delete option. So better to use each tool for its proper job. This is the unix
philosophy - one tool, one job, use them together to do what you need to do.

Last edited by BW-userx; 03-08-2016 at 01:04 PM.
Old 03-08-2016, 02:22 PM   #7
Registered: Dec 2011
Location: Simplicity
Posts: 349

Rep: Reputation: 163Reputation: 163
Unix philosophy - yes.
GNU philosophy - no.
For example, look at GNU tar's compress options!

Last edited by MadeInGermany; 03-08-2016 at 02:45 PM.
Old 03-08-2016, 02:37 PM   #8
Phantom Of The Linux
LQ Newbie
Registered: Mar 2016
Posts: 3

Rep: Reputation: 1
Originally Posted by packets View Post
Is there an explanation what + means in exec?
I thought the OP wanted an explanation of what the + does in the find syntax.

I guess he wanted to know how to use find to delete files as well.

Anyway, this is how I use find to delete files.

find <path> -iname "pattern" -exec rm '{}' '+'
The single quotes around the brackets will include files/directories with spaces.

add -type f if you only want files and no directories.

BTW, here's a tip. Before deleting many files and directories with find, do a dry run first. Like so

find <path> -iname "pattern" '{}' '+'
See what files/directories will be deleted beforehand.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using find and deleting a lot of data boblc123 Linux - Enterprise 4 05-15-2014 10:42 AM
Trying to find what the error is and how to correct 1969JCW Linux - Newbie 2 02-13-2014 10:45 PM
Cannot find correct Qt headers! drussell6 Linux - Software 2 07-03-2008 07:38 AM
Deleting Files Older Than 2 Hours using Find? LinuxGeek Linux - Software 1 06-29-2005 06:10 AM
how to find correct tty number? SeanatIL Linux - Hardware 1 07-07-2004 07:03 PM

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration