I have a folder with about 7,000 files and each file is numbered e.g. 74857.mkv 74858.mkv etc.

enter image description here

I also have a text file which has all the file numbers and file names:

In this order:

enter image description here

I would like to rename each file from its number to its name; an example:

74857.mkv > Taken S01 E01.mkv

74858.mkv > Taken S01 E02.mkv

Any commands to do this please I have been doing it manually and only managed to do 50 files in 5 days!

  • 1
    Is "it's name example" the name on the line immediately above? it's not clear from your example (your numbers aren't consistent) Nov 24 '19 at 19:35

I would do these in small batches and test with a copy if I were you but something like this should do you

while read n; do read f; mv $f "$n.${f##*.}"; done < file

Before you start you would be well advised to check the output of

while read n; do read f; echo mv $f "$n.${f##*.}"; done < file > checkfile

To make sure there are no errors in your renaming file.

Safer if you have the space and the time to copy to keep your originals safe until you are happy....

while read n; do read f; cp $f "/a/safe/location/$n.${f##*.}"; done < file


To reverse the process you just need to reverse the order of the names in the mv command

mv "$n.${f##*.}"  $f
  • Hi,Where do I write the text files name that has the list of names of each file? while read n; do read f; echo mv $f "$n.${f##*.}"; done < file > checkfile Nov 25 '19 at 15:27
  • Thats the < file part. Change it to < myfiles.txt or whatever. If you haven't done this before, run a test using it in anger..... check all output with the echo version first and run with the 'cp' version if you have space. Slower but far far safer if you are unfamiliar with scripts.
    – bu5hman
    Nov 25 '19 at 15:39
  • Hi Mate, I can't thank you so much for your help and support I tried your script and the checkfile.txt is showing as them like this: prnt.sc/q1xztn Its made a third line with the .mkv extensions I have not tried with the files as some files are mp4 and avi so if it removes the extensions than It will be hard for me to know which extension the file is. Nov 25 '19 at 19:34
  • Did you check your txt file for blank lines? Is it a Windows text file (run it through dos2unix if it is). Either of those may be part of your problem. Also I tested @steeldriver solution and it works like a charm but both solutions rely on the txt file being correct.
    – bu5hman
    Nov 25 '19 at 20:07
  • You my friend are a genius! You are the man! Yes it was edited in windows sorry I learnt something new.. I used this one because some of my files in different extensions ie .mp4 .avi but you done it all my files are names perfect! Nov 25 '19 at 23:07

Assuming that "it's name example" is the string on the line immediately above the current .mkv file name i.e. that given

$ cat file.txt 
Taken S01 E01
Taken S01 E02

you actually want

74857.mkv > Taken S01 E01.mkv

74858.mkv > Taken S01 E02.mkv

then with GNU parallel and mv:

parallel -N2 echo mv -- {2} {1}.mkv < file.txt

Remove the echo once you are happy that it is composing the correct commands.

  • Doesn't this omit the suffix? {1}.mkv? assuming all are mkv files
    – bu5hman
    Nov 25 '19 at 17:36
  • @bu5hman oops thanks - should be better now Nov 25 '19 at 17:39
  • @bu5hman AFAIK GNU parallel (unlike xargs for example) delimits on newlines by default; it seems to have no problem with the whitespace when I test it on my system Nov 25 '19 at 17:41
  • Just seen that when I tested. lesson learned Like the use of parallel though. If I were doing this I think that mv {2} {1}.{2} would be a good move in either my or your code as it allows OP to revert back to the original naming very easily even if there is a problem in the name list file.
    – bu5hman
    Nov 25 '19 at 17:46
  • @steeldriver Use --dry-run instead of echo. That way you can review more complex commands (e.g. composed commands or redirection to files).
    – Ole Tange
    Nov 25 '19 at 19:32

With perl:

perl -lne '$dst = "$_.mkv"; $src = <>; chomp $src; rename $src, $dst
  or warn "$src -> $dst: $!\n"' your-file

With zsh:

zmodload zsh/files # to get a builtin mv
while IFS= read -ru3 dst && IFS= read -ru3 src; do
   mv -- "$src" "$dst.mkv"
done 3< your-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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