Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-02-2012, 10:20 PM   #1
LQ Newbie
Registered: Mar 2012
Posts: 23

Rep: Reputation: Disabled
Rename filles help


How can I append a zero next to a number in a file name.. For example if I have three files named file1 file2 file3, how can I rename then to file01 file02 file03 all at once without doing it one at a time?


Last edited by GrepAwkSed; 04-02-2012 at 10:22 PM.
Old 04-02-2012, 10:36 PM   #2
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130
Lots of ways. How many files are there? If it's less than 10, the easiest way is
for i in file?; do mv $i ${i/file/file0}; done
It all depends on how many files there are, how many digits you want to expand the number to, whether or not the text before the number is the same or has the same number of digits, etc.
Old 04-02-2012, 10:44 PM   #3
LQ Newbie
Registered: Mar 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
thanks suicidaleggroll

Yes, I do have many files and I just need to covert the first nine files. They all have the same common name.

Anyway, the code you gave me works as I needed. Thanks for the help
Old 04-03-2012, 05:11 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
I wrote up a script a while back specifically for padding numbers in filenames. You can specify the number of zeroes to pad them with, and will prompt you before completing the renaming.

Use it in combination with globbing to target only the files you want.


# Pads numbers in file names if found.
# See help message for more.

# Set the environment
shopt -s extglob

BCYAN="${BCYAN:-\e[1;36m}"    #Define some color codes, for prettified output.
#BGREEN="${BGREEN:-\e[1;32m}"  #Use environment defaults if they exist.

#set default padding level

# Set up the help dialog
	local -a help
	help+=( '' 																				)
	help+=( '\tA quick script to zero-pad files that contain numbers.' 									)
	help+=( '\tIt will only pad the first number string it finds in the name, and ignores files without numbers.'	)
	help+=( '' 																				)
	help+=( "\tUsage: \t${BCYAN}${0##*/} [-n <num>] <files>${RESET}"										)
	help+=( "\t\t${BCYAN}${0##*/} -h${RESET}" 														)
	help+=( '' 																				)
	help+=( '\tUse -n to specify the number of digits to pad, from 1-9.  Defaults to '"$pad"' if not used.'		)
	help+=( '\tIf no files are given, it processes the current directory.' 								)
	help+=( '' 																				)
	help+=( '\t-h prints this help message'															)
	help+=( '' 																				)

	local IFS=$'\n'
	echo -e "${help[*]}"

# Process input options
# If "-h" print help & exit.
# If "-n" test for valid inputs and set "pad" variable
# Ignore anything else
while getopts ":hn:" opt; do

     case "$opt" in

          h) help >&2
             exit "2"

          n)	if [[ "$OPTARG" =~ [^[:digit:]] ]] || (( "10#$OPTARG" < 1 )) || (( "10#$OPTARG" > 9 )); then
				echo -e "${BCYAN}invalid option: [$OPTARG].${RESET}" >&2
				echo -e "${BCYAN}-n must be an integer from 1 to 9${RESET}" >&2
				echo -e "${BCYAN}Falling back to the default of $pad${RESET}"
				pad="$(( 10#$OPTARG ))"

          \?) echo -e "${BCYAN}Invalid option: [-$OPTARG].  Ignoring.${RESET}" >&2

shift $(( OPTIND - 1 )) ; OPTIND=1

# Now test for files.
# If nothing given, set input parameters to files in current directory.
if [[ -z "$*" ]]; then
	set -- ./*

# Process files in input parameters

for file ; do

	# Ignore files without digits
	[[ "$file" != *[0-9]* ]] && continue

	# Split filename into prefix-digits-suffix
	[[ "$file" =~ $re ]]

	# Pad digits to desired width
	printf -v numpad "%0*d" "$pad" "${BASH_REMATCH[2]##*(0)}"

	# Add old and new filenames to arrays for final processing
	oldfile+=( "$file" )
	newfile+=( "${BASH_REMATCH[1]}${numpad}${BASH_REMATCH[3]}" )


# If there are any files to rename, ask to confirm the operation.
# And rename if confirmed.
if [[ -n "${oldfile[*]}" ]]; then

	echo -e "${BCYAN}Rename the following files?${RESET}"
	for i in "${!oldfile[@]}" ; do
		echo -e "${oldfile[i]/#$PWD/.}\t-->\t${newfile[i]/#$PWD/.}"

	read -p "(y/n): "


	case "${REPLY,,}" in

		y*)  	for i in "${!oldfile[@]}" ; do
					mv -n "${oldfile[i]}" "${newfile[i]}"

		*)		echo -e "${BCYAN}Aborting.${RESET}"
				exit 1

# Otherwise just exit.
	echo -e "${BCYAN}No files to rename.${RESET}" >&2
	echo -e "${BCYAN}Exiting.${RESET}" >&2
	exit 1


exit 0


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
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 03:14 AM
[SOLVED] rename all lipun4u Linux - Newbie 8 05-02-2011 07:26 PM
rename errorsunknownp Linux - Newbie 2 12-28-2008 06:06 AM
Problem with log filles postfix dawidson Linux - Newbie 3 11-03-2005 07:12 PM
Rename my pc ? Tanc Linux - Newbie 6 09-13-2003 09:28 AM > Forums > Linux Forums > Linux - Newbie

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

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