LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Use of {} in find command (http://www.linuxquestions.org/questions/linux-newbie-8/use-of-%7B%7D-in-find-command-4175427345/)

shivaa 09-15-2012 12:51 AM

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!

holdencaulfield 09-15-2012 01:42 AM

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 \;

shivaa 09-15-2012 10:31 PM

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?

pixellany 09-15-2012 10:47 PM

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."

David the H. 09-16-2012 10:14 AM

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

shivaa 09-18-2012 11:48 PM

Thanks.
 
Quote:

Originally Posted by pixellany (Post 4781364)
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.

shivaa 09-19-2012 11:07 AM

Thanks a lot. Answers are really helpful.


All times are GMT -5. The time now is 07:11 PM.