LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-15-2012, 12:51 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Use of {} in find command


Hello Friends,
While using find command, we put a {} followed by \;
So what's use of curly braces i.e. {} in find command?
For example, $ find . -name "foo" -exec ls -la {} \;
Thanks in advance!
 
Old 09-15-2012, 01:42 AM   #2
holdencaulfield
LQ Newbie
 
Registered: Jan 2012
Distribution: CRUX, Gentoo, LFS, OpenBSD
Posts: 16

Rep: Reputation: Disabled
The curly braces in the find command, basically contain whatever was found by find. For instance if you wanted to copy all files and move them someplace else:

Code:
find . -type f -print -exec cp {} /path/to/location \;
 
Old 09-15-2012, 10:31 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
holdencaulfield, Thanks for your answer.
I have read somewhere that it's work is "record seperator", somewhat like command "xargs" do. Can u please elaborate this little more?
 
Old 09-15-2012, 10:47 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
It's not a "record separator" (nor is xargs)---a record separator (AKA field separator) is something used to mark the boundaries between pieces of data.

{} is simply the syntax for the "exec" feature in the find command. As suggested by holden*, it says: "take what was found by find and put it here."
 
Old 09-16-2012, 10:14 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
It's really more like a variable. it "contains" the current file being processed by the -exec command. xargs uses pretty much the same syntax, although with xargs you can redefine it to be any character string (using the -I option) if you want.

Speaking of which, -exec has a second command-ending style, which gives it xargs-style processing ability. With the usual ";" ending, find executes the command once for each file found. If you match 20 files, you get 20 executions. But if you replace it with "+", it will gather multiple files together (as many as the system will allow) and run the command only once for the whole list.

Code:
find . -type f -exec mv -t targetdir '{}' '+'
This can often mean a great increase in speed and efficiency, but it comes with a couple of limitations. Since the '{}' brackets now represent a list of multiple files, they can now only come at the end of the command, just in front of the '+'. And the command you use must also be able to handle multiple file arguments in that fashion. e.g. mv could not be used with it's usual syntax, since the last file in the list would be treated as the target directory. That's one reason why gnu mv has added the -t option to the command.


PS: Incidentally, the "safe" record separator in both find and xargs is the null character, and that's generally used for safely transferring the list from one command to another through pipes. In find you use the -print0 option (and relatives) for null-separated output, and xargs will accept them as input delimiters with -0.

The majority of other gnu core commands also have some form of null separator option, but it varies by command, so check the man pages.

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020

Last edited by David the H.; 09-16-2012 at 10:22 AM. Reason: addendum
 
2 members found this post helpful.
Old 09-18-2012, 11:48 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks.

Quote:
Originally Posted by pixellany View Post
It's not a "record separator" (nor is xargs)---a record separator (AKA field separator) is something used to mark the boundaries between pieces of data.

{} is simply the syntax for the "exec" feature in the find command. As suggested by holden*, it says: "take what was found by find and put it here."
Thanks. It was really helpful.
 
Old 09-19-2012, 11:07 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks a lot. Answers are really helpful.
 
  


Reply

Tags
find


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
need help with find command and rar/unrar command line krakatoa1 *BSD 6 09-11-2012 04:21 PM
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 09:47 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 05:45 PM
can't find my find command! how to replace? dave247 Debian 4 11-19-2008 11:51 AM


All times are GMT -5. The time now is 10:27 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration