Removing file extensions with sed May 12th, 2010

I need the following sed one-liner periodically, and I thought it was useful enough to share here. Other versions I’ve seen chopped off everything after the first dot, which doesn’t work for files with a dot in the basename. This one does:

sed s/\.[^\.]*$//

Sample Usage:

bsterne@zodiac:~$ ls /video | sed s/\.[^\.]*$// | head -n5
2001: A Space Odyssey
40 Year Old Virgin, The
9
A Bronx Tale
A Clockwork Orange


2 comments on “Removing file extensions with sed

  1. Jojo says:

    Why not simply use bash parameter expansion as follows :

    “i” being the variable storing the file name :
    FILENAME=”${i%.*}”; # matches longuest chain until last “.” is met.
    FILEEXT=”${i##*.}”; # matches shortest chain starting with a “.” (so usually the extension)

  2. Inukaze says:

    How i cant remove using sed , in

    ls -la | awk ‘{print$09}’

    This show a list of folders , but each folder finish in “/”
    i need remove that “/” in the final of name

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>