LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bulk rename files (https://www.linuxquestions.org/questions/linux-newbie-8/bulk-rename-files-709037/)

atiman 03-04-2009 01:44 AM

bulk rename files
 
hi,

i have a bunch of files which i want to rename by truncating first 6 and last 4 characters from the file name keeping the extension the same.

original file name

Code:

20081_dinnerparty01.jpg.jpg
20091_pool01.jpg.jpg
20073_homegame.jpg.jpg
20068_uniday.jpg.jpg

new file name

Code:

dinnerparty01.jpg
pool01.jpg
homegame.jpg
uniday.jpg


regards
atiman

Disillusionist 03-04-2009 02:01 AM

Have you tried using sed?

Code:

old_name=20081_dinnerparty01.jpg.jpg
new_name=`echo $old_name|sed 's/^[0-9][0-9]*_//'|sed 's/\.jpg\.jpg/\.jpg/'`


Tinkster 03-04-2009 02:03 AM

Hi,

welcome to LQ!

Hmmm ... interesting :}

It will depend on what rename comes with BSD ...

man rename

overlook 03-04-2009 03:22 AM

Quote:

Originally Posted by atiman (Post 3464231)
original file name
Code:

20081_dinnerparty01.jpg.jpg
20091_pool01.jpg.jpg
20073_homegame.jpg.jpg
20068_uniday.jpg.jpg

new file name
Code:

dinnerparty01.jpg
pool01.jpg
homegame.jpg
uniday.jpg


This should do the trick:
Code:

#!/bin/sh
if [ ! -d ${1} ]; then
  printf "$(basename ${0}): Error: parameter needs to be a valid path.\n"
  exit 1
else
  for img in $(find ${1} -type f -name '*.jpg') ; do
    dir=$(dirname ${img})
    f=$(basename ${img})
    mv ${img} ${dir}/$(printf ${f} | cut -f2- -d_ | sed 's/\.jpg//g').jpg
  done
fi

The above script grabs everything in the string after the first underscore (of the filename) and strips away redundant .jpg's at the end of what's left. It only takes the path where you keep your images as an argument. I don't have access to a shell right now so I can't test it for typo's, but it should work, I hope.

atiman 03-04-2009 09:29 PM

thx everybody .... i tried all of them and the one which worked for me was rename command. I had run the command a few times to get the desired result.

@Disillusionist

i have never used sed :(

@overlook

i tried ur script but all i got was permission denied error

@Tinkster man rename worked

thx again for the help

regards
atiman

ghostdog74 03-05-2009 12:42 AM

if you don't mind a ready script and you have Python, here's how you can use it:
Code:

# ls -1 *.jpg
20068_uniday.jpg.jpg
20073_homegame.jpg.jpg
20081_dinnerparty01.jpg.jpg
20091_pool01.jpg.jpg

# filerenamer.py -c :6  "*.jpg" #remove first 6 characters
/home/images/20091_pool01.jpg.jpg  is renamed to  /home/images/pool01.jpg.jpg
/home/images/20068_uniday.jpg.jpg  is renamed to  /home/images/uniday.jpg.jpg
/home/images/20073_homegame.jpg.jpg  is renamed to  /home/images/homegame.jpg.jpg
/home/images/20081_dinnerparty01.jpg.jpg  is renamed to  /home/images/dinnerparty01.jpg.jpg

# ls -1 *.jpg
dinnerparty01.jpg.jpg
homegame.jpg.jpg
pool01.jpg.jpg
uniday.jpg.jpg

# filerenamer.py -c -4:  "*.jpg" #remove last 4 chars
/home/images/homegame.jpg.jpg  is renamed to  /home/images/homegame.jpg
/home/images/pool01.jpg.jpg  is renamed to  /home/images/pool01.jpg
/home/images/dinnerparty01.jpg.jpg  is renamed to  /home/images/dinnerparty01.jpg
/home/images/uniday.jpg.jpg  is renamed to  /home/images/uniday.jpg

# ls -1 *.jpg
dinnerparty01.jpg
homegame.jpg
pool01.jpg
uniday.jpg



All times are GMT -5. The time now is 01:19 PM.