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! |
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 \; |
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? |
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." |
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 '{}' '+' 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 |
Thanks.
Quote:
|
Thanks a lot. Answers are really helpful.
|
All times are GMT -5. The time now is 09:32 AM. |