1

In a directory I have large number of randomly named '.wav' files. For example

090913yui.wav
jiyafa123.wav
.
.
.
awe-6rt.wav

I want to change the file names to

file1.wav
file2.wav
.
.

and so on

Can anyone help me with how to do the same? I have only terminal access to the server. So need to do it through some command only.

Turing101
  • 153

3 Answers3

1

Using Perl's rename (usable in any OS and any shells, a must have in the toolchest):

rename -n 's/.*/"file" . ++$::c . ".wav"/se' ./*.wav

gives:

rename(090913yui.wav, file1.wav)
rename(jiyafa123.wav, file2.wav)

Or with zero padding:

rename -n 's/.*/"file" . sprintf("%.3d", ++$::c) . ".wav"/se' ./*.wav

gives:

rename(./090913yui.wav, file001.wav)
rename(./jiyafa123.wav, file002.wav)

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

  • In Alpine Linux, there is a package perl-file-rename, but it is in the testing repository of edge branch and isn't installed by default. Furthermore, mksh has rename as a builtin. Quoting man mksh: rename [--] from to (defer always, needs rename(2)) Renames the file from to to. Both pathnames must be on the same device. Intended for emergency situations (where /bin/mv becomes unusable); thin syscall wrapper. – Vilinkameni Sep 26 '23 at 21:17
  • I provided this link: https://unix.stackexchange.com/questions/730894/what-are-the-different-versions-of-the-rename-command-how-do-i-use-the-perl-ver/730895#730895 that goes in depth explaining the use of Perl's rename and how to install it everywhere. – Gilles Quénot Sep 26 '23 at 21:19
  • If you use mksh (you are =~ 0.00001% in the world), then use the full path of Perl's rename executable. – Gilles Quénot Sep 26 '23 at 21:21
  • Since this answer makes a rather general claim and the linked answer doesn't include the bit of information about mksh, this needs to be mentioned. I will comment on that article as well. P.S: How many people use a shell is irrelevant. It exists and is used. – Vilinkameni Sep 26 '23 at 21:25
  • Feel free to improve linked answer if you are picky – Gilles Quénot Sep 26 '23 at 21:40
  • Technically, mksh being the shell of Android, it's on most of the world's devices, though in practice it's not directly used by many users. – Stéphane Chazelas Sep 27 '23 at 05:35
1

Assuming that the filename globbing pattern *.wav matches all the names that you want to change, then you may possibly use the following loop to rename the matching names in the way you want:

n=1
for name in *.wav; do
    while [ -e "file$n.wav" ]; do n=$((n+1)); done
    mv -- "$name" "file$n.wav"
done

This iterates over all matching names. For each name, it finds an integer $n such that the name file$n.wav does not already exist. It then changes the current name to that name.

Kusalananda
  • 333,661
1

With zsh's zmv:

autoload -Uz zmv
n=0; zmv -n '*.wav' 'file$((++n)).wav'

Or to left pad the numbers with 0s to a width of 3 so the files get sorted by number in the output of ls and most other things:

n=0; zmv -n '*.wav' 'file${(l[3][0])$((++n))}.wav'

Remove the -n (dry-run) if happy. It will check ahead of time if there are any potential conflicts.

If some files have already been renamed, you'd initialise n to the highest number value of already existing file<number>.wav files and change the *.wav pattern to (^file<->).wav.