0

I want to rename all the files in the directory at specific position.

Original File sample Names:

neif11_fastcredit_20190629101333.txt
neif11_fastcredit_20190629101334.txt
neif11_fastcredit_20190629101335.txt
neif11_fastcredit_20190629101336.txt
neif11_fastcredit_20190629101337.txt

I want to rename it like:

neif11_fastcredit_20191129061333.txt
neif11_fastcredit_20191129061334.txt
neif11_fastcredit_20191129061335.txt
neif11_fastcredit_20191129061336.txt
neif11_fastcredit_20191129061337.txt

File name Understanding:

neif11_fastcredit_2019 should remain as is the next part is month and date [MMDD]0629 which I want to update to 1129 as today's date and last part is HHMMSS which also remain unchanged.

Need help as I am new to Linux.

Kevdog777
  • 3,224
Mohsin
  • 103

3 Answers3

1

With Larry Wall's rename (rename (Debian, Ubuntu) or prename (RHEL/CentOS)):

rename -n s/_20190629/_20191129/ neif11_fastcredit_20190629*.txt

-n is just the "dry run" switch. Remove or replace with -v for actual use.

xenoid
  • 8,888
1

If you do not have rename available, you can try the following (Bash) loop which uses sed:

user@host$ for FILE in *.txt; do NEWNAME=$(sed 's/_20190629/_20191129/' <<< "$FILE"); mv "$FILE" "$NEWNAME"; done

Note that this requires Bash. If you have another shell, you will have to resort to something like

user@host$ for FILE in *.txt; do NEWNAME=$(echo "$FILE" | sed 's/_20190629/_20191129/'); mv "$FILE" "$NEWNAME"; done

Also note that this assumes the filenames are "reasonably well-behaved", so special characters (not part of your example) may cause this to fail.

AdminBee
  • 22,803
0

Using mv and a loop:

for f in neif11_fastcredit_20190629*.txt; do
  mv "$f" "neif11_fastcredit_201911${f##*_201906}"
done

The ${f##*_201906} part removes the longest matching prefix neif11_fastcredit_201906 from each filename and leaves the DDHHMMSS.txt part as the new filename's suffix.

Freddy
  • 25,565