chop
chop

Reputation: 471

Bash to refer to txt list to find data in directory and copy items

I have a text file with over 7000 lines of font names which are required by our design department. This list has been generated by software which has stripped out any file extensions. Some files indeed wouldn't have had extensions.

This "fontList.txt" needs reading by a script and reference a folder which will have many sub-directories with tens of thousands of font files within. The original files will have their file extensions where appropriate.

The challenge is that there are no file extensions in the source list but there are version differences of fonts, whereby the older Type 1 variant might be "Banana" and the new Open Type "Banana.otf". Both versions might be required but the source list will only refer to "Banana", so the script needs to find both.

Running this interactively on the command line on Mac OS 10.8, I need to modify the following to work:

sourceList="/Users/"$useris"/Desktop/fontList.txt"
sourceDir="/Users/Shared/DesignFonts"

cat "$sourceList" |
while read FONTPATH
do
    echo font from source list to find and copy is "${FONTPATH}" | tee -a "$logPath"
    find "$sourceDir" -type f -name "${FONTPATH}" -exec cp -vf {} /Users/"$useris"/Desktop/fontPot/ \;
done

The echo statement is purely for checking purposes.

Currently, the script works but will not reference font files in the sourceDir which have file extensions, like .otf .ttf.

To clarify, the sourceList looks like:

Marion
MetaNorCap
MetaNorCapExp
MinionPro-Bold

Whereas the sourceDir contents look like:

Marion.ttc
MesquiteStd.otf
MetaNorCap
MetaNorCapExp
Microsoft
Microsoft Sans Serif.ttf
MinionPro-Bold.otf

Note:
"Microsoft" above is a directory.
These are examples lists from much bigger A-Z lists.

Upvotes: 1

Views: 189

Answers (2)

Lri
Lri

Reputation: 27633

This would also match for example Helvetica.Neue.ttf even if the list only contained Helvetica. f2 is not needed if fontList.txt does not contain globbing characters ([]*?\).

cat ~/Desktop/fontList.txt | while IFS= read -r f; do f2=$(sed 's/[][*?\]/\\&/g' <<< "$f"); find /Users/Shared/DesignFonts -type f \( -name "$f2" -o -name "$f2.*" \) -exec cp {} ~/Desktop/fontPot \;; done

Or you can use globstar if you install bash 4:

shopt -s globstar extglob; cat ~/Desktop/fontList.txt | while IFS= read -r f; do cp /Users/Shared/DesignFonts/**/"$f"?(.*) ~/Desktop/fontPot; done

The glob expression also matches directories but cp should refuse to copy them. ?(.?|.??|.???|.????) would only match extensions that are 4 characters or shorter.

Upvotes: 2

choroba
choroba

Reputation: 242443

You can specify all the extensions:

... -exec cp -vf {} {}.otf {}.ttf /Users/"$useris"/Desktop/fontPot/ \;

Upvotes: 0

Related Questions