10 responses to “Using AWK to remove file extensions”

  1. tannoiser

    *cough*

    rename ‘s/\.jpg$//’ *.jpg

    ;)

  2. tannoiser

    I know, Carlo, and your code is nice.
    But to me, the correct answer is “don’t”. Use the appropriate tool, it’s often a good suggestion. ;)

    (of course, this is for the original “researcher”… :))

  3. Random Guy

    any idea how to make your example work on all subfolders within a directory also? i tried ls -R but it won’t work.

  4. Booger

    this is great – i don’t think “tannoiser” understands how an example should illustrate a concept with an easy example so that you can modify it to real needs. In particular, I use a compiler that compiles xyz.sd into xyz.sb. So on a directory of 1000 .sd’s the easiest way to find the 3 that don’t compile is to list all of the .sds and .sbs, remove the extensions and do uniq on the list to see which have only 1 entry (not 2). And AWK is a GREAT tool to do this

  5. anonymous

    Ahh, but what if you have files like “file.txt.old”

    Then you are somewhat stuffed, as the extention gets defined to be .txt.old, rather than .old?

  6. Lucas

    why not make it modular

    #!/bin/bash
    from=$1
    to=$2
    for i in $(ls *${from} | awk -F "." '{print $1}'); do mv $i.${from} $i.${to}; done

Leave a Reply