Renaming files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming files
# 1  
Old 01-15-2011
Renaming files

Can someone please help. Much appreciated!!

I have 4 directories, for ex...

/RUN1/ReportTable.nxt
/RUN2/ReportTable.nxt
/RUN3/ReportTable.nxt
/RUN4/ReportTable.nxt

I would like to write a for loop, to add the directory name to each ReportTable.nxt

I would like for it to be:
/RUN1/Run1_ReportTable.nxt
/RUN2/Run2_ReportTable.nxt
/RUN3/Run3_ReportTable.nxt
/RUN4/Run4_ReportTable.nxt
# 2  
Old 01-15-2011
Try this

create a file called dir and put all the directory names (in your case it is RUN1 and in the next line RUN2 and so on...)

First try this.

just comment the mv command and make sure it gives the correct expected file name in the echo.

mv $j $new_file

Code:
for i in `cat dir`
do
        cd $i;
    for j in `ls`
    do
        new_file=`echo $i\_$j`
        echo $new_file
        #mv $j $new_file
    done
    cd -;
done

# 3  
Old 01-16-2011
Quote:
Originally Posted by itkamaraj
Try this

create a file called dir and put all the directory names (in your case it is RUN1 and in the next line RUN2 and so on...)

First try this.

just comment the mv command and make sure it gives the correct expected file name in the echo.

mv $j $new_file

Code:
for i in `cat dir`
do
        cd $i;
    for j in `ls`
    do
        new_file=`echo $i\_$j`
        echo $new_file
        #mv $j $new_file
    done
    cd -;
done

Uh, there is something wrong with that first line of code. No need to examine the contents of any files with cat, plus you can't cat a directory.
Code:
cat dir
cat: dir: Is a directory

Plus this needs to run from the same directory dir is in so cd dir/$i is necessary. I have no clue what the semicolons are there for so I just removed them. Otherwise this is a nice solution to the problem at hand:
Code:
for i in `ls dir`
do
        cd dir/$i
    for j in `ls`
    do
        new_file=`echo $i\_$j`
        echo $new_file
        #mv $j $new_file
    done
    cd -
done

# 4  
Old 01-16-2011
Using
Code:
for i in `anything`

isn't a nice solution to anything.

And what is the point of:
Code:
new_file=`echo $i\_$j`

when
Code:
new_file=$i\_$j

would do?

And why are there no quotes around variables here:
Code:
mv "$j" "$new_file"

(although I suppose they make no difference as that for-loop can't handle filenames with spaces anyway!)
# 5  
Old 01-16-2011
Very good points there Scottn. When first learning bash or any other shell programing its easy to get focused on just making the code work. There are a lot of gothcha's that can be avoided with a little more thought.

---------- Post updated at 03:02 PM ---------- Previous update was at 01:57 PM ----------

OK, I am still learning this stuff but I think I cleaned it up after taking Scottn's suggestions (works with filenames that have spaces). Plus I learned a lot from the whole process.

Code:
cd dir
for i in *
do
  cd "$i" >/dev/null
  for j in *
  do
    new_file=$i\_$j
    echo mv "$j" "$new_file"
    #mv "$j" "$new_file"
  done
  cd - >/dev/null
done

# 6  
Old 04-08-2011
Code:
user:RUN$ ls
RUN1    RUN2    RUN3    RUN4
user:RUN$ for i in `ls`;do for j in `ls $i`; do mv $i/$j "$i"_"$j";done;done
user:RUN$ ls
RUN1            RUN2            RUN3            RUN4
RUN1_ReportTable.nxt    RUN2_ReportTable.nxt    RUN3_ReportTable.nxt    RUN4_ReportTable.nxt


Last edited by Scott; 04-08-2011 at 08:10 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Hello, I am looking for a command line that will rename name files : f700_abc_o_t_MASTERID_AS_AE_20130323.csv like this f700_abc_o_t_MASTERID_AS_AE_20130324.csv The great idea could be to get the date stamp 20130323 and change any part of it, instead of just change the... (4 Replies)
Discussion started by: Aswex
4 Replies

3. Shell Programming and Scripting

renaming files

Hi, I have a list of files in a folder with the same name ending (over 1000 files) joe.jpy.jpeg joe1.jpy.jpeg joe2.jpy.jpeg jon3.jpy.jpeg jor5.jpy.jpeg .....jpy.jpeg etc. I want to change jpy to hhk So the output will be: joe.hhk.jpeg joe1.hhk.jpeg joe2.hhk.jpeg jon3.hhk.jpeg... (3 Replies)
Discussion started by: kylle345
3 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. Shell Programming and Scripting

renaming files

Hi all, using a utility image file was named starting with blank space and a blank space in between. I want to rename the files. file names are in the format " sb 12.tif"," sb 13.tif"," sb 14.tif" the files are in thousands. i want to rename as 12.tif, 13.tif, 14.tif.... thanks. (3 Replies)
Discussion started by: ahkverma
3 Replies

6. UNIX for Dummies Questions & Answers

renaming files

I have a list of files named ab_*.csv I would like to remane them all by removing the ab_ and have *.csv I did the following but I am surely missing something. /* wrong script */ for i in `ls -1 ab_*`; do mv ab_$i $i; done Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies

7. UNIX for Dummies Questions & Answers

Renaming files

Hello! I am not familiar with UNIX and I have this problem: I need to move files from a UNIX machine to a PC. UNIX file names contain ":" as special character which is not recognized in a PC. How can I change ":" for "_" in the name of a bunch of files in UNIX? Thanks for your help. (7 Replies)
Discussion started by: Tygoon
7 Replies

8. UNIX for Dummies Questions & Answers

renaming files

directory name = /usr/tom/1997 files - ABC_1997_ST1_BCD.SQL BCD_1997_ST1_EFG_SAB.SQL TTT_EBC_1997_ST1_A.SQL sub directory - /usr/tom/1997/jan a) I want to just rename the all files ending with '.SQL' and also its contents in the 1997 directory(excluding subdirectories eg... (3 Replies)
Discussion started by: systemsb
3 Replies

9. UNIX for Dummies Questions & Answers

renaming the files

Hi All, Today I got a small problem while handling zipped files in PROD support. There are files in this format and I had to grep them reading some contents A.B.gz.C.D where A,B,C and D stand for variables (like FIRST.NAME.gz.MIDDLE.LAST). I know that these files are zipped files and If I... (1 Reply)
Discussion started by: adurga
1 Replies

10. UNIX for Dummies Questions & Answers

renaming files

i have a set of *.lst files. now i want to change the names from "lst" to "dat". how to do it? ex.: -rw-r--r-- 1 rram group 22 Sep 21 13:10 a.lst -rw-r--r-- 1 rram group 22 Sep 21 13:09 b.lst -rw-r--r-- 1 rram group 22 Sep 21 13:10 c.lst... (4 Replies)
Discussion started by: raguramtgr
4 Replies
Login or Register to Ask a Question