Base Name

Aug 23 2005

As I’m always happy to see a little unix utility saving me a lot of time, I will start this unix tip section in my blog, hoping to bring joy to the likes of me.

Today’s tip comes from the fellow designer Garrett. In my lousy little bash scripts I have resorted to using my poor regex skills and sed to strip out an extension from filename or remove a directory prefix. It turns out that there is a neat utility that does it for ya. It’s called basename. The syntax is basename NAME [SUFFIX] where NAME is the filename that includes a file path. The optional suffix parameter allows you to strip out an extension. As an example is always most useful, here’s how to render a bunch of svgs into the current directory using inkscape:

for svg in /home/jimmac/icons/*svg; \

do stripped=basename $svg .svg; \ echo “converting $stripped to PNG”; \ inkscape -e $stripped.png $svg; done;