renaming and number padding using directory as guide, help


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting renaming and number padding using directory as guide, help
# 1  
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 : /home/shots/pic/seasons/winter/snowy

I need the script to recognize pieces of the path as variables to the images new name.

pt.2
path : /home/$NAME/pic/seasons/$SEASON/

I then need the file which is named xyxy.1.tif to be named in the following convention

pt.3
name : $NAME_$SEASON.0001.tif

the number needs to be repadded with 4 digits

I have been hacking at it for the past two days and have comeup with a rather large mess and have finally broken down and called out for help. I had it half working in Perl my knowlege of that is even more limited then the BASH shell. Thanks
# 2  
I'm more of a Korn shell man, myself. Here is a solution for that:
Code:
#!/bin/ksh

    typeset -Z4        IMG=0

    DIR=/home/shots/pic/seasons/winter/snowy/

    NAME=$(expr "$DIR" : "/home/\([^/]*\)/pic")
    SEASON=$(expr "$DIR" : ".*seasons/\([^/]*\)/")

    for FILE in $(find $DIR -type f -name "*.tif")
    do
        echo "mv $FILE ${NAME}_${SEASON}.${IMG}.tif"

        (( IMG += 1 ))
    done

# 3  
how are you using that expr command?
# 4  
lol, nevermind got it.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #357
Difficulty: Medium
BusyBox includes an AWK implementation written by Linus Torvalds.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zero padding a Number before and after a decimal place

Hi I was hoping someone could help me with a sed script I am trying to write? I am on a Mac running ElCapitan I have some text that I have converted from a pdf that I want to format into an xml file. In the file I have managed to delete all the text I do not need. The text I have left is... (8 Replies)
Discussion started by: Paul Walker
8 Replies

2. Shell Programming and Scripting

Renaming a file with sequence number

Hi team, I need a script for renaming a file with sequence number. script get a file from one directory, /home/billing/Cmm/sms/sms_tmp, append sequence no at the end of file name and move a file to other directory, /home/billing/Cmm/sms/. Actual file is cdr201508271527, and file after... (10 Replies)
Discussion started by: mfaizan40
10 Replies

3. Shell Programming and Scripting

Renaming the file name for n number of files

Hi , I am kind of new to shell scripting and found a situation to handle ... I have few files which will be ftpd in to our sustem , the file names needs to be renamed based on condition. ------------ Eg file names :- AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT... (6 Replies)
Discussion started by: chillblue
6 Replies

4. Shell Programming and Scripting

Renaming multiple files in a directory

Hello, I would like to rename all available files in a directory from Filename to Filename_Normal. I tried to use below script but it is giving some error: #!/bin/sh for i in `ls` do echo Changing $i mv $i $i_Normal done Error received: Usage: mv src target or: mv ... (10 Replies)
Discussion started by: manishdivs
10 Replies

5. Shell Programming and Scripting

Copying subdirectories of a directory to some other directory and renaming them

Hi, I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory. please... (8 Replies)
Discussion started by: sholay
8 Replies

6. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: SporkFu
6 Replies

7. UNIX for Dummies Questions & Answers

renaming files in the directory

suppose i have a few file like "a b c.txt" , "hello world.c" etc...(files that have space between them in their filenames). how do i change their filenames to "a_b_c.txt" and "hello_world.c" respectively? heres what i have tried...but failed $ find -name "*" -exec mv "{}" "`echo {} | tr "... (4 Replies)
Discussion started by: c_d
4 Replies

8. UNIX for Dummies Questions & Answers

Renaming a Number of Files

I want to rename my mp3s. Iw ant them to use a standard of 'Band Name - Song Title.mp3'. Currently, all of my mp3s are in the following directory structure: /MyMusic/Band Name/Album Name/*.mp3 Esentailly I want to update each file with the Band Name from the hierarchy and then move all... (2 Replies)
Discussion started by: fnard
2 Replies

9. Shell Programming and Scripting

Renaming files as per directory

I have many files with duplicate names spread out over several tens of directories. I would like to mv them to the parent directory, but to avoid conflicting filenames I'd like to prefix each filename with the name of the directory it was in. For example, if this is my directory structure:... (2 Replies)
Discussion started by: dotancohen
2 Replies

10. Shell Programming and Scripting

renaming files in a directory to lowercase

can anybody help me in renaming all the file in a directory to lowercase? script will be helpful. (1 Reply)
Discussion started by: vhariprasad
1 Replies

Featured Tech Videos