How To Rename a File in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Rename a File in a loop
# 1  
Old 09-14-2007
How To Rename a File in a loop

Hi All,

I am getting all .txt files from a particular directory, as in a following example:

for FILE in `echo $v_filename`
do
done

here i need to rename each original file with a new name for e.g.
we have 3 files in test directory
/test
a.txt
b.txt
c.txt

These files are in a loop so when i get first file say a.txt,
i need to rename it to a09142007.txt and continue. But for some text processing i need this file in same loop. I am getting error that file does not exists.. Please help

Thanks
sandeep
# 2  
Old 09-15-2007
Quote:
Originally Posted by sandeepb
Hi All,

I am getting all .txt files from a particular directory, as in a following example:

for FILE in `echo $v_filename`

Code:
for FILE in $v_filename

Either way, it will fail if your filenames contain spaces or other pathological characters. You should use:

Code:
for FILE in *.txt

Quote:
do
done

here i need to rename each original file with a new name for e.g.
we have 3 files in test directory
/test
a.txt
b.txt
c.txt

These files are in a loop so when i get first file say a.txt,
i need to rename it to a09142007.txt and continue. But for some text processing i need this file in same loop. I am getting error that file does not exists.

It is not clear what you mean; please post the script you have tried, along with the exact error messages it gives.
# 3  
Old 09-15-2007
Quote:
These files are in a loop so when i get first file say a.txt,
i need to rename it to a09142007.txt and continue. But for some text processing i need this file in same loop. I am getting error that file does not exists.
Once you have renamed a.txt to a09142007.txt, you will not find the file a.txt for next processing, will you ?

instead of moving the file a.txt to a09142007.txt , copy it and then do your next processing with a.txt and remove it at the end.

Hope i am clear. If you need further help , please post your script.
kamitsin
# 4  
Old 09-19-2007
How To Rename a File in a loop

All,

Thanks for your replies.


Based on

"Once you have renamed a.txt to a09142007.txt, you will not find the file a.txt for next processing, will you ? "

I have resolved this problem by renaming a file at the end of loop after
completion of other processings.

Thanks
Sandeep
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

sed to rename files in bash loop

I am trying to use sed to rename all .txt files in /home/cmccabe/test. However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you :). bash # rename classified cd /home/cmccabe/test pattern2_old="_classify"... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

5. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

6. Shell Programming and Scripting

Download images from the first column and rename it with the second column in a loop: Please help!

Dear Friends, I have a very little knowledge on shell scripting. I am stuck with a problem for which I need an expert advice. I have a .txt file "image_urls.txt" which contains the data like this imageurl ... (2 Replies)
Discussion started by: Praveen Pandit
2 Replies

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

8. Linux

rename files using loop with different name

Hi, i need to write a shell script where i have to loop through all the file in a directory and rename them based on below condition. file1.dat file2.dat file3.dat the above files has to be moved to another directory like below file1_201001.dat file2_201002.dat file3_201003.dat... (3 Replies)
Discussion started by: feroz
3 Replies

9. UNIX for Dummies Questions & Answers

Rename files with sed in an until loop (double post)

I want to change the name of some of my files (mypics-0001, mypics-0002, mypics-0003.....mypics-0240) and I want to double check to see if this code is right: x=0 until do sed 's/mypics\-*/bday/g' done Would this change all of my file names to "bday0001....bday0240"? Please let me... (0 Replies)
Discussion started by: jvpike
0 Replies

10. UNIX Desktop Questions & Answers

Rename files without using for loop

Hi, Is it possible to rename files at a time in a directory without using the for loop. (ex: intial filename- abc.txt to abc.tmp or abc.txt.tmp) I need to rename the files in a remote directory to which I'm connecting thru tectia sftp. The commands 'mv', 'for', 'echo' do not work. I have... (3 Replies)
Discussion started by: Qwerty123
3 Replies
Login or Register to Ask a Question