removing the extension from all filenames in a folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers removing the extension from all filenames in a folder
# 1  
Old 05-21-2008
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 .xls, for example, extension of the files without changing the rest of the filename. How do I do this please?

Apologies if this does appear elsewhere in this forum and thanks in advance for any help.
# 2  
Old 05-21-2008
This should gives you the commands to do the job:

Code:
ls|sed 's/\(.*\).xls/mv & \1/'

If you get the desired move commands you can pipe the output to sh to move the files:

Code:
ls|sed 's/\(.*\).xls/mv & \1/'| sh

Regards
# 3  
Old 05-21-2008
Thanks for the response. Worked beautifully.

Any chance you have the time to explain exactly what the command you provided did/meant? Obviously I know what the outcome was but if you have time to explain all the parameters that'd be great. Don't worry if you don't, I'm sure, with time, I'll be able to figure it out for myself.

Once again, thanks for the swift response Smilie
# 4  
Old 05-21-2008
Hammer & Screwdriver another approach

Here is another approach I scripted - part of a bigger program.
It actually copies files called dm12345678_DAT to dm123456.dat, so for you:
(a) exclude the .dat from the copy
(b) use a move instead of copy to rename [I wanted to keep the original file just in case.]
(c) could eliminate the tailprg which was merely to use the first six characters of a very long numeric string

But, my example shows a couple other things you can accomplish while manipulating filenames.

Code:
#!/usr/bin/bash
tailprg="/usr/local/bin/tail" #force version of tail command

echo "Shortening the filenames"
for rf in *_DAT; do
   echo "Creating .dat for $rf"
   tf=dm`basename $rf _DAT | $tailprg -c6`.dat
   cp $rf $tf
done

# 5  
Old 05-21-2008
s -> substitute command

\(.*\).xls -> This saves the pattern before the dot (filename without extension) which is recalled with \1.

The & gives the original line (filename with extension) so "mv & \1" prints:

mv "the filename with extension" "filename without the extension"

A useful link with some tutorials:

https://www.unix.com/answers-frequent...tutorials.html

Hope this helps.

Regards

Last edited by Franklin52; 05-21-2008 at 01:56 PM..
# 6  
Old 05-21-2008
Brilliant!

Thanks so much for all the help and guidance. One day I'll get the hang of this UNIX thing! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
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 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 file.test1.f-1.fig.eps to be file_test1_f-1_fig.eps Using awk, the following line will replace ALL periods with underscores, but I want to leave the... (2 Replies)
Discussion started by: erinbot
2 Replies

4. Shell Programming and Scripting

removing the filename extension

Is there an easy way to strip off a filename's extension? For example, here's a filename: blahblahblah.thisisok.thisisnotok I want to get rid of .thisisnotok from the filename, so that what's left is blahblahblah.thisisok Thanks. I have a directory full of filenames that need to be... (5 Replies)
Discussion started by: daflore
5 Replies

5. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

6. Shell Programming and Scripting

copy all files with the same filenames as those in another folder

Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. All image files in folder A are able to find their corresponding description files in folder B. That is to say,... (3 Replies)
Discussion started by: jiapei100
3 Replies

7. Shell Programming and Scripting

SED: Removing Filenames From Paths

I'm using a script with a lot of SED commands, in conjunction with grep, cut, etc. I've come up against a wall with a particular road block: I output a file from an SVN registry that gives me a list of files. The list consists of a variable number of lines that contain a path/file. The paths... (4 Replies)
Discussion started by: Brusimm
4 Replies

8. Shell Programming and Scripting

Count matching filenames in a folder

Hi all, I have 4 files for example named abc01012009.txt abc02012009.txt abc03012009.txt abc04012009.txt in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the... (4 Replies)
Discussion started by: tententen
4 Replies

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

10. 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
Login or Register to Ask a Question