Renaming a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming a file
# 1  
Old 04-15-2009
Renaming a file

I am a complete Unix newbie and I need some help! (Please...)

I need to rename a file from the following format:
Test_Test_EAR_1234.ear

To the following:
Test_Test_EAR.ear

In other words, I need to remove everything after & including the final underscore up to the "." What is the best way to go about doing this? Any help would be greatly appreciated. Thanks!
# 2  
Old 04-15-2009
You can use sed to make the change, something like this:

namefilename=echo $filename | sed -e 's/_[^.]*(\.[^._]*)/$1/'`
# 3  
Old 04-15-2009
you mean you want to remove whatever present between last "_" and "." right???
this will help
Code:
mv Test_Test_EAR_1234.ear `echo Test_Test_EAR_1234.ear|sed 's/\(.*\)\(\_.*\)\(\..*\)/\1\3/g'`
mv anyfile `echo anyfile|sed 's/\(.*\)\(\_.*\)\(\..*\)/\1\3/g'`

# 4  
Old 04-15-2009
Thanks, CRGrathouse, but I need to do this via an automated Unix shell script. The list of files and the numbers I need to remove will be variable.
# 5  
Old 04-15-2009
vidyadhar85, the following works like a charm! Thank you SO much!

mv Test_Test_EAR_1234.ear `echo Test_Test_EAR_1234.ear|sed 's/\(.*\)\(\_.*\)\(\..*\)/\1\3/g'`

Don't suppose you could explain how the expression works?
# 6  
Old 04-15-2009
You may build using shell variable too, under "bash" or "ksh" they work the same.
Code:
X=Test_Test_EAR_1234.ear
XExt=${X#*.}
XNoExt=${X%.*}
XNewBody=${XNoExt%_*}
XNew=${XNewBody}.${XExt}
echo $X $XNoExt $XExt $XNewBody $XNew
unset XNoExt XExt XNewBody
echo mv $X $XNew

Just delete the "echo" on last line and you may change name to you file. The output for this command would be
Code:
Test_Test_EAR_1234.ear Test_Test_EAR_1234 ear Test_Test_EAR Test_Test_EAR.ear
mv Test_Test_EAR_1234.ear Test_Test_EAR.ear

Yes I know is a little big for you all guy may be used to. But this would be the way I would do it.

My best regards to all
# 7  
Old 04-16-2009
Code:
name="Test_Test_EAR_1234.ear"
echo ${name%_*}.ear

or a more applicable one:

Code:
IFS="\."
name="Test_Test_EAR_1234.ear"
set $name
echo ${1%_*}.$2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. UNIX for Dummies Questions & Answers

Renaming File

Hi there, I have 350 files in this directory: /home/adams/29 that was mistakenly renamed in this format: TTFILE_BIT_638478.txt.dat I want to take out the trailing .dat so that it ends in .txt: TTFILE_BIT_638478.txt I need help please. Thank you. (6 Replies)
Discussion started by: Creems
6 Replies

3. UNIX for Dummies Questions & Answers

Need help with Renaming a file

I have a file named as Pro_PatAct_MMDDYYYY.csv. I need to renmae it to this Pro_PatAct.csv without the date timestamp. Can someone help me to achieve this using a regular expn. (3 Replies)
Discussion started by: imran_affu
3 Replies

4. Shell Programming and Scripting

Renaming file

Hello, I have an outstanding issue..Iam on linux and using a putty to connect to my server and then fire our unix shell script. At location /usr/sam a file called "er 1 32.txt" out boss transfer via application. From my end on terminal when i want to transfer this file to some other location... (2 Replies)
Discussion started by: j_panky
2 Replies

5. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

6. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

7. Shell Programming and Scripting

renaming file

Dear Friends, Need your help once again. I have this file name e.g.1) report_12.rp_1 e.g.2) remark_mm.rmr_3 I want it to be renamed as report_12_1.rp remark_mm_3.rmr (3 Replies)
Discussion started by: anushree.a
3 Replies

8. UNIX for Dummies Questions & Answers

Help in renaming file !!!

Hi All, I want to rename a file inside a script which has a date portion appended at the start of the file name. The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too. #!/usr/bin/ksh cd /space/file/source dt=$(date "date "+%m%d%Y")... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

9. Shell Programming and Scripting

Renaming a file name

I have an audit log that is produced each day from a production printer. It names the file using todays date, but it removes the leading zero's. For example: todays date 060104 names the file 6104.txt. I ftp this file onto a Sun box and pull stats off of it. To keep some consistency to what I am... (2 Replies)
Discussion started by: dbrundrett
2 Replies

10. UNIX for Dummies Questions & Answers

Renaming a file to the same name

Hi All, I was wondering if anyone knows how i take in a file, format the file, and then rename the file as the same name as the input file in shell script. Here is an example of what I am doing: if ] then echo "please enter a filename" else export infile=$1 I take in a file... (3 Replies)
Discussion started by: lachino8
3 Replies
Login or Register to Ask a Question