LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-02-2012, 10:20 PM   #1
GrepAwkSed
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Rep: Reputation: Disabled
Rename filles help


Hi,

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?

Thanks

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

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Lots of ways. How many files are there? If it's less than 10, the easiest way is
Code:
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
GrepAwkSed
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 sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Code:
#!/bin/bash

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

# Set the environment
shopt -s extglob
IFS=""

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

#set default padding level
pad=2


# Set up the help dialog
help(){
	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
				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}"
				echo
			else
				pad="$(( 10#$OPTARG ))"
			fi
          ;;

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

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

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

# Process files in input parameters
re='([^[0-9]*)([0-9]+)(.*)'

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]}" )

done

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

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

	read -p "(y/n): "

	echo

	case "${REPLY,,}" in

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

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

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

fi

exit 0
 
  


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
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


All times are GMT -5. The time now is 12:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration