Copy png from each dir, if png is >600x600?
I have a tree of directories with png files in each.
From each directory would I like to copy those png 开发者_如何学运维files where their resolution is >(600x600).
Using the following to commands could I create a script that would do just that, but it would be fairly long.
This command will list png's where the horizonal number of pixels is >600.
for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then echo $f;fi;done
This command will find all dirs and copy file.png to them
find <basedir>/ -type d -exec cp file.png '{}' \;
Does someone know of a good way to solve this problem?
Update: This is what I got.
for f in $(find . -type f -name *png -print0); do
identify -format '%w %h' $f|awk '{print $1 $2}'
# if [$1 -gt 600 && $2 -gt 600]; then
cp $f ~/600x600
fi
done
How do I use $1
and $2
from awk
in the if
-statement?
I'm not really sure to understand the question, but i guess you want to copy file with size greater than 600x600 into a specific directory, then you can do something like :
for f in *.png;do if [[ `file $f | cut -f5 -d\ ` -gt 600 ]] ; then
echo $f;fi;done|nawk '{print "cp "$1" my_specific_directory"}' | bash
where my_specific_directory is the directory where you want to copy your pngs...
identify -format '%w %h' yourImage.png
UPDATE
Sorry, i had most of this typed out awhile ago, but was interupted.
I tested this, and it should work
for f in *png;
do
identify -format '%w %h' $f | awk -v f="$f" '{ if ($1 >= 600 && $2 >= 600) print "cp -piv "f" ~/DESTINATION_DIR"}' | bash
done
notice that you can do the comparison inside awk. If you have a desire to learn, try making this a one-liner. Sure, it will be long, but you should be able to do this entire operation with find. the problem for me is piping inside -exec, but I know its possible.
精彩评论