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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-15-2008
Djaunl Djaunl is offline
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
  #2 (permalink)  
Old 01-15-2008
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
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.
  #3 (permalink)  
Old 01-15-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
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
  #4 (permalink)  
Old 01-15-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
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
  #5 (permalink)  
Old 01-15-2008
Djaunl Djaunl is offline
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
  #6 (permalink)  
Old 01-15-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
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.
  #7 (permalink)  
Old 01-15-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
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).
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 10:50 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