I have several files like Apple-AP01, Apple-AP02, Banana-AP05, Chocolate-RS33 and others in my home directory that are pulled from an ftp server. In the same home directory there are Folders like Fruit, Sweet, etc. And within these folders, there are subfolders with names like Apple-AP01, Apple-AP02, Chocolate-RS33, etc.
I need to run a script on my home directory such that once I have Apple-AP01, it knows to place it into the Fruit folder based on the "AP" keyword and then further place that into the Apple-AP01 folder. and for Chocolate-RS33, it needs to go into the Sweet folder based on the "RS" keyword and then further into the Chocolate-RS33 subfolder within the Sweet folder. I need this for all of my files. Can someone put a bash script that would work?
I've tried
for f in *.
do
name=`echo "$f"|sed 's/ -.*//'`
letter=`echo "$name"|cut -c1`
dir="DestinationDirectory/$letter/$name"
mkdir -p "$dir"
mv "$f" "$dir"
done
I think I need to use a for loop, but I’m not sure how to do it in bash.
echo "$f"|sed 's/ -.*//'letter=echo "$name"|cut -c1dir="DestinationDirectory/$letter/$name" mkdir -p "$dir" mv "$f" "$dir" donecutnorsedknow that apples are fruits or chocolates are sweets, so you need a mechanism to relate the files to the directories.