Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

#!/bin/bash
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
1  
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? –  Tetsu Aug 11 '13 at 1:22

1 Answer 1

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:

#!/bin/bash
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.