Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.