zero padding while renaming files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting zero padding while renaming files
# 1  
Old 03-19-2010
zero padding while renaming files

I wrote a script that renames all files in a folder with a generic numeric filename, but was wondering if there is a more elegant way to do the zero-padding.

BTW it is also pretty slow, I was thinking that was a result of the shell evaluating so many IF statements.

Anyone have any cool tricks to do this better/faster?

Code:
#!/usr/bin/bash

pre=1
cnt=1
list=$(ls)

for file in $list
    do
    if [ -f "$file" ] ; then
        ext=$(echo $file | awk '{n=split($0,e,"."); print (n > 1 ? e[n] : "")}')
        if [ "$cnt" -le 9 ] && [ "$cnt" -ge 1 ] ; then
            pad="0000000"
            fi
        if [ "$cnt" -le 99 ] && [ "$cnt" -ge 10 ] ; then
            pad="000000"
            fi
        if [ "$cnt" -le 999 ] && [ "$cnt" -ge 100 ] ; then
            pad="00000"
            fi
        if [ "$cnt" -le 9999 ] && [ "$cnt" -ge 1000 ] ; then
            pad="0000"
            fi
        if [ "$cnt" -le 99999 ] && [ "$cnt" -ge 10000 ] ; then
            pad="000"
            fi
        if [ "$cnt" -le 999999 ] && [ "$cnt" -ge 100000 ] ; then
            pad="00"
            fi
        if [ "$cnt" -le 9999999 ] && [ "$cnt" -ge 1000000 ] ; then
            pad="0"
            fi
        if [ "$cnt" -le 99999999 ] && [ "$cnt" -ge 10000000 ] ; then
            pad=""
            fi
        mv $file ${pre}${pad}${cnt}.${ext}
        cnt=$(( $cnt + 1 ))
    fi
    done
exit 0

# 2  
Old 03-19-2010
Quote:
Originally Posted by SporkFu
I wrote a script that renames all files in a folder with a generic numeric filename, but was wondering if there is a more elegant way to do the zero-padding.

BTW it is also pretty slow, I was thinking that was a result of the shell evaluating so many IF statements.

It's slow mostly because you are calling awk for every file.
Quote:
Anyone have any cool tricks to do this better/faster?

Code:
#!/usr/bin/bash

pre=1
cnt=1
list=$(ls)

for file in $list


That will break if any filename contains whitespace. Use wildcard expansion:
Code:
for file in *

Quote:
Code:
    do
    if [ -f "$file" ] ; then
        ext=$(echo $file | awk '{n=split($0,e,"."); print (n > 1 ? e[n] : "")}')


You don't need awk:
Code:
ext=${file##*.}

Quote:
Code:
        if [ "$cnt" -le 9 ] && [ "$cnt" -ge 1 ] ; then
            pad="0000000"
            fi
        mv $file ${pre}${pad}${cnt}.${ext}

Code:
printf -v pad "%07d" "$cnt"
mv "$file" "${pre}${pad}.${ext}"

# 3  
Old 03-19-2010
Dude, thanks for the tips, incorporated your suggestions and this works great:
Code:
#!/usr/bin/bash

pre=1
cnt=1

for file in *
    do
        if [ -f "$file" ] ; then
        ext=${file##*.}
        printf -v pad "%07d" "$cnt"
        mv "$file" "${pre}${pad}.${ext}"
        cnt=$(( $cnt + 1 ))
        fi
    done
exit 0

gonna have to figure out how

Quote:
${file##*.}
and

Quote:
printf -v pad "%07d" "$cnt"
actually works. That's some wizardry that is beyond me!
# 4  
Old 03-19-2010
Quote:
Originally Posted by SporkFu
gonna have to figure out how
Code:
${file##*.}

and
Code:
printf -v pad "%07d" "$cnt"

actually works.

Read the bash man page. The first is under Parameter Expansion, the other is a builtin command.
# 5  
Old 03-19-2010
Cool, I'll look it up there. Thanks again, this is the stuff they don't teach you in class.
# 6  
Old 03-19-2010
Quote:
Originally Posted by SporkFu
Cool, I'll look it up there. Thanks again, this is the stuff they don't teach you in class.

Note that the first item is part of the standard (i.e., POSIX) shell.

The second is only in bash.
# 7  
Old 03-20-2010
I get the printf part mostly, but this:
Code:
${parameter##word}

is godlike. I'll be back in a couple days after I have finished researching parameter expansion.

Virtual high fives to you, sir!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming files

Hi i have to achieve the following i have files as xyz001.csv, xyz002.csv.......xyz0025.csv in a folder, i need to keep xyz001.csv as it is but want to remove the extra zero on filename from 10 say xyz0010 should be renamed to xyz010 xyz0025 should be renamed as xyz025 Note xyz... (8 Replies)
Discussion started by: mad_man12
8 Replies

3. UNIX for Dummies Questions & Answers

Renaming files

Hi all, I'm working in a specific directory and I have file names which I'd like to rename but in a way in which I can specify the new filenames as @ARGV or user input at prompt. Can someone shed some light on this? Cheers :) (7 Replies)
Discussion started by: pawannoel
7 Replies

4. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

5. UNIX for Dummies Questions & Answers

Need help renaming files

I just can't figure this one out. I have a lot of files name (for example) ABC1234.5678.ext I need to rename these files U0105678PQRS So I'm removing everything before the first "." I'm keeping "5678" in the file name Adding "U010" to the front of "5678" Dropping the ".ext" extension ... (5 Replies)
Discussion started by: bbbngowc
5 Replies

6. Shell Programming and Scripting

Renaming files

hi, I have the following files as below. reg_test_123232 reg_test_125654 reg_test_473843 How do I rename reg_test_123232 to abc_123232 and the rest of file by just keeping the numeric field ? Please advise :) (3 Replies)
Discussion started by: cedrichiu
3 Replies

7. Shell Programming and Scripting

renaming and number padding using directory as guide, help

I need to take a series of image files, all numbered consecuativly, that were recently dumped in a directory and rename them to pieces of the directories path. Assume all directories are structured as this one so that I may use this script to easly sort and rename files. pt.1 path :... (3 Replies)
Discussion started by: TiredOrangeCat
3 Replies

8. Shell Programming and Scripting

renaming Files

Renaming Files more than 1000 in Diffrent Directories in system.. help me in this issue to resolve.... (5 Replies)
Discussion started by: sunsap
5 Replies

9. Shell Programming and Scripting

Renaming files

Hello. Need to rename 1000's of files with names like 991%20(2003)%20-%20991%20Ryall%20Prison%20Population%20and%20Recorded%20Crime.doc All I need is the beginning numbers , in this case 991 . Tried: for file in * ; do mv $file `echo $file | sed '/^{p;q}'` ; done but no luck. Thank... (8 Replies)
Discussion started by: whatsup
8 Replies
Login or Register to Ask a Question