Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to group jpeg images by odd number and even number. So I did like following:

$ mkdir odd
$ mkdir even
$ find . -regextype posix-egrep -regex ".*[13579].jpg$" -print0 | xargs -i -0 mv {} odd/
$ find . -regextype posix-egrep -regex ".*[02468].jpg$" -print0 | xargs -i -0 mv {} even/

It works fine. And I write same thing in a shell script.

mkdir odd
mkdir even
find . -regextype posix-egrep -regex ".*[13579].jpg$" -print0 | xargs -i -0 mv {} odd/
find . -regextype posix-egrep -regex ".*[02468].jpg$" -print0 | xargs -i -0 mv {} even/

But it returns following error:

xargs: Warning: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?

Why this script does not work? And how to fix it?

share|improve this question
Not sure what your current script is doing, but you can simplify the command and drop xargs entirely by doing the execution in find with the -exec option - so 'find . -regextype posix-egrep -regex ".*[13579].jpg$" -exec mv {} odd/ \;' would do you for the first line. – davidgo Aug 10 '13 at 8:17
I wonder if the "-i" is the cause of your problems - according to xargs its used for a replace function, and may be trying to replace a null character. This doesn't explain why you had it there, or why it works on the command line but not a script. I'd be curious to find out if removing the -i works for you. – davidgo Aug 10 '13 at 8:21
Thanks davidgo! It works with -exec option. But I does not work if I remove -i option in xargs. I added the -i option because I want to use {} expression in xargs argument. Is it not necessary? – ironsand Aug 11 '13 at 1:22

One possible source of this is that you may have two different versions of xargs with different option syntax, and because of differences in $PATH the script is using a different one than your interactive session. You can check this pretty easily by writing another script to see what version it finds:

echo "xargs is: $(which xargs)"
echo "PATH is: $PATH"

...see what the script prints, then run the same commands (or just which xargs) interactively and see if you get a different result. If they come out different, there's probably something in your shell initialization scripts (~/.bashrc, ~/.bash_profile, etc) that's causing it.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .