renaming files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming files
# 1  
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 '/usr/tom/1997/jan) like

ABC_1997_ST1_BCD.SQL to ABC_1997_ST2_BCD.SQL
(also change the any 1997_ST1 TO 1997_ST2 within the file as well)

BCD_1997_ST1_EFG_SAB.SQL to BCD_1997_ST2_EFG_SAB.SQL
(also change the any 1997_ST1 TO 1997_ST2 within the file as well)

TTT_EBC_1997_ST1_A.SQL to TTT_EBC_1997_ST2_A.SQL
(also change the any 1997_ST1 TO 1997_ST2 within the file as well)

i was able to change the file contents but not the filename.Also the folowing
script changes even the subdirectories which i do not want

list=`ls`
for i in $list
do
sed -n '/[sS][Tt]1/p' ${i}
done
# 2  
see this

Go to the 1997 directory.

Code:
cd /usr/tom/1997
for i in `find . \( ! -name . -prune \) -name "*.SQL"`
do
 fname=$(basename ${i})
 sed 's/^\(.*\)_[Ss][Tt]1_\(.*\)$/\1_ST2_\2/g' ${fname} >${fname}.tmp
 newfilename=$(echo $fname | sed 's/^\(.*\)_[Ss][Tt]1_\(.*\)$/\1_ST2_\2')
 mv ${fname}.tmp $newfilename
 rm -f $i
done

Hope this gives you some idea.
# 3  
Thanks for the reply

could you please explain to me what this means

. \( ! -name . -prune \)
# 4  
check this

Check this link - Non-recursive find
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #303
Difficulty: Easy
Normally sed is invoked like this: sed SCRIPT INPUTFILE ...
True or False?

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. UNIX for Dummies Questions & Answers

Help with renaming the files.

Hi, I have 31 file as below.... dt_ref_med_merged_madt_h_19980101.nc .. .. dt_ref_med_merged_madt_h_19980131.nc I want to rename it like... dt_ref_med_merged_madt_h_1998_1.nc .. .. dt_ref_med_merged_madt_h_1998_31.nc How can I do it ? any suggestions ? I want to do it small... (5 Replies)
Discussion started by: mahesh shinde
5 Replies

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

4. UNIX for Dummies Questions & Answers

renaming all files

requirement: There are many files inside a directory and all are named like this "out_1". The file names goes on itearation like out_2, out_3 etc.... Now i want to rename all the file names based on the text inside the file. the text in all the files follow a pattern like it satrts... (4 Replies)
Discussion started by: arunmanas
4 Replies

5. Shell Programming and Scripting

renaming files

Hello, I wanted to rename one file where filename contains space.. How can i rename in unix? The file name is ABC XYZ.TXT I wanted to rename this file as ABCXYZ.TXT. Any help is greatly appreciated... Regards. (4 Replies)
Discussion started by: govindts
4 Replies

6. UNIX for Dummies Questions & Answers

Need help renaming files

I just can't figure this one out. I have a lot of files name (for example) ABC1234.5678.ext I need to rename these files U0105678PQRS So I'm removing everything before the first "." I'm keeping "5678" in the file name Adding "U010" to the front of "5678" Dropping the ".ext" extension ... (5 Replies)
Discussion started by: bbbngowc
5 Replies

7. Shell Programming and Scripting

Renaming Files

Hi Alll, I have a script that we use on the servers to change the name of files that have spaces in the name: #!/bin/tcsh set n = 0 foreach f ( * ) echo $f | grep " " if ( $? == 0 ) then mv "$f" `echo $f | sed -e "s/ /_/g"` @ n += 1 endif end echo $n changed I need to write a... (2 Replies)
Discussion started by: abch624
2 Replies

8. UNIX for Dummies Questions & Answers

Renaming Files

Hi Alll, I have a script that we use on the servers to change the name of files that have spaces in the name: #!/bin/tcsh set n = 0 foreach f ( * ) echo $f | grep " " if ( $? == 0 ) then mv "$f" `echo $f | sed -e "s/ /_/g"` @ n += 1 endif end echo $n changed I need to write a... (2 Replies)
Discussion started by: abch624
2 Replies

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

10. UNIX for Dummies Questions & Answers

renaming files

Hello all- I need to rename files by adding an embedded 0 e.g. aaa_bbb_1234 needs to become aaa_bbb_01234 The aaa and 1234 will change but the bbb_ can be my anchor. TIA (9 Replies)
Discussion started by: ohagar
9 Replies

Featured Tech Videos