0

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?

1
  • Please show us your own attempt, and clarify what didn't work. For more info about asking a good question, please see our tour and How to Ask. Commented Oct 11, 2021 at 10:25

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

That works like charm, thanks! How would I modify the above to run from a different path to where the files are, or run this across multiple directories please?
You can do ((i++)) which is simpler
@CMR H : If you want to run for different directory simply may it as a generic function and pass the path at run time to the script which ceving as provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.