Multiple file rename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple file rename
# 1  
Old 08-18-2012
Multiple file rename

I've been googling for days but can't find a solution to this problem.

I have a number of sets of files on a server

file02.dat
.
.
file12.dat

/.../fred(1 to n)/bill(1 to m)/tony/joe/

in any "fred" branch there will be one or more "bill"s
some joe/'s may not have a fileset and could be empty.

the files need to be renumbered file01.dat.. file11.dat

ie. mv file02.dat file01.dat

if you prefer the filenames could be 01file.dat or any variation.

If I can get it to work, this will be a last day of the month cronjob

Is there a solution to this?
# 2  
Old 08-18-2012
Are you saying that there will never be a file01.dat in any of these directories?

Are all of the files in any given directory a contiguous numeric sequence starting with file02.dat? (I.e., could there just be file02.dat, file10.dat, and file11.dat in one of the directories?)

Are there ever any files other than fileXX.dat in the leaf directories?

Do we need to renumber files in directories that aren't leaves in the file system hierarchy?
# 3  
Old 08-18-2012
It's late for me now, but - with just GNU/Linux find and bash - you could start working from here:
Code:
find /tmp/fred{1..N}/bill{1..M}/tony/joe/file{02..12}.dat 2>/dev/null | while IFS= read -r filename; do num=${filename: -6:2}; newnum=$(echo "$num - 1" | bc); echo ${filename/%$num.dat/$newnum.new.dat} ; done

For instance:
Code:
lem@biggy:/tmp$ find /tmp/fred{1..2}/bill{1..4}/file{02..12}.dat 2>/dev/null
/tmp/fred1/bill3/file02.dat
/tmp/fred1/bill3/file03.dat
/tmp/fred1/bill3/file04.dat
/tmp/fred1/bill3/file05.dat
/tmp/fred1/bill3/file06.dat
/tmp/fred1/bill3/file07.dat
/tmp/fred1/bill3/file08.dat
/tmp/fred1/bill3/file09.dat
/tmp/fred1/bill3/file10.dat
/tmp/fred1/bill3/file11.dat
/tmp/fred1/bill3/file12.dat
/tmp/fred2/bill4/file02.dat
/tmp/fred2/bill4/file03.dat
/tmp/fred2/bill4/file04.dat
/tmp/fred2/bill4/file05.dat
/tmp/fred2/bill4/file06.dat
/tmp/fred2/bill4/file07.dat
/tmp/fred2/bill4/file08.dat
/tmp/fred2/bill4/file09.dat
/tmp/fred2/bill4/file10.dat
/tmp/fred2/bill4/file11.dat
/tmp/fred2/bill4/file12.dat
lem@biggy:/tmp$ find /tmp/fred{1..2}/bill{1..4}/file{02..12}.dat 2>/dev/null | while IFS= read -r filename; do num=${filename: -6:2}; newnum=$(echo "$num - 01" | bc); echo ${filename/%$num.dat/$newnum.new.dat} ; done
/tmp/fred1/bill3/file1.new.dat
/tmp/fred1/bill3/file2.new.dat
/tmp/fred1/bill3/file3.new.dat
/tmp/fred1/bill3/file4.new.dat
/tmp/fred1/bill3/file5.new.dat
/tmp/fred1/bill3/file6.new.dat
/tmp/fred1/bill3/file7.new.dat
/tmp/fred1/bill3/file8.new.dat
/tmp/fred1/bill3/file9.new.dat
/tmp/fred1/bill3/file10.new.dat
/tmp/fred1/bill3/file11.new.dat
/tmp/fred2/bill4/file1.new.dat
/tmp/fred2/bill4/file2.new.dat
/tmp/fred2/bill4/file3.new.dat
/tmp/fred2/bill4/file4.new.dat
/tmp/fred2/bill4/file5.new.dat
/tmp/fred2/bill4/file6.new.dat
/tmp/fred2/bill4/file7.new.dat
/tmp/fred2/bill4/file8.new.dat
/tmp/fred2/bill4/file9.new.dat
/tmp/fred2/bill4/file10.new.dat
/tmp/fred2/bill4/file11.new.dat

HTH.
--
Bye
# 4  
Old 08-19-2012
Hi Guys,

