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.

1
  • What distribution are you working on?
    – Kevdog777
    Commented Nov 29, 2019 at 14:33

3 Answers 3

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.

1
  • this is what i was looking for Thank you
    – Mohsin
    Commented Dec 3, 2019 at 8:09
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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .