LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-04-2009, 02:44 AM   #1
atiman
LQ Newbie
 
Registered: Mar 2009
Distribution: FreeBSD
Posts: 2

Rep: Reputation: 0
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
 
Old 03-04-2009, 03:01 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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/'`

Last edited by Disillusionist; 03-04-2009 at 03:05 AM.
 
Old 03-04-2009, 03:03 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Hi,

welcome to LQ!

Hmmm ... interesting :}

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

man rename
 
Old 03-04-2009, 04:22 AM   #4
overlook
LQ Newbie
 
Registered: May 2006
Location: Sweden
Distribution: Custom
Posts: 11

Rep: Reputation: 1
Quote:
Originally Posted by atiman View Post
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.

Last edited by overlook; 03-04-2009 at 04:54 AM.
 
Old 03-04-2009, 10:29 PM   #5
atiman
LQ Newbie
 
Registered: Mar 2009
Distribution: FreeBSD
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 03-05-2009, 01:42 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell command for bulk 'rename' ashrafulkarim Linux - Newbie 5 12-17-2008 04:46 AM
how to rename files bkcreddy17 Programming 7 09-14-2008 10:22 AM
rename files allelopath Linux - General 5 07-05-2005 04:00 AM
Deleting files in bulk through ftp doctorwebbox Linux - Software 2 12-25-2004 01:43 PM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 02:36 PM


All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration