LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Don't understand the meaning of a pair of braces in xargs command line (https://www.linuxquestions.org/questions/linux-general-1/dont-understand-the-meaning-of-a-pair-of-braces-in-xargs-command-line-4175487914/)

sisrnb 12-14-2013 02:32 AM

Don't understand the meaning of a pair of braces in xargs command line
 
I did 'man xargs', and saw '-I{}' in statement for '-i' OPTION.
The question is: what does '{}' in '-I{}' stand for?
Thanks in advance!

druuna 12-14-2013 02:45 AM

The '{}' is replaced by that what is given to xargs (same principle as with the find command).

Have a look here: Linux / Unix Command: xargs (specifically the -replace[=replace-str], -i[replace-str] part)

sisrnb 12-14-2013 07:38 AM

Quote:

Originally Posted by druuna (Post 5080498)
The '{}' is replaced by that what is given to xargs (same principle as with the find command).

Have a look here: Linux / Unix Command: xargs (specifically the -replace[=replace-str], -i[replace-str] part)

Thanks for the replay!
The following statement from the link is key to me:
Code:

If replace-str is omitted, it defaults to "{}"
So, the '{}' is not of any syntax, it is just a string to indicate what to be replaced, right?

druuna 12-14-2013 08:02 AM

Quote:

Originally Posted by sisrnb (Post 5080573)
Code:

If replace-str is omitted, it defaults to "{}"
So, the '{}' is not of any syntax, it is just a string to indicate what to be replaced, right?

It depends on what you want/need to do.

Have a look at this example:
Code:

$ ls -l test*
-rw-r----- 1 druuna druuna 14 dec 14 14:54 test 01
-rw-r----- 1 druuna druuna 14 dec 14 14:55 test02

$ ls test\ 01 | xargs grep "two"
grep: test: No such file or directory
grep: 01: No such file or directory

$ ls test\ 01 | xargs -i grep "two" {}
two

$ ls test02 | xargs grep "two"
two

Or this:
Code:

$ find . -type d -print | xargs echo Directories:
Directories: . ./New ./Temp

$ find . -type d -print | xargs -I {} echo Directories: {}
Directories: .
Directories: ./New
Directories: ./Temp


sisrnb 12-14-2013 08:03 PM

Quote:

Originally Posted by druuna (Post 5080578)
It depends on what you want/need to do.

Have a look at this example:
Code:

$ ls -l test*
-rw-r----- 1 druuna druuna 14 dec 14 14:54 test 01
-rw-r----- 1 druuna druuna 14 dec 14 14:55 test02

$ ls test\ 01 | xargs grep "two"
grep: test: No such file or directory
grep: 01: No such file or directory

$ ls test\ 01 | xargs -i grep "two" {}
two

$ ls test02 | xargs grep "two"
two

Or this:
Code:

$ find . -type d -print | xargs echo Directories:
Directories: . ./New ./Temp

$ find . -type d -print | xargs -I {} echo Directories: {}
Directories: .
Directories: ./New
Directories: ./Temp


Thanks again for the reply!

I'm sorry I forgot to say I'm using debian wheezy 7.2, maybe different distributions/versions have different contents of 'man xargs'.

According to the following statement in 'man xargs':
Code:

      --replace[=replace-str]
      -i[replace-str]
              This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise.  This option is deprecated; use -I instead.

'-i' equals to '-I{}', thus I think the following command from the first example:
Code:

ls test\ 01 | xargs -i grep "two" {}
is equals to:
Code:

ls test\ 01 | xargs -I {} grep "two" {}
So I think the usages of '{}' from the following two commands from the two examples are of the same way:
Code:

ls test\ 01 | xargs -i grep "two" {}
find . -type d -print | xargs -I {} echo Directories: {}

So, I still think the '{}' is just a string/identifier to be replaced in a xargs command line.

Expect you reply again. :)

druuna 12-15-2013 02:21 AM

I think we are on the same page (and used the same Distro in this case; Debian).

The reason I gave the examples was due to this:
Quote:

Originally Posted by sisrnb
So, the '{}' is not of any syntax

Which I interpreted as "Doesn't need to be used". As the examples show it depends on the situation if they are actually needed.
This is a better description:
Quote:

Originally Posted by sisrnb
the '{}' is just a string/identifier to be replaced in a xargs command line

BTW: -i and -I (capital i) do the same thing, -I is being preferred as -i is deprecated and will/might be gone in the future versions of xargs.

sisrnb 12-18-2013 08:17 AM

Quote:

Originally Posted by druuna (Post 5080912)
I think we are on the same page (and used the same Distro in this case; Debian).

The reason I gave the examples was due to this:Which I interpreted as "Doesn't need to be used". As the examples show it depends on the situation if they are actually needed.
This is a better description:

BTW: -i and -I (capital i) do the same thing, -I is being preferred as -i is deprecated and will/might be gone in the future versions of xargs.

Thanks a log for helping me! I'm clear now.
And sorry for my bad description/english.:)

chrism01 12-23-2013 04:36 AM

You may find this list of xargs examples handy/educational; I did :)
http://javarevisited.blogspot.sg/201...inux-unix.html


All times are GMT -5. The time now is 06:34 AM.