LinuxQuestions.org
Visit Jeremy's Blog.
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 09-16-2004, 11:26 AM   #1
linux_ub
Member
 
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65

Rep: Reputation: 18
renaming batch of files


hi

i need some help renaming a batch of files

the original files are spread around a few directories with overlapping names

eg. /image1/file1.jpg
/image1/file.jpg
/image2/file.jpg
/image2/file1.jpg
/image2/file2.jpg
/image3/file1.jpg


i need the files in this format

/image/file001.jpg
/image/file002.jpg
/image/file003.jpg
/image/file004.jpg
/image/file005.jpg
/image/file006.jpg

i need the number in the new files to be a exactly 3 digits. i am sure tht i dont have more than 999 files

thanks a lot for ur help
 
Old 09-16-2004, 03:37 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Ummm ... that will be scripting :)


Cheers,
Tink
 
Old 09-16-2004, 03:50 PM   #3
realjustin
Member
 
Registered: Aug 2004
Location: /dev/null
Distribution: Slack 10, Debian
Posts: 99

Rep: Reputation: 15
www.tldp.org/LDP/abs/abs-guide.pdf

That is the best bash scripting guide I know of. Read it and you'll have no issues. You're looking for a slightly less trivial script than what you are probably ready for, so this guide will prepare you well.
 
Old 09-16-2004, 04:22 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,417

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
You'll have to generate the leading zeroes yourself. Use ${#var} syntax as exampled on this page: http://tille.soti.org/training/bash/ch10s03.html
 
Old 09-17-2004, 04:22 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
I did not test, but it should give you a start, if not all-done:
Code:
#!/bin/bash
# $1 (optional): file mask (eg: "*.gif"), else "*.jpg" assumed
# $2 (optional): file prefix (eg: "file"), else "file" assumed
# $3 (optional): destination dir (eg: "~/images"), else "$HOME" assumed
# $4 (optional): ':'-separated list of directories
#                (eg: "/mnt/img1:/mnt/img2"), else "." assumed
# Files named like ${1} in directories ${4} will be copied to ${3}
# with name ${2}000 ... ${2}999 (maximum) with same extension as ${1}

if [ -z "$1" ]; then
	echo "File mask (first parameter) must be given." >&2
	exit 1
fi

MASK="${1:-\*.jpg}"
EXT="${MASK##*.}"
[ -n "$EXT" ] && EXT=".$EXT"
FPFX="${2:-file}"
DEST="${3:-$HOME}"
DIRS="${4:-.}"
DIRS="$(echo "$DIRS" | sed 's|:|\n|g')"
NUM=0

if [ ! -d "$DEST" ]; then
	mkdir -p "$DEST" || ( \
	echo "Destination directory (second parameter) is not valid." >&2
	exit 1)
fi
DEST="$(cd "$DEST" && pwd)"

function formatNum() {
	local result="$1"
	while [ ${#result} -lt $2 ]; do
		result="0$result"
	done
	echo "$result"
}

echo "$DIRS" | while read DIR; do
	if [ -d "$DIR" ]; then
		DIR="$(cd "$DIR" && pwd)"
	else
		echo "Directory $DIR skipped. It does not exist." >&2
		continue
	fi
	if [ -z "${DEST##${DIR}/*}" ]; then
		echo "Directory $DIR skipped. It contains the destination." >&2
	fi
	find "$DIR" -type f -iname "$MASK" -print
done | while read FILE; do
	NUM2=$(formatNum $NUM 3)
	while [ -e "${DEST}/${FPFX}${NUM2}${EXT}" ]; do
		NUM=$(( $NUM + 1 ))
		NUM2=$(formatNum $NUM 3)
	done
	cp -f "$FILE" "${DEST}/${FPFX}${NUM2}${EXT}"
done
Yves.
 
Old 09-17-2004, 01:11 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,417

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Actually, for nums you could prob use:
myvar=`printf "%03d\n" $oldvar`
 
Old 10-27-2004, 10:41 PM   #7
xplasma
LQ Newbie
 
Registered: Sep 2003
Location: Here
Distribution: Slackware
Posts: 6

Rep: Reputation: 0
if u lazy u can use this

I know some of us dun have the time to read a whole book just to figure out how to batch rename some files

BTW That pdf rocks, i highly recommend it for anyone who wants to know more about bashing ..

I was browsing around and found this program Krename It rocks for batch renaming !@
Maybe use it as alternative while your learning how to do it by your own code .. Anyway thought id help out alil ..
 
  


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
Renaming multple files.... maginotjr Slackware 4 06-04-2006 11:09 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 04:24 PM
bash help renaming files kahn Programming 6 06-16-2005 08:15 AM
Renaming files as they are uploaded Cr4wford Linux - Software 1 04-26-2004 04:41 PM
Renaming files in one go anon318 Linux - Software 1 01-12-2004 02:16 PM

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

All times are GMT -5. The time now is 11:48 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
Open Source Consulting | Domain Registration