Sponsored Content
Full Discussion: Understanding Xargs
Top Forums Shell Programming and Scripting Understanding Xargs Post 303010086 by scrutinizerix on Tuesday 26th of December 2017 08:56:37 AM
Old 12-26-2017
Understanding Xargs

I'm struggling to understand the man page entry about xargs in conjunction with the option -I. It states:

Quote:
Execute utility for each input line, replacing one or more occur-
rences of replstr in up to replacements (or 5 if no -R flag is
specified) arguments to utility with the entire line of input
And the description reads:

Quote:
The xargs utility reads space, tab, newline and end-of-file delimited
strings from the standard input and executes utility with the strings as
arguments.
Am I correct interpreting that as the fact that if no specified otherwise xargs -I can pass to a command following it no more than 5 arguments (a.k.a "strings") each of them containing unlimited number of occurrences of the data to be replaced using that command?

Furthermore what is a string in UNIX?

Example:
pass a list of highest level objects of current directory to some command

Code:
ls . | xargs some_command

Does ls return one string comprising multiple lines? How many arguments that passes for? Or is one line = 1 string? 1 argument?

Moderator's Comments:
Mod Comment Thread moved. Please post your questions in the appropriate sub-forum. Thank you.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

xargs

Can I use xargs to send a list of commands to a process, to be acted upon individually? Here's what I have: a file that contains numbers, one per line. The desired outcome it to send each number to a DB2 query. I thought xargs would work, but it doesn't. I tried it like this: cat file | xargs |... (4 Replies)
Discussion started by: jpprial
4 Replies

2. UNIX for Advanced & Expert Users

xargs -P

I discovered that GNU's xargs has a -P option to allow its processes to run in parallel. Great! Is this a GNU thing, or is it supported by other platforms as well? (4 Replies)
Discussion started by: otheus
4 Replies

3. Shell Programming and Scripting

Using xargs

hi i just want to know that how do we use xargs command to find files which are greater than specified memory in a given directory (6 Replies)
Discussion started by: sumit the cool
6 Replies

4. Shell Programming and Scripting

Help in using xargs

Hi, I have a requirement to RCP the files from remote server to local server. Also the RCP has to run in parallel. However using 'xargs' retrives 2 file names during each loop. How do we restrict to only one file name using xargs and loop till remaining files. I use the below code for... (2 Replies)
Discussion started by: senthil3d
2 Replies

5. Shell Programming and Scripting

Xargs and

Hello there, Let me show you a simple example of what I am trying to achieve: 1) I have an input text file with some lines: 1 a 2 b 3 c 2) And I want to run a command with these lines as arguments (+ arbitrary extra arguments). For example: $ command "1 a" "2 b" "3 c" "bye" I... (7 Replies)
Discussion started by: tokland
7 Replies

6. Shell Programming and Scripting

Help with xargs

hi Could any one please tell me the option using which we can run multiple commands using xargs I have list of files, I want to run dos2unix and chmod at one shot on them I tried google n searched man pages but couldnt really find the solution , please help right now im doing this ls... (4 Replies)
Discussion started by: sunilmenhdiratt
4 Replies

7. Shell Programming and Scripting

xargs

I have dir with many files ( close to 4M ) . $ ls -la total 76392 drwxr-xr-x 10 oracle dba 512 Jun 06 14:39 . drwxr-xr-x 11 oracle dba 512 Dec 20 13:21 .. drwxr-xr-x 2 oracle dba 39074816 Jun 15 14:07 ad I am trying to delete them using... (8 Replies)
Discussion started by: talashil
8 Replies

8. Shell Programming and Scripting

Help with xargs

Using the bash shell I'm trying to either create a command for the command line or a script that will show netstat info for a given process name. Here is an example of what I'm trying to do:$ ps aux |grep catalina |grep -v grep | awk '{print $2}' 5132 $ netstat -nlp |grep 5132 (Not all processes... (11 Replies)
Discussion started by: axiopisty
11 Replies

9. Shell Programming and Scripting

Xargs

Hello, I need some help with xargs $ ls aaa bbb ccc ddd$ ls | xargs -I{} ls -la {} -rw-rw-r--. 1 xxx xx 0 May 30 20:04 aaa -rw-rw-r--. 1 xxx xx 0 May 30 20:04 bbb -rw-rw-r--. 1 xxx xx 0 May 30 20:04 ccc -rw-rw-r--. 1 xxx xx 0 May 30 20:04 dddit's possible to have output like this with... (3 Replies)
Discussion started by: vikus
3 Replies

10. UNIX for Dummies Questions & Answers

Xargs

Hi, can anyone tell me in detail ? what the following do in detail ? I am trying to get a largest number in a list Thanks Tao LARGEST=$(echo $* | xargs -n1 | sort -nr | tail -1) (3 Replies)
Discussion started by: ccp
3 Replies
xargs(1)						      General Commands Manual							  xargs(1)

Name
       xargs - construct argument list and execute command

Syntax
       xargs [flags] [ command [initial-arguments] ]

Description
       The command combines fixed initial-arguments with arguments read from standard input to execute a specified command one or more times.  The
       number of arguments read when a command is invoked and how they are combined is determined by the options specified.

       The specified command, (which can be a Shell file) is searched for using ones' $PATH specification.  If command is not specified, /bin/echo
       is used.

       Arguments  read	from  standard	input  are defined as contiguous strings of characters delimited by one or more blanks, tabs, or newlines;
       empty lines are always discarded.  Blanks and tabs can be embedded as part of an argument if they contain an escape character  or  if  they
       are  quoted.   Characters enclosed in quotes (single or double) are taken literally, and the delimiting quotes are removed; a backslash ()
       escapes the next character.

Options
       Each argument list begins with the initial-arguments, followed by arguments read from standard input, with the exception of the -i  option.
       See the description of the -i option for more information.

       The  options -i, -l, and -n determine how arguments are selected when each command is invoked.  If none of these options are specified, the
       initial-arguments are followed by arguments read continuously from standard input until the internal buffer is full; then, command executes
       with  the accumulated arguments.  This process repeats until no arguments exist.  When conflicts arise, such as the -l option used with the
       -n, the last option has precedence. The options values are as follows:

       -lnumber
	     Execute command for each non-empty number lines of arguments from standard input.	When command is invoked for the final time, it has
	     fewer  lines  of  arguments if fewer than a specified number remain.  A line ends with the first newline unless the last character of
	     the line is a blank or a tab; a trailing blank or tab signals continuation through the next non-empty line.   If  number  is  is  not
	     specified, the value 1 is assumed.  The option -x is forced.

       -ireplstr (Insert mode)
	     Execute  command for each line from standard input, taking the entire line as a single argument and inserting it in initial-arguments
	     for each occurrence of replstr.  A maximum of five arguments specified in initial-arguments can contain one  or  more  occurrence	of
	     replstr.	Blanks and tabs at the beginning of each line are discarded.  A constructed arguments cannot exceed 255 characters and the
	     option -x is a forced.  A {|} is assumed for replstr if not specified.

       -nnumber
	     Execute command using as many standard input arguments as possible, up to the specified number arguments  maximum.   Fewer  arguments
	     are used if their total size is greater than size characters, and when the last command is invoked, fewer number of arguments remain.
	     If the option -x is also include, each specified number of arguments must fit in the size limitation, or else xargs terminates execu-
	     tion.

       -t (Trace mode)
	     Echo the command and each constructed argument list to file descriptor 2 prior to their execution.

       -p (Prompt mode)
	     Asks  the user whether or not command should be executed each time command is invoked. Trace mode (-t) is turned on to print the com-
	     mand instance to be executed, followed by a ?... prompt.  A reply of y executes the command; any other response does not invoke  that
	     particular command.

       -x    Causes  the  command xargs to terminate if an argument list is greater than the specified size of characters; the option -x is forced
	     by the options -i and -l.	When the options -i, -l, or -n are included, the total length of all arguments must be within  the  speci-
	     fied size limit.

       -ssize
	     The maximum size of each argument list is set to size characters; size must be a positive integer less than or equal to 470. If -s is
	     not included, 470 is the default.	Note that the character count for size includes one extra character  for  each	argument  and  the
	     count of characters in the command name.

       -eeofstr
	     The  option  eofstr  is  taken  as the logical end-of-file string.  Underscore (_) is assumed for the logical EOF string if -e is not
	     specified.  The value -e without eofstr specified turns off the logical EOF string capability; the  underscore  is  taken	literally.
	     The command xargs reads standard input until either end-of-file or the logical EOF string is encountered.

       The command xargs terminates if it receives a return code of -1 from command or if it cannot execute command.  When command is a Shell pro-
       gram, it should explicitly exit with an appropriate value to avoid returning with -1.  See for more information.

Examples
       The following example moves all files from directory $1 to directory $2 and echoes the move command prior to executing it:

       ls $1 | xargs -i -t mv $1/{} $2/{}

       The following example combines the output of the parenthesized commands onto one line, which is then echoed to the end of file log:

       (logname; date; echo $0 $*) | xargs >>log

       In the next example, the user is prompted to specify which files in the current directory are to be archived.  The first  example  archives
       the files one at a time; the second example archives groups of files:

	 ls | xargs -p -l ar r arch

	 ls | xargs -p -l | xargs ar r arch

       The following example executes diff(1) with successive pairs of arguments originally typed as Shell arguments:

       echo $* | xargs -n2 diff

See Also
       sh(1).

																	  xargs(1)
All times are GMT -4. The time now is 08:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy