LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-09-2009, 10:56 PM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Rep: Reputation: 30
how do i remove a word from multiple file names?


hey all lets say i have some files with names like these:

Quote:
file.WordiWantToRemove.part1.rar
file.WordiWantToRemove.part2.rar
file.WordiWantToRemove.part3.rar
how would i go about removing the 'word' in the file names (or replace it) this is what i have so far but the quotes are wrong... ???

Code:
for files in *.rar; do mv “$files” `echo $files | tr ‘oldword‘ ‘newword’`; done

Last edited by steve51184; 12-09-2009 at 10:58 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-09-2009, 11:04 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
for files in `ls *.rar`; do mv $files `echo $files | sed s/oldword/newword/g`; done
 
0 members found this post helpful.
Old 12-09-2009, 11:09 PM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
perfect thanks for the quick reply
 
Old 12-09-2009, 11:15 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
no need to use external commands. with bash
Code:
for file in *.rar; do echo mv "$file" "${file/Word/NewWord}"; done
 
2 members found this post helpful.
Old 12-09-2009, 11:20 PM   #5
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
no need to use external commands. with bash
Code:
for file in *.rar; do echo mv "$file" "${file/Word/NewWord}"; done
that works just as well without the word 'echo' can you tell me why/if this is better then ramram29 method?
 
Old 12-09-2009, 11:30 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by steve51184 View Post
that works just as well without the word 'echo' can you tell me why/if this is better then ramram29 method?
Quote:
Originally Posted by ramram29
Code:
for files in `ls *.rar`
  do mv $files `echo $files | sed s/oldword/newword/g`
done
1) useless use of ls in the for loop. Also, it breaks on files with spaces. Use shell expansion instead.
2) using "|" and calling external commands creates extra overheads. The shell provides simple substitution, so try to use it whenever possible
 
1 members found this post helpful.
Old 12-10-2009, 12:00 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by ghostdog74 View Post
no need to use external commands. with bash
Code:
for file in *.rar; do echo mv "$file" "${file/Word/NewWord}"; done
I have always done this with sed, like ramram did above. Yet again, something new learned today! And another reason bash is good

Thanks Ghostdog,

Sasha
 
Old 12-10-2009, 12:09 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
You have many answers, so I just thought I'd point out that the primary problem with your original attempt is that tr only replaces single characters.

Evo2.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i convert file names to lower case and remove the underscore from the name? asilentmurmur Linux - Newbie 16 10-30-2012 06:52 PM
Remove whitespace in file and directory names using bash TheFutonEng Programming 14 02-26-2012 12:35 PM
HELP! Script to RENAME/REMOVE ( ) From File NAMES!! xberetta21 Linux - Newbie 4 01-29-2008 01:10 PM
remove * from file nameS zchoyt Linux - Newbie 1 02-13-2005 04:33 PM
Need to remove spaces from all file / dir names ?? Solution here! jsjohnst Linux - General 2 07-20-2004 09:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration