Wednesday, 15 June 2011

bash: Copy last version of a file from a mask list -



bash: Copy last version of a file from a mask list -

there set of programs in source folder, recent version must copied destination usb drive.

from bash script - re-create latest version of file in directory recursively, shows formula be:

f=$(find . -name adberdr\*.exe | sort -n | tail -1)

so how create find work within loop on set of masks?

set1="adberdr\*.exe jre-\*.exe libreoffice\*.msi" m in $set1 echo "m: $m" f=$(find . -name $m | sort -n | tail -1) echo "f: $f" cp $f /media/usb done

$m outputs right values (adberdr*.exe, etc.), $f empty , cp copies whole parent directory. if specify mask explicitly without variable (find . -name adberdr\*.exe | sort -n | tail -1), lastly file outputted correctly.

where going wrong? , how can handle spaces if occur in filenames?

thanks!

use array rather string hold elements, this:

set1=( 'adberdr*.exe' 'jre-*.exe' 'libreoffice*.msi' ) m in "${set1[@]}" echo "m: $m" f=$(find . -name "$m" | sort -n | tail -1) echo "f: $f" cp "$f" /media/usb done

use double-quotes around variables handle spaces in filenames.

bash list loops for-loop mask

No comments:

Post a Comment