rename files using loop with different name


 
Thread Tools Search this Thread
Operating Systems Linux rename files using loop with different name
# 1  
Old 06-25-2010
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.
Code:
file1.dat
file2.dat
file3.dat

the above files has to be moved to another directory like below
Code:
file1_201001.dat
file2_201002.dat
file3_201003.dat

can you please help me

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-25-2010 at 08:37 AM.. Reason: code tags
# 2  
Old 06-25-2010
Are there more than 100 or even 1000 files?
What if the renamed file has a name like file100.dat, what would be the name of the copied file? file100_201100.dat or file100_2010100?
And what if the renamed file has a name like file1000.dat, what would be the name of the copied file? file1000_201000.dat or file1000_20101000.dat?

---------- Post updated at 13:45 ---------- Previous update was at 13:39 ----------

Do the existing files already have an extension? Do you want to keep it or remove it?
# 3  
Old 06-25-2010
Code:
$> ll
total 12
drwxr-xr-x 3 root root 4096 25. Jun 14:00 .
drwxr-x--- 4 isau isau 4096 25. Jun 13:37 ..
-rw-r--r-- 1 root root    0 25. Jun 14:00 file1.dat
-rw-r--r-- 1 root root    0 25. Jun 14:00 file2.dat
-rw-r--r-- 1 root root    0 25. Jun 14:00 file3.dat
drwxr-xr-x 2 root root 4096 25. Jun 14:00 newdir
$> Y=`date +%Y`; D=0; for FILE in file*.dat; do D=`printf "%02d\n" $((D+1))`; mv $FILE newdir/`echo $FILE| sed 's/\./_'${Y}${D}'./'`; done
$> ll newdir
total 8
drwxr-xr-x 2 root root 4096 25. Jun 13:58 .
drwxr-xr-x 3 root root 4096 25. Jun 13:58 ..
-rw-r--r-- 1 root root    0 25. Jun 13:58 file1_201001.dat
-rw-r--r-- 1 root root    0 25. Jun 13:58 file2_201002.dat
-rw-r--r-- 1 root root    0 25. Jun 13:58 file3_201003.dat

Suitable up to file99.dat - the printf adds a missing 0 in front of a single digit. Else you might want to increase 02d to whatever number number of digits you encounter.
# 4  
Old 06-28-2010
Hi,

there are 3 folders eg. IN, PRC & OUT.

files (.dat) will be copied to IN folder every 15 minutes (number of files may be 50 -100).

i have to write a shell script, that will move all the files from IN folder to PRC folder and then all the files will be executed (through SQL LDR) on by one and moved to OUT folder.


when files are moved from IN folder to PRC folder it should be moved with different name to PRC folder.


thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

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

4. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

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

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

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

8. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

9. Shell Programming and Scripting

Rename files

Hello, I've a list of file like this img_001 img_22 img_44 and I would rename all with this form photo_0001 photo_0002 photo_0003 photo_0004 suggestions?Thanks to all. (2 Replies)
Discussion started by: cv313x
2 Replies

10. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: sandeepb
3 Replies
Login or Register to Ask a Question