LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   how do i remove a word from multiple file names? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-remove-a-word-from-multiple-file-names-774625/)

steve51184 12-09-2009 10:56 PM

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

ramram29 12-09-2009 11:04 PM

for files in `ls *.rar`; do mv $files `echo $files | sed s/oldword/newword/g`; done

steve51184 12-09-2009 11:09 PM

perfect thanks for the quick reply :)

ghostdog74 12-09-2009 11:15 PM

no need to use external commands. with bash
Code:

for file in *.rar; do echo mv "$file" "${file/Word/NewWord}"; done

steve51184 12-09-2009 11:20 PM

Quote:

Originally Posted by ghostdog74 (Post 3786176)
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?

ghostdog74 12-09-2009 11:30 PM

Quote:

Originally Posted by steve51184 (Post 3786178)
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

GrapefruiTgirl 12-10-2009 12:00 AM

Quote:

Originally Posted by ghostdog74 (Post 3786176)
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

evo2 12-10-2009 12:09 AM

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.


All times are GMT -5. The time now is 05:49 AM.