I have downloaded multiple files from different s3 buckets to a single folder on a linux vm. I want to rename only the *.gz
files in the directory, preferably sequentially, using a script to something like FILE001_$(date '+%Y%m%d').csv.gz
,FILE002_$(date '+%Y%m%d').csv.gz
etc, before moving them to client's sftp server(existing process).
I tried using find . -name "*.csv.gz" -exec mv {} FILE%03d_$(date '+%Y%m%d').csv.gz
from another post but get missing argument to -exec
error. Any examples using rename
or mv
for the above task please?
Try something like this:
#! /bin/bash
declare -i i=0
for f in *.csv.gz; do
: $((i++))
mv "$f" "$(printf "FILE%03d_%s.csv.gz" $i "$(date +%Y%m%d)")"
done
You can pass the names of the files into the script. In that case you have to use dirname
to get the name of the directory of each file.
#! /bin/bash
declare -i i=0
for f in "$@"; do
: $((i++))
mv "$f" "$(dirname "$f")"/"$(printf "FILE%03d_%s.csv.gz" $i "$(date +%Y%m%d)")"
done
Example:
$ mkdir {1,2,3}
$ touch {1,2,3}/{a,b,c}.csv.gz
$ ./rename.sh {1,2,3}/*.csv.gz
$ ls {1,2,3}
1:
FILE001_20211012.csv.gz FILE002_20211012.csv.gz FILE003_20211012.csv.gz
2:
FILE004_20211012.csv.gz FILE005_20211012.csv.gz FILE006_20211012.csv.gz
3:
FILE007_20211012.csv.gz FILE008_20211012.csv.gz FILE009_20211012.csv.gz