Weblog entry #1 for a_ashabi

#25
Posted by a_ashabi on Mon 5 Jan 2009 at 22:37
Tags: none.
Hi.I have a folder with 1000 file names like this:
14280_aljuc_1.jpg
14280_aljuc_3.jpg
14280_LBL.jpg
14280_SUP.jpg
100_Fkidbld_3.jpg
110_Fkidbld_1.jpg
1001_Soothing_1.jpg
10100_Half_1.jpg
...

if there is a middle part in the name of the files I want to rename them and delete the middle text.which means I need to have these results:
14280_1.jpg
14280_3.jpg
14280_LBL.jpg
14280_SUP.jpg
100_3.jpg
110_1.jpg
1001_1.jpg
10100_1.jpg

plz help.thanks

 

Comments on this Entry

Posted by Anonymous (70.255.xx.xx) on Tue 6 Jan 2009 at 00:07
sed is your friend here. First, pick only the files you want for a for-loop. Using sed, capture the two parts of the filename you do want, and use backreferences to paste them together. Add the -i option to mv if you don't want to risk stomping any files.

for file in [0-9]*_*_[0-9]*.*
do
mv $file `echo $file | sed -e 's/\([0-9]*_\)[^_]*_\([0-9]*\)/\1\2/'`
done

[ Parent | Reply to this comment ]

Posted by Anonymous (75.62.xx.xx) on Tue 6 Jan 2009 at 09:19
Thanks for yr reply.I've installed sed.exe but when I try to do the command it says:
file was unexpected at this time
what did I miss?

[ Parent | Reply to this comment ]

Posted by Anonymous (194.7.xx.xx) on Tue 6 Jan 2009 at 14:32
If you are using windows (which i guess you use because of the 'sed.exe' you installed) the commands above don't work essentially because of the syntax of the for loop and the environment variables. (windows for syntax is here http://www.ss64.com/nt/for.html)

[ Parent | Reply to this comment ]

Posted by Anonymous (80.185.xx.xx) on Tue 6 Jan 2009 at 15:11
If you are using Windows, than install total commander from ghisler.com, it has a great tool to rename multiple files.

"Files > Multi-Rename Tool or CTRL M as a shortcut"

[ Parent | Reply to this comment ]

Posted by Nilshar (82.238.xx.xx) on Tue 6 Jan 2009 at 08:47
[ Send Message | View Weblogs ]
rename can do the job too.

[ Parent | Reply to this comment ]

Posted by Anonymous (212.24.xx.xx) on Tue 6 Jan 2009 at 15:10
hello some inspiration:

$ a=14280_aljuc_3.jpg
$ echo ${a//_*_/_}
14280_3.jpg

or use renameutils [1] and edit the names (using regexp for example) in vim
[1] http://packages.debian.org/search?keywords=renameutils

regards,
-jhr.

[ Parent | Reply to this comment ]

Posted by rjc (85.12.xx.xx) on Thu 8 Jan 2009 at 09:59
[ Send Message ]
aptitude install mmv

In a directory containing the files:

mmv "*_*_*" "#1_#3"

Cheers,
rjc

[ Parent | Reply to this comment ]