Questions tagged [xargs]

xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input.

xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Under the Linux kernel before version 2.6.23, arbitrarily long lists of parameters could not be passed to a command, so xargs breaks the list of arguments into sublists small enough to be acceptable.

xargs often covers the same functionality as the backquote(`) feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a good companion for commands that output long lists of files like find, locate and grep, but only if you use -0, since xargs without -0 deals badly with file names containing ', " and space.

1303 questions
7
votes
2 answers

Why do I need parenthesis In bash `set -e` and negated return code

I have a shell script which checks for windows line endings. set -e (! git ls-files | xargs grep -I $'\r') I am using the ! character to negate the return code of the command. Grep will return code 0 when a file with carriage return is found,…
7
votes
4 answers

Execute bash function from find command

I have defined a function in bash, which checks if two files exists, compare if they are equal and delete one of them. function remodup { F=$1 G=${F/.mod/} if [ -f "$F" ] && [ -f "$G" ] then cmp --silent "$F" "$G" && rm "$F"…
7
votes
3 answers

xargs: exec command with prompt

I'm trying to do the following with xargs pacman -Q | grep xf86-video | awk '{print $1}' | xargs pacman -R to remove all xf86-video-* driver on my machine. To make the question more clear, here is the output of pacman -Q | grep xf86-video | awk…
helsinki
  • 743
  • 7
  • 14
7
votes
4 answers

xargs not working with built in shell functions

I am trying to speed up the processing of a database. I migrated towards xargs. But I'm seriously stuck. Piping a list of arguments to xargs does not work if the command invoked by xargs isn't a built in. I can't figure out why. Here is my…
Sgt.Doakes
  • 97
  • 2
  • 6
7
votes
2 answers

Using xargs with Special characters

I have the following problem. Got a file which includes certain paths/files of a FS. These for some reason do include the whole range of special characters, like space, single/double quotes, even sometimes the Copyright ASCII. I need to run each…
derchris
  • 285
  • 4
  • 11
7
votes
3 answers

How can I take STDIN and use it on the bash shell to set an environment variable?

I'm trying to get better at one-liners here, this is what I have currently. $ echo $PATH /home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games What I want to do is strip off that first chunk, so here's what I've…
Tom
  • 330
  • 4
  • 15
7
votes
5 answers

linux shell: How to read command argument from a file?

I have process id in a file "pid" I'd like to kill it. Something like: kill -9 I tried: kill -9 `more pid` but it does not work. I also tried xargs but can't get my head around it.
Igor Katkov
  • 6,290
  • 1
  • 16
  • 17
7
votes
1 answer

Pipe to export command

Why does export fail when used as the last step in a command pipeline? echo FOO=bar | xargs export # => xargs: export: No such file or directory I can rewrite it this way to accomplish what I want: export `echo FOO=bar` But why can't I use export…
pje
  • 21,801
  • 10
  • 54
  • 70
7
votes
4 answers

Run shell script for every file in directory

I have a bunch of files in a directory all named YYYY_MM_DD -rw-r--r-- 1 root root 480K Apr 21 13:17 2012_04_05 -rw-r--r-- 1 root root 483K Apr 21 13:17 2012_04_06 -rw-r--r-- 1 root root 484K Apr 21 13:17 2012_04_07 -rw-r--r-- 1 root root 480K Apr…
general exception
  • 4,202
  • 9
  • 54
  • 82
6
votes
5 answers

xargs: variable substitution after redirection

I'm trying to find all text files which have the encoding iso-8859-1 and convert these to UTF-8. My attempt so far is: find . -name '*.txt' | xargs grep 'iso-8859-1' | cut -d ':' -f1 | xargs iconv -f ISO-8859-1 -t UTF-8 {} > {}.converted The…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
6
votes
4 answers

Run 4 concurrent instances of a python script on a folder of data files

We have a folder with 50 datafiles (next-gen DNA sequences) that need to be converted by running a python script on each one. The script takes 5 hours per file and it is single threaded and is largely CPU bound (the CPU core runs at 99% with…
Jon Rhoades
  • 496
  • 4
  • 12
6
votes
6 answers

run the output of a script as a standalone bash command

suppose you have a perl script "foobar.pl" that prints the following to stdout date -R and you want to run whatever that perl script outputs as a standalone bash command (don't worry about security problems as this is running in a trusted…
dreftymac
  • 31,404
  • 26
  • 119
  • 182
6
votes
3 answers

subtle difference in using xargs and xargs -i

Why does find . -name "*.xml" | xargs grep FOO returns matchs with filenames, while find . -name "*.xml" | xargs -i -sh -c "grep FOO {}" doesn't?
shabunc
  • 23,119
  • 19
  • 77
  • 102
6
votes
3 answers

Running `bash` using docker exec with xargs command

I've been trying to execute bash on running docker container which has specific name as follows. --(1) docker ps | grep somename | awk '{print $1 " bash"}' | xargs -I'{}' docker exec -it '{}' but it didn't work and it shows a message like…
Abraxas5
  • 63
  • 1
  • 1
  • 6
6
votes
2 answers

How to smoothly rename a Phoenix Project?

I've cloned a git repo and I need to start a new project based off of it. How do I rename it, making sure everything is taken care of? I'm using macOS.
Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50