Thanks for the help. When I awoke this morning I must have had a suddden rush of blood to the head and came up with this:
Code:
for DIR in fred*/bill*/tony/joe;
do
mv $DIR/filename02.dat $DIR/filename01.dat;
.
.
mv $DIR/filename12.dat $DIR/filename11.dat;
done

It does the job but if there is a more elegant solution I'd be interested to see it.

Actually the first line of the script deletes filename01.dat
there is a blank.dat which the last line copies to filename12.dat to complete the reset. As that is just a file containing "0" it could be located in /root which is where the script is located.
# 5  
Old 08-19-2012
You might try something like:
Code:
#!/bin/ksh
basedir=$(pwd)
for dir in fred*/bill*/tony/joe/
do
        if ! cd "$dir"
        then    # No permission, or not-a-directory
                printf "%s: %s not processed\n" "$0" "$dir" >&2
                continue
        fi
        printf "%s: processing directory %s\n" "$0" "$dir" 
        target=file01.dat
        src=2
        while [ $src -le 12 ]
        do
                source=$(printf "file%02d.dat" $src)
                if [ -e $source ]
                then    echo mv $source $target
                else    echo touch $target
                fi
                target=$source
                src=$(( src + 1 ))
        done
        echo touch $target
        cd "$basedir"
done

Remove the "echo " in all three places it occurs if you're convinced that this does what you want.

This creates empty files instead of files containing a "0" for file12.dat and any other missing files in the given directories. If you really want a file containing "0" (with no trailing <newline>), replace both touch $target commands with printf "0" > $target. If you want a more complicated initialization file, create it as a file named initfile.dat in the directory where this script will be started and replace the touch commands with cp "$basedir"/initfile.dat $target commands. If you want to be able to run this when you aren't sitting in the base directory for this file hierarchy, add cd rootdir(where "rootdir" is an absolute pathname to the desired starting point) before the line that sets basedir.

Hope this helps...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename multiple file names?

Hi all, I need to rename more file name in one command or script. The files have this structure: XxY - filename.doc where X and Y are numbers and the x is the letter itself. I need to rename these files with this structure: string.S0XEY.filename.doc the string is a suffix that... (8 Replies)
Discussion started by: idro
8 Replies

2. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

3. Shell Programming and Scripting

Multiple File Rename based on pattern - one line

All, I wanted to copy the files From: Daily_XYZ_TEST_1.csv Daily_XYZ_TEST_2.csv Daily_XYZ_TEST_3.csv Daily_XYZ_TEST_4.csv To: Daily_ABC_TEST_1.csv Daily_ABC_TEST_2.csv Daily_ABC_TEST_3.csv Daily_ABC_TEST_4.csv I have tried the rename command but it is not working (5 Replies)
Discussion started by: alfredo123
5 Replies

4. UNIX for Dummies Questions & Answers

Multiple file rename

hi im new to linux and was just wondering if some 1 could help me i have folders with T.V. series in them and i would like to delete part of the filename e.g. (series name).s01e01.(episode name) (series name).s01e02.(episode name) (series name).s01e03.(episode name) (series... (4 Replies)
Discussion started by: stevemcd1990
4 Replies

5. Shell Programming and Scripting

Rename multiple file names in a directory

I hope some one can help me I have multiple files in a directory with out extension like as below mentioned. But i want to change all the file names along .DDMMYYYYHHMISS format. And all files should have same DDMMYYYYHHMISS. Scenario: direcory name = /vol/best/srcfiles files in a... (4 Replies)
Discussion started by: hari001
4 Replies

6. UNIX for Dummies Questions & Answers

For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this... for i in new_* do if ] && ]; then j=`echo "$i"|cut -c5-` mv $i $j fi done ... (5 Replies)
Discussion started by: Korn0474
5 Replies

7. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

8. Shell Programming and Scripting

Rename multiple file from file listing

I am new at Linux/UNIX programming. Here is my problem. I had one big file which I split using the command csplit -k -s -f april.split. april '/^ISA/' '{10000}' So now I have multiple files with names april.split.01 april.split.02 april.split.03 But I need the name of the file like... (5 Replies)
Discussion started by: yshahiac
5 Replies

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question