The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
removing particular lines ending with a .cnt extension in a text file ramky79 SUN Solaris 2 03-03-2008 05:17 AM
Reading filenames with extension .xml bhalotias Shell Programming and Scripting 3 01-21-2008 12:02 PM
Removing certain text from multiple filenames Djaunl UNIX for Dummies Questions & Answers 6 01-15-2008 05:52 PM
Removing the extension and FTP the files pradkumar Shell Programming and Scripting 1 02-14-2007 02:01 PM
Adding an extension to a group of filenames pepintheshort UNIX for Dummies Questions & Answers 9 04-09-2004 09:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-21-2008
johnmcclintock johnmcclintock is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
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 (permalink)  
Old 05-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
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 (permalink)  
Old 05-21-2008
johnmcclintock johnmcclintock is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
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
  #4 (permalink)  
Old 05-21-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 05-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
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:

Unix Tutorials/Programming Tutorials/Shell Scripting Tutorials

Hope this helps.

Regards

Last edited by Franklin52; 05-21-2008 at 01:56 PM..
  #6 (permalink)  
Old 05-21-2008
johnmcclintock johnmcclintock is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Brilliant!

Thanks so much for all the help and guidance. One day I'll get the hang of this UNIX thing!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0