Contents
Are there any solutions for xargs with multiple arguments?
None of the solutions given so far deals correctly with file names containing space. Some even fail if the file names contain ‘ or “. If your input files are generated by users, you should be prepared for surprising file names. GNU Parallel deals nicely with these file names and gives you (at least) 3 different solutions.
How to control and use command line arguments?
You can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command: $ echo 1 2 3 4 | xargs -n 2. Find all .bak files in or below the current directory and delete them.
Which is better, xargs or backquote in shell?
xargs is more safer and easy to use. xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with blanks or special characters in file names easily. It is often used with find, grep and other commands.
How to use more than two arguments per command?
For example following will use first two argument per command: Find all .bak files in or below the current directory and delete them. {} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time.
What does grep only the first match mean?
-m 1 means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed. You can pipe grep result to head in conjunction with stdbuf.
When to use stdbuf to stop grep after nth match?
Note, that in order to ensure stopping after Nth match, you need to using stdbuf to make sure grep don’t buffer its output: As soon as head consumes 1 line, it terminated and grep will receive SIGPIPE because it still output something to pipe while head was gone. This assumed that no file names contain newline.
How to find first match with extended regexp?
Reading the grep manual (man grep) this is the minimum command to find first match with Extended regexp. Example getting the ethernet name that in my laptop is NOT eth0 !