change filenames but not extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change filenames but not extension
# 1  
Old 02-06-2012
change filenames but not extension

I have a filename with a bunch of periods that I want to replace with underscores, but I don't want to change the extension.

Ex: I want
Code:
file.test1.f-1.fig.eps

to be
Code:
file_test1_f-1_fig.eps

Using awk, the following line will replace ALL periods with underscores, but I want to leave the last period for the extension
Code:
ls *.eps -print|awk '{f=$0;gsub(/\./,"_");print "mv "f" "$0}'|sh


Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 02-06-2012 at 06:53 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-06-2012
Code:
ls -1 *.eps | while read line; do name=`basename $line .eps`; name=$(echo $name | sed 's/\./_/g'); mv $line $name.eps; done

ls -1 ( numeric one )
# 3  
Old 02-06-2012
Pure shell approach, if you are using bash or ksh:
Code:
ls -1 *.eps | while read file ; do
  base=${file%.*}
  suff=${file##*.}
  echo mv $file ${base//./_}.${suff}
done

For explanation see 'man bash' and scroll to "Parameter expansion"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change filenames recursively

Hello, I made a mistake in a script and now need to go back and change allot of filenames. I need to change "v4" in filenames to "v3". I was thinking of something like this. #!/bin/bash FILELIST=$(ls -f -R *) for FILE in $FILELIST do # create new filename ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

How to remove filenames having the same extension.?

hi, i have a directory which contains some files and a subdirectory. i am writing only the files names to a file using the below code. ls -ltr | grep "^-" | awk '{print $NF}' > /home/file_list$$ cat /home/file_list$$ s1_abc.txt s2_def.xls s3_def.xls as you can see there is one .txt... (7 Replies)
Discussion started by: Little
7 Replies

3. Shell Programming and Scripting

Change all filenames under different folders...

Hi, all: I'd love to use shell script to change all filenames under different folders once for all: I've got over 100 folders, in each of them, there is a file named "a.ppm". I wanna change all these "a.ppm" to "b.ppm", and still . Visually, the directory structure looks like: and hope... (1 Reply)
Discussion started by: jiapei100
1 Replies

4. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

5. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

6. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies

7. Shell Programming and Scripting

Reading filenames with extension .xml

Hi, I want to write a script to read all the filenames with extension .xml in a directory and pass the name of the file, one by one, to another function. Please help me out. Regards. Saurabh (3 Replies)
Discussion started by: bhalotias
3 Replies

8. Shell Programming and Scripting

how do i change extension

Hi I am writing a script which does an FTP of a set of files onto another machine and then would have to rename the files into a different extension on the source machine. for example if the file being sent via FTP is sample.txt. Once the file has been transferred i would want to modify the... (2 Replies)
Discussion started by: kswaraj
2 Replies

9. UNIX for Dummies Questions & Answers

Adding an extension to a group of filenames

Hi - I'm stuck. I have a group of text files created using the split command. My files have the names "projectaa", "projectab", "projectac", etc. What I want to do is add the extension ".txt" to each file. I think I've got part of a sed command together, but I'm stuck on my regex - I keep getting... (9 Replies)
Discussion started by: pepintheshort
9 Replies

10. Shell Programming and Scripting

How to change extension?

How do you write a shell script that change the extension of all the files? e.g chext rtf doc where .rtf is the original extension and .doc is the new extension is it something to do with basename? do I need a for loop? Please help! Unix SuperNewbie (4 Replies)
Discussion started by: prkwan
4 Replies
Login or Register to Ask a Question