0

I have a directory with several files named as follow:

London_qle_merged Paris_qle_merged Tokyo_qle_merged London_qle_obs Paris_qle_merged Tokyo_qle_merged 

How can I rename the file having *merged in the filename as follow:

London_Qle_merged Paris_Qle_merged Tokyo_Qle_merged 

I know that I could do a loop as follow:

for name in Paris London Tokyo do; mv ${name}_qle_merged ${name}_Qle_merged done 

but is there no other way?

4 Answers 4

2

Sure. In ksh/Bash/zsh:

for f in *_merged; do mv -- "$f" "${f/_qle_/_Qle_}" done 

or in standard shell.

for f in *_qle_merged; do mv -- "$f" "${f%_qle_merged}_Qle_merged" done 

See also, e.g. Parameter Expansion in BashGuide

0

I have done this by combination of awk and tr

Command

find . -type f -iname "*merged" |tr -d "./" | awk -F "_" '{print "mv" " " $1"_"$2"_"$3 " " $1"_"toupper(substr($2,1,1))substr($2,2)"_"$3 }' | sh 

Below merged files

London_qle_merged Paris_qle_merged Tokyo_qle_merged 

renamed to

London_Qle_merged Paris_Qle_merged Tokyo_Qle_merged 
0

This will do the job with no script loops, spawning just a single sub-process:

rename 's/qle/Qle/' *_merged 
0

The rename command is your friend here. It is sed for file names. (ensure that you have the Larry Wall version, there is at least two programs with this name).

The following answer is for file content, and thus uses sed. I have used it and re-purposed it to rename. https://unix.stackexchange.com/a/211579/4778

rename -e 's/_(.)/_\u$1/' *_merged 

It up cases the letter immediately following the first _.

A simpler solution for this specific input

rename 's/qle/Qle/' *_merged 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.