سؤال

At work, I need to upload images to a website. They cannot be larger than 300 KB. In order to group the images that are ready to be uploaded, I devised the following line in Bash:

du -h * | grep "[0-2]..K" | awk '{print $2}' | xargs mv Ready/

This did not work, however, because the shell returned the following:

usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

Finally, I resorted to a for-loop to accomplish the same:

for file in $(du -h * | grep "[0-2]..K" | awk '{print $2}')
do
    mv -v ${file} Ready/
done

Can somebody explain why the first line doesn't work? It is probably something very simple I'm missing, but I can't seem to find it.

I'm on Mac OS X 10.7, Bash version 4.3.

هل كانت مفيدة؟

المحلول

I would use the find command to get all files smaller than a certain size, makes the code a lot cleaner and easier to read like so:

find . -size -300k -name *.png -exec mv {} Ready/ \;

The reason your first command fails is because you have to reference the value you are piping in since it is not at the end of the statement. This should work:

du -h * | grep "[0-2]..K" | awk '{print $2}' | xargs -0 -I {} mv {} Ready/
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top