Search code examples
regexbashawksedcut

Remove text in file name after particular search pattern


I have a file name such as follows:

file_name1.pdf.sometext.here

I have a directory of several files in the same format, and I want to edit all the files so that the portion after .pdf is deleted... thus the file would look like this

file_name1.pdf

Solution

  • Using parameter expansion:

    $ ls *.pdf*
    file_name1.pdf.sometext.here  file_name2.pdf.blah  file_name3.pdf.sometext
    $ for fname in *.pdf*; do mv "$fname" "${fname//.pdf.*/.pdf}"; done
    $ ls *.pdf*
    file_name1.pdf  file_name2.pdf  file_name3.pdf