Home > OS >  Rename multiple files in Linux following pattern
Rename multiple files in Linux following pattern

Time:06-16

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!

CodePudding user response:

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

CodePudding user response:

I think you can find a better method.

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