0

I have multiple files and want to add a character at the beginning of filename, add extension, and remove a word in the center of the filename at once using single command and I run this using bash. Here are my files:

DATA_20210301_INPUT_CHECK 
DATA_20210302_INPUT_CHECK
DATA_20210303_INPUT_CHECK
DATA_20210304_INPUT_CHECK

And want to rename them (in one operation) to :

1_DATA_20210301_CHECK.DAT
1_DATA_20210302_CHECK.DAT
1_DATA_20210303_CHECK.DAT
1_DATA_20210304_CHECK.DAT

How to change all the files name at once with single command? From what I do right know is do all this loop one by one

 for f in *; do mv "$f" "$f.DAT"; done
 for i in *; do mv "$i" 1_"$i"; done
 for i in *; do mv "${i}" "${i//\_INPUT/}"; done

Please help me, thank you!

2
  • ... so do mv "$f" "1_${f/_INPUT/}.DAT"; ? Commented Mar 23, 2021 at 0:32
  • Wah thats work perfectly, thats what I want thank you! @steeldriver
    – Shifa
    Commented Mar 23, 2021 at 0:38

2 Answers 2

0

The rename command can do that. I know of two versions: GNU and Perl Regular Expressions.

GNU:

rename DATA 1_DATA DATA*

Regular expression:

rename s/^/1_/ DATA*

Run rename --help to find out which version you have.

0

This is actually two commands but it gives the output that you want:

rename 'DATA' '1_DATA' DATA_2021030*_INPUT_CHECK && rename 'CHECK' 'CHECK.DAT' *DATA_2021030*_INPUT_CHECK

Output:

1_DATA_20210301_INPUT_CHECK.DAT
1_DATA_20210302_INPUT_CHECK.DAT
1_DATA_20210303_INPUT_CHECK.DAT
1_DATA_20210304_INPUT_CHECK.DAT

You must log in to answer this question.

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