M2bandit
M2bandit

Reputation: 99

How do you replace a file name prefix in bulk with a bash script?

What I've managed to do so far is append it.

#!/bin/bash
for f in *
do 
mv "$f" "File${f##/}"
done

File names consist of 3 random letters followed by 3 numbers. I need to replace the 3 letters with "File".

Upvotes: 0

Views: 78

Answers (3)

M2bandit
M2bandit

Reputation: 99

Got it

#!/bin/bash
for f in *
do 
newName=File"$(echo "$f" | cut -c4-)"
mv "$f" "$newName"
done

Apparently I'm not supposed to comment to say thank you so I'll just add it here. Thank you, I'll use Ignacio's method.

Upvotes: 0

Klemen
Klemen

Reputation: 69

You can use:

newName=`echo $f | sed 's/^[a-z]\{3\}/File/'`
mv $f $newName

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Then you need to strip the three letters.

"File${f#???}"

Upvotes: 2

Related Questions