user6784934
user6784934

Reputation: 1

Script to Rename files with a Date suffix to a common filename

I have a requirement where I need to rename a couple of filenames to a fixed filename every week.

To explain better, each week I receive 3 files in the format :

File_ABC_20160822.TXT.pgp
File_DEF_20160822.TXT.pgp
File_GHI_20160822.TXT.pgp

I need to run a small script to rename the files to :

File_ABC.dat
File_DEF.dat
File_GHI.dat

without deleting the original files, since we have to keep a log of delivered files.

The issue is that since the files come with a new date suffix each week, the script would have to pick current system date in YYYYMMDD format.

Upvotes: 0

Views: 278

Answers (1)

heemayl
heemayl

Reputation: 41987

You can loop over the files names use bash parameter expansion to get the desired name while renaming:

for f in File_*_*.TXT.pgp; do echo cp -i "$f" "${f%_*}.dat"; done

The above will do a Dry-run, for actual action, remove echo:

for f in File_*_*.TXT.pgp; do cp -i "$f" "${f%_*}.dat"; done

Upvotes: 1

Related Questions