1

There is a set of programs in a source folder, but only the most recent version must be copied to the destination USB drive.

From Bash Script - Copy latest version of a file in a directory recursively, it shows that my formula would be:

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

So how to make find work inside a for loop on a set of masks?

set1="AdbeRdr\*.exe jre-\*.exe LibreOffice\*.msi"
for m in $set1
do
    echo "m: $m"
    f=$(find . -name $m | sort -n | tail -1)
    echo "f: $f"
    cp $f /media/USB
done

$m outputs the correct values (AdbeRdr*.exe, etc.), $f is empty and cp copies the whole parent directory. If I specify the mask explicitly without a variable (find . -name AdbeRdr\*.exe | sort -n | tail -1), the last file is outputted correctly.

Where am I going wrong? And how can I handle spaces if those would occur in filenames?

Thanks!

Community
  • 1
  • 1

1 Answers1

2

Use an array rather than a string to hold your elements, like this:

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

Use double-quotes around your variables to handle spaces in filenames.

dogbane
  • 266,786
  • 75
  • 396
  • 414