The UNIX and Linux Forums  

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
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 12:59 PM
removing the extension from all filenames in a folder johnmcclintock UNIX for Dummies Questions & Answers 5 05-21-2008 05:23 AM
Searching filenames containing certain text??? skyineyes UNIX for Advanced & Expert Users 6 01-16-2008 04:48 AM
Renaming of multiple filenames shashi_kiran_v UNIX for Dummies Questions & Answers 4 07-11-2005 04:57 AM
Renaming of multiple filenames shashi_kiran_v UNIX for Dummies Questions & Answers 7 07-05-2005 05:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-15-2008
Registered User
 

Join Date: Jul 2006
Posts: 13
Removing certain text from multiple filenames

Hi,

I want to remove a ".ff" from multiple filenames. Searching google and these forums brought me close, but no cigar. I know I can use sed to replace what I want, but I'm unsure how to input the filename (filename, not the stuff in the actual file) to the command.

For example, I know how to do something simple like:

echo filename.ff.txt | sed s/.ff//

which displays what i want.

How though would I actually do this?

Thank you very much,
Dan
Reply With Quote
Forum Sponsor
  #2  
Old 01-15-2008
Registered User
 

Join Date: Oct 2007
Posts: 70
I'm a beginner in UNIX. So I can do this in 6 steps.

1) ls -l | awk '{print $9}' > getfilenames1.txt
2) cp filenames1.txt filenames2.txt
3) vi filenames2.txt
i) :%s/ff.//g
4) vi filenames1.txt
i) :%s/^/mv /g # note blant after mv
5) paste -d" " filenames1.txt filenames2.txt > filename3
6) run filename3.

I know it can be done in one command at the prompt. But as a beginner, I have to try.
Reply With Quote
  #3  
Old 01-15-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Post

Code:
ls -1 *.ff* | while read filename ; do mv $filename `echo $filename | sed 's/\.ff//'` ;done
Not actually tested so maybe start by running this using 'echo' in place of 'mv'

Could be done using a 'for' loop too:
Code:
for filename in *.ff* ; do ...
But that would throw a wobbly on filenames with spaces in them
Reply With Quote
  #4  
Old 01-15-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Quote:
Originally Posted by bobbygsk View Post
I'm a beginner in UNIX. So I can do this in 6 steps.

1) ls -l | awk '{print $9}' > getfilenames1.txt
2) cp filenames1.txt filenames2.txt
3) vi filenames2.txt
i) :%s/ff.//g
4) vi filenames1.txt
i) :%s/^/mv /g # note blant after mv
5) paste -d" " filenames1.txt filenames2.txt > filename3
6) run filename3.

I know it can be done in one command at the prompt. But as a beginner, I have to try.
This looks like it would do the job after a few typos are fixed ), and it's safe in that you can look at the contents of filename3 to see if it's going to do what you would expect. Although if you use 'ls -1' instead of 'ls -l', then you don't need to use awk to filter out all the other junk but the filename (saves CPU).
Reply With Quote
  #5  
Old 01-15-2008
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,636
Quote:
Originally Posted by Smiling Dragon View Post
[code]
Code:
for filename in *.ff* ; do ...
But that would throw a wobbly on filenames with spaces in them
Actually, it won't. That would work fine even for files with a space.

What would break is if the list of files was in a varible, and this also avouid the fork and exec needed for the other option, additionally you don't need -1 when piping the output of ls as it will output that way anyway when writing to a pipe.

If you are using bash or ksh93 you could do this:
Code:
for file in *.ff.* ; do
    mv "$file" "${file/.ff/}"
done
Reply With Quote
  #6  
Old 01-15-2008
Registered User
 

Join Date: Jul 2006
Posts: 13
Thank you very much for the informative replies guys. I used the last one to do the job, as it was quick. I will definitely study sed and awk more though; they seem incredibly useful.

@Smiling Dragon: I take great pains to make sure I never have any spaces in my filenames
Reply With Quote
  #7  
Old 01-15-2008
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,636
Quote:
Originally Posted by Djaunl View Post
I take great pains to make sure I never have any spaces in my filenames
Hope for the best and plan for the worst, it's always a sensible approach for things like filenames.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0