LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-07-2016, 09:01 AM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

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.

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

Quote:
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
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Man page documentation:
Code:
       -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
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
Code:
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.

Whereas,

Code:
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
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
For deleting files the -delete option/action is even faster.
Code:
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
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
...

Quote:

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:

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

Code:
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
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
Quote:
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.

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

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:57 AM.

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