0

I have a directory with some files that are messed up:

1243435.pdf
1222424.pdf
643234234.pdf
3423436pdf
45435435pdf
343432.pdf.meta
345232.pdf.meta
3434311pdf.meta
12121234pdf.meta

Files with extension .pdf or .pdf.meta are OK and should stay the same, but how can I rename only the ones that don't have . in front of pdf?

45435435pdf > 45435435.pdf
12121234pdf.meta > 12121234.pdf.meta

Thank you for the help!

Sammy Merk
  • 21
  • 8

3 Answers3

2

I think you can find a better method.

find . -type f -regex '\.\/[^.]+pdf\.?[meta]*?' -exec bash -c 'mv "$1" "${1/\pdf/\.pdf}"' -- {} \;
Magikon
  • 123
  • 4
1

You can match all the files with *[^.]pdf, i.e. anything followed by a non-dot followed by pdf. To extract the prefix, use parameter expansion:

#! /bin/bash
for file in *[^.]pdf ; do
    prefix=${file%pdf}
    mv "$file" "$prefix".pdf
done
choroba
  • 231,213
  • 25
  • 204
  • 289
0

First

Save your files in a list e.g. list.txt

Second

Try a dry-run to see it will be correct

perl -lne 's/(?<=\d)(?=pdf)/./g && print' list.txt

Which for your input, has this output

3423436.pdf
45435435.pdf
3434311.pdf.meta
12121234.pdf.meta

Thrid

Run it

perl -lne '($old=$_) && s/(?<=\d)(?=pdf)/./g && rename($old,$_)' list.txt

If you have lots of files, this is the fastest way.

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44