Issue
There is a folder in which the application generates different files, such as those below.
Delhi_20221234556.csv Melbourne_20123456789.csv Goa_1234567890.csv Sydney_2022345689.csv
The script should removes numbers from files as below, then copies them to Azure blob storage, and after copying them to Azure blob, it should move the file to the archive folder with the date.
Delhi.csv Melbourne.csv Goa.csv Sydney.csv
Initially I was doing it for Goa_1234567890.csv as below.
#!/bin/bash
find /abc/def -type f -name 'Goa*' -execdir bash -c 'for f in "$@";
do mv "$f" "${f%%_*}.csv";
done' bash {} +
./azcopy cp "/abc/def/Goa.csv" "https://<Blobaccount>.blob.core.windows.net/abc\home\xyz?<SAS- Token>"
mv /abc/def/Goa.csv /abc/def/Archive/Goa$(date +%F).csv`
Would anyone be able to help me improve the above script to achieve this?
Solution
You may want to generalize your script to handle multiple filenames
other than Goa*
. Assuming the csv files are located just under the
/abc/def
directory (not in the subdirectories), would you please try:
#!/bin/bash
dir="/abc/def"
mkdir -p "$dir"/Archive/
for f in "$dir/"*.csv; do
if [[ -f $f ]]; then
new="${f%%_*}.csv"
mv -- "$f" "$new"
./azcopy cp "$new" "https://<Blobaccount>.blob.core.windows.net/abc\home\xyz?<SAS- Token>"
base="${new##*/}"
mv -- "$new" "$dir/Archive/${base%.csv}$(date +%F).csv"
fi
done
Please note it doesn't consider the case there are multiple filenames which share the same city name with different numbers.
Answered By - tshiono Answer Checked By - Gilberto Lyons (WPSolving Admin)