Shell script to rename files with .1,.2,.3 ....ext respectively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to rename files with .1,.2,.3 ....ext respectively
# 1  
Old 02-06-2009
Lightbulb Shell script to rename files with .1,.2,.3 ....ext respectively

Hey Guys....
Just need some help as I am not proficient in Unix shell script...
Doubt:
---------------
Suppose there will be some of the following files inside a directory called OUT ...

Path: - /appdb1/product/batch/rms/OUT

files inside OUT directory:-
POSU_75002_20090127_20090129035442
POSU_75002_20090127_20090128035145
POSU_75002_20090127_20090129035442

I want a script which will rename these files and put an extension of .1, .2, .3, .4, .5, .6 .................respectively till whatever number of POSU files it will be there inside the OUT directory.....

I just know that mv $LINE $LINE.1 will rename the file to .1 ext but i want the above outputs.....
# 2  
Old 02-06-2009
Try this
Code:
cnt=1
for file in `ls /appdb1/product/batch/rms/OUT/` ; do
    mv ${file} ${file}.${cnt}
    cnt=$(( cnt +1 ))
done

# 3  
Old 02-06-2009
Satyajit,

Try this.

Code:
 
# count the number of POSU files, assuming the file names start with POSU
fc=`ls POSU* | wc -l`
ls POSU* > /tmp/file_list.txt
i=1
while read line
do
  if [[ ${i} -gt ${fc} ]]; then
    break;
  fi
  mv ${line} ${line}.${i}
  i=$(( ${i} + 1 ))
done < /tmp/file_list.txt

HTH, Smilie

Regards,

Praveen
# 4  
Old 02-06-2009
MySQL Thanks a lot guys

Thanks pludi and Praveen ....

Both the scripts are working.....

In pludi's solution i added just one word (POSU*) and it worked...

cnt=1
for file in `ls -ltr /appdb1/product/batch/rms/OUT/POSU*` ; do
mv ${file} ${file}.${cnt}
cnt=$(( cnt +1 ))
done

Thanks again for your help guys....
Hey I have one more question...altough this is totally diff from the topic...

How can we delete all the files except for the last 3 days files ????
# 5  
Old 02-06-2009
Code:
find <path> -type f -mtime +3 -exec rm '{}' ';'

Explanation: find all files (-type f) in <path>, not modified in the last 24*3 hours, and pass them as an argument to rm.
I suggest you try it with mv or -print first, to see if it captures those files you want, before feeding them straight to rm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

2. Shell Programming and Scripting

Windows Power Shell - rename files and move

hi people; i want to make a file/folder operation as follows. - i have 41 folders in Windows and each of them have same-named files (~200 files each) inside. - i want to stack these files together in a folder but Windows is asking to "overwrite" (as usual) since the file names are the same.... (2 Replies)
Discussion started by: gc_sw
2 Replies

3. Shell Programming and Scripting

Shell script to rename a group of files

Hello, I am having 1800 files in a directory with a specified format, like amms_850o_prod.000003uNy amms_850o_prod.000003u8x amms_850o_prod.000003taP amms_850o_prod.000003tKy amms_850o_prod.000003si4 amms_850o_prod.000003sTP amms_850o_prod.000003sBg amms_850o_prod.000003rvx... (12 Replies)
Discussion started by: atlantis
12 Replies

4. Shell Programming and Scripting

Need help for a Shell script to rename multiple files

Hi! I need help to create a shell script to search inside a file and then copy a portion of the search result as the new file name. Basically I was hacked over the weekend and the genius wipe out my drive from my server. I was able to recover alot of files, but biggest problem Is now the... (15 Replies)
Discussion started by: kidney514
15 Replies

5. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

6. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

7. Shell Programming and Scripting

How to Rename/Convert Files in Shell Scripting?

Hi All, I want to Rename/Convert all the .doc files in to .pdf format. I am using the following Script. But the final output is not proper. ########################################## cd /u13/prepaid/ftpdata/INfiles/sap/ for name in `ls *.doc` do name1=`echo $name | sed -e... (11 Replies)
Discussion started by: hanu_oracle
11 Replies

8. UNIX for Dummies Questions & Answers

How to delete files with certain ext?

Hi All, How can I work on following request? Delete all the html files older than 29th November from the path - /dding/ting/tong/unixyang/output (4 Replies)
Discussion started by: tonyvirk
4 Replies

9. Shell Programming and Scripting

rename file to file.ext.datetime

Hi, I need to rename a file like this to include date and time: Original File : error.log Date time: Sep 20, 2007 14:10:10 New File Name: error.log.20070920_1410 How can I get date and time stame and include it in mv command. Thanks in advance (2 Replies)
Discussion started by: tripsat
2 Replies

10. Shell Programming and Scripting

rename files using shell scripting

Hi all, i want to rename some files in my directory using korn shell scripting. 1) i want to rename files who have no extension so that they will have the format: filename.extension and 2) i want the files who has extension initially, to stay the same (they will not be... (4 Replies)
Discussion started by: gfhgfnhhn
4 Replies
Login or Register to Ask a Question