1

This question already has an answer here:

I have multiple folders and each one have around 175 files in it. file names are like

1.wav
2.wav
3.wav
......
175.wav 

I have to rename them as

A1.wav
A2.wav
A3.wav
......
A175.wav 

In other words I have to append Letters in previous file names.

I am wondering if there is a easy way to do this.

Ubuntu Version is 16.10

marked as duplicate by muru command-line Jun 7 '17 at 1:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

There are many ways, my own choice would be a 'for' loop:

for f in *.wav ; do mv "$f" "A$f" ; done

This is simple and easily modified for other needs...

3

User prename command:

$ prename -nv 's/^(.*)$/A$1/' *.wav                    
1.wav renamed as A1.wav
2.wav renamed as A2.wav
3.wav renamed as A3.wav

The way this reads is simple:

  • *.wav allows the shell to expand the wild card to list of all files that end with .wav. When the shell runs the full command, the computer will see prename -nv 's/^(.*)$/A$1/' 1.wav 2.wav 3.wav and so on as the actual command.
  • The 's/^(.*)$/A$1/' is actually is s/PATTERN/REPLACEMENT regular expression with grouping (.*), which allows us to group the whole name of the file from beginning ^ to end $ and refer to it as $1.

Note that -nv switches are for verbose -v and dry-run -n. If you're happy with the test run, remove -n to apply actual renaming.

  • What if I have to rename it as 1A.wav and 2A.wav etc. I tried with. " prename -nv 's/^(.*)$/$1A/' *.wav " it renames like it 1.wavA and 2.wavA – Adnan Ali Jun 7 '17 at 16:51
  • @AdnanAli Yes, because ^(.*) makes the whole name to be treated as $1. So you're literally sticking A at the end of whole name. What you want to do is use extra grouping, make $1 be everything before .wav and make .wav be $2. Like this: prename -nv 's/^(.*)(.wav)$/$1A$2/' *.wav So here first (.*) will be $1 and is everything before .wav. The second group (.wav)is $2 – Sergiy Kolodyazhnyy Jun 7 '17 at 17:06
2

Using rename:

rename 's/([0-9]+).wav/A\1.wav/' *.wav
  • s/SEARCH-FOR/REPLACE-WITH/ within-this-files
  • ([0-9]+) holds the number section then we can use it again using \1.
  • A\1.wav: A + (numberic section) + .wav

Not the answer you're looking for? Browse other questions tagged or ask your own question.