LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-25-2008, 02:08 PM   #16
boerl
LQ Newbie
 
Registered: Dec 2008
Posts: 1

Rep: Reputation: 0
Smile regexp rename utility with renumbering of groups of files


i wrote an extension which also can renumber files. in case someone is interested:
Code:
#!/bin/bash
#regexp rename utility with renumbering of groups of files
#files with the same portion anywhere in the filename can be numbered anywhere in the filename  
#in dsl linux shell
#note:
#this script deletes file called groups.txt if present

dir="/mnt/hda5/ps fotos"

echo >groups.txt
i=0
ls "$dir" | while read file; do
   echo old $file
   group=`echo $file | sed -e "s/^\(....\).*/\\1/"`
   found=`sed -n "-e /$group/ {p q}" groups.txt`
   if test -z "$found"
   then echo "$group" >> groups.txt
     i=11                                           #starting value
   else
     i=`expr "$i" + 1`                              #increment
   fi
   prettyi=`printf "%03d" "$i"`                     #%03d is 000 to 999 to 1000..
   newname=`echo $file | sed -e "s/^\(....\) ....../\\1 ""$prettyi""000/"`
   echo new $newname
   mv "$dir/$file" "$dir/$newname"
done
rm groups.txt

#renames files like
# 1989 112200 corio.jpg
# 1989 112201 corio.jpg
# 1990 112300 corio.jpg
#to
# 1989 011000 corio.jpg
# 1989 012000 corio.jpg
# 1990 011000 corio.jpg
#that is, renumbers files starting with 11000 each new year. increment is 1000.
#order doesn't change

#remember chmod +x foo.sh

#thx to jamiew  for Handling filenames with spaces in a bash for loop
#thx  Bebo from Go"teborg for the  Batch Script to rename files...
jo
 
Old 06-14-2009, 01:26 PM   #17
furthen
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Thanks for these! Here's my version.

For images.

This code will just rename all files in given folder to 0,1,2,3,4 .jpg

WARNING, usage:

/path/to/batchrename /PATH/TO/FILES/



Code:
#!/bin/bash
#regexp rename utility with renumbering of groups of files
#files with the same portion anywhere in the filename can be numbered anywhere in the filename
#in dsl linux shell


dir=$1


i=0
ls "$dir" | while read file; do
   echo old $file
   newname=`echo "$i.jpg"`
   let "i=$i+1"
   echo new $newname
   mv "$dir/$file" "$dir/$newname"
   echo "------------------------------------"
done


#renames files like
# 1989 112200 corio.jpg
# 1989 112201 corio.jpg
# 1990 112300 corio.jpg
#to
# 1989 011000 corio.jpg
# 1989 012000 corio.jpg
# 1990 011000 corio.jpg
#that is, renumbers files starting with 11000 each new year. increment is 1000.
#order doesn't change

#remember chmod +x foo.sh

#thx to jamiew  for Handling filenames with spaces in a bash for loop
 
  


Reply

Tags
linux, rename



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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 03:57 PM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:29 AM.

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
Open Source Consulting | Domain Registration