shell script to change the extension of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to change the extension of a file
# 1  
Old 09-25-2012
shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example
Code:
file1.new.new.new
file2.new.new.new
file3.new.new.new
file4.new.new.new

i want to write a shell script that rename all such file with only single extension like
Code:
file1.new
file2.new
...

need help on this
# 2  
Old 09-25-2012
# 3  
Old 09-28-2012
Hi
it isn't working for me, below is one of the sample file i have
Code:
example.new.new.new.new.new

Expected output
Code:
example.new

I have tried using below script
Code:
IFS="."
for i in *.new*
do
     set -- "$i"
     mv "$i" "$1.$2"
done

Have also tried below
Code:
#!/bin/bash
for i in *.new*
do
     mv "$i" "$(basename "$i".new)"
done

# 4  
Old 09-28-2012
Code:
 
for i in *.new*
do
filename=$(echo "${i}" | cut -d. -f1)
ext=$(echo "${i}" | cut -d. -f2)
mv "$i" "${filename}.${ext}"
done

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 09-28-2012
Another option:
Code:
for f in *.new
do
  mv "$f" "${f%%.new.*}.new"
done


or try the general case and pick the leftmost extension of files that have two or more extensions:
Code:
for f in *.*.*
do
  mv "$f" "${f%.${f#*.*.}}"
done


Last edited by Scrutinizer; 09-28-2012 at 07:04 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 09-28-2012
Hi,

just tried this
Code:
[root@centos5 test]# ls -1 | awk  -F"." '{ print $0,$1".new" }' | xargs -n2 echo  mv
mv file10.new.new.new file10.new
mv file1.new.new.new file1.new
mv file2.new.new.new file2.new
mv file3.new.new.new file3.new
mv file4.new.new.new file4.new
mv file5.new.new.new file5.new
mv file6.new.new.new file6.new
mv file7.new.new.new file7.new
mv file8.new.new.new file8.new
mv file9.new.new.new file9.new

Note:- remove echo after checking..&& run above code in that directory where file exists
# 7  
Old 10-04-2012
hi scrutinizer,
thanks for your post, could you please explain me the meaning of this line :
Code:
mv "$f" "${f%%.new.*}.new"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script change new format on the file.

Hi---Is there's way can write small shell script or perl script open "abc.txt" file and create new "new_abc.txt" file with format output below? Thanks cat abc.txt ###########################Readme############################### Contained with this README.TXT file are all of the file... (7 Replies)
Discussion started by: dotran
7 Replies

2. Shell Programming and Scripting

Change file extension

Hi Guys, i am trying to redirect a file wherein i need to change the extension of the file from .sh to .tmp, but getting an error a=test.txt sh test.txt > path/$(basename "$a" .sh).tmp i need test.tmp ---------- Post updated at 02:09 AM ---------- Previous update was at... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

3. Shell Programming and Scripting

Finding file extension on C Shell script

I very new to C Shell. I am trying to do is read from Command line. Find the if the file is zip, .txt, symbloic link,pipe, unknow (if file is not zip, txt, sy....) here is what I what got so far. I am very stuck atm Please help me out : If the file is symblooc link what file is link to ... (12 Replies)
Discussion started by: madbull41
12 Replies

4. Shell Programming and Scripting

Need Help:Shell Script for Solaris to change the dates in a file by one week

I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error: /var/tmp\n\r-> ./script.ksh date: illegal option -- d... (3 Replies)
Discussion started by: sriramanaramoju
3 Replies

5. Shell Programming and Scripting

Change value in a file using perl or shell script

hi, I have a local.conf file which has the first line TOPDIR = "/home/mvdev/workspace/boxer". I want to replace the value to "/home/common/workspace/mirror". I tried the following perl command that is perl -p -i -e 's/Path/path1/g' myfile.txt then sed... (7 Replies)
Discussion started by: amvarma77
7 Replies

6. Shell Programming and Scripting

Help with shell script for know when a file change it

Hi, IŽd like to know how to program a shell script for know when a file changes and based on that make another tasks all this in real time.. Thanks (2 Replies)
Discussion started by: mrios7
2 Replies

7. Shell Programming and Scripting

Change a line in a php file thanks to a shell script

Hi, I'm working on a script to make automatic the new releases of my website... However in this script I put all the css script in a single one. There's no rpoblem for that. My problem is when I want to change the header of my layout page to put instead of : $header.="<link rel=\"stylesheet\"... (2 Replies)
Discussion started by: lahabana
2 Replies

8. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

9. Homework & Coursework Questions

Create file and then change the extension case.

Interpreter should be bash. 1. The problem statement, all variables and given/known data: I need to make a file (myText.txt or song.mp3 or cloud.tar.gz or whatever) and then change the extension to (myText.TXT , song.MP3, cloud.TAR.GZ). It would be good if I can add all information in... (4 Replies)
Discussion started by: Kdenmen
4 Replies

10. Shell Programming and Scripting

change file extension from root and subdirectories

Hello, my first post! I'd appreciate help with this script, I'm new to this. I have a media directory where I want to batch convert image file names from .img to .iso. I've tried but get: $ ./img2iso2.sh ./img2iso2.sh: line 13: syntax error: unexpected end of file :( This is my... (10 Replies)
Discussion started by: Astrid
10 Replies
Login or Register to Ask a Question