1

I'm trying to rename multiple numbered files according to a list of names.

Example:

1.pdf, 2.pdf, …, n.pdf

And a file called names.txt, with a value per line:

Fabio
Joao
n-name

So we will have

  • 1.pdfFabio.pdf
  • 2.pdfJoao.pdf
  • n.pdfn-name.pdf

Any ideas on how to accomplish this?

7
  • 1
    Hi @FabioB, We had a exact identical question over at AskUbuntu! : askubuntu.com/questions/1281686/… Commented Oct 10, 2020 at 17:14
  • 1
    How do you determine the matching? Will the first row of names.txt go to the first, sorting alphabetically ascending, existing filename? Or you have another file old_names.txt that matches one to one the new names.txt?
    – thanasisp
    Commented Oct 10, 2020 at 17:17
  • 1
    No @roaima, it's a routine I'm trying to accomplish to avoid repetitive tasks at work.
    – FabioB
    Commented Oct 10, 2020 at 17:18
  • 1
    @steeldriver I answerd with a post I did on AskUbuntu; But thanks that's some good insight too; //Best Wishes Commented Oct 10, 2020 at 17:49
  • 1
    There are dozens of duplicates of this right here, please try to suggest dupes that are on this site and can be used as dupe targets.
    – terdon
    Commented Oct 10, 2020 at 18:34

1 Answer 1

2

If the files are really just "lineNumber.pdf", then this is very easy to do. In the shell:

c=0
while IFS= read -r name; do
    ((c++))
    echo mv -- $c.pdf "$name.pdf"
done < names.txt

Once you're sure that works as you want it, remove the echo from the mv command.

If you have very many files, you might want to consider doing it in Perl instead which will be much faster:

perl -lne 'rename("$..pdf","$_.pdf")' names.txt 
1
  • Perfect!!! Thanks a lot.
    – FabioB
    Commented Oct 17, 2020 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .