Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory with a couple thousand images in it. Most of them have sensible filenames like SD-000.tif SD-001.tif BX-000.tif etc...

However probably ~25% of them have names like BX-003_old.tif BX-004_veryold.tif

How can I rename the problem files by removing the _word from the end of the each?

The files are stored on a FreeBSD server however the directory is shared out via samba.

I tried using combinations of sed and awk but I'm not having any luck getting them to only work with the middle of the files.

Does anyone have any ideas?

share|improve this question
newname=${filename%_*}.${filename##*.}

${filename%_*} gives the part before the last underscore.
${filename##*.} gives you the part after the last dot (the extension).

share|improve this answer
    
In case anyone ever ends up having a similar problem in the future I ended up fixing this with the cut command. – torcod Aug 11 '11 at 19:46

In case anyone ever ends up having a similar problem in the future I ended up fixing this with the cut command. I ran:

for i in *
do
mv $i `echo $i|cut -d"_" -f1`
done

The loop simply selects all files in the current directory and the cut -d specifies to truncate everything after the specified character. In my case the _.

share|improve this answer
    
You don't care about the file's extension? – glenn jackman Aug 11 '11 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .