LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2010, 08:44 AM   #1
berrance
Member
 
Registered: Aug 2004
Location: Hull - England
Distribution: Ubunto and slowly switching to debian
Posts: 308

Rep: Reputation: 30
replacing part of filename


I have just re transcoded a bunch of avi's. to tell the new ones from the old ones I put '[xvid]' at the end of all the new avi's. but now I have deleated the old avi's I want to remove the [xvid] part of the file name.

This is what I have so far

Code:
#!/bin/bash
for name in *.avi
do
newname=`echo "$name" | tr -d [xvid]`
echo "$name" "$newname"
mv "$name" "$newname"
done
##########
but that just removes all the leters X, V, I & D from the file name and doesnt actualy remove the string '[xvid]' from the file name.

any point in the wright direction would be helpfull

Thanks Berrance
 
Old 04-25-2010, 08:58 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"[xvid]" in fact means any one of the four characters. Try it without the brackets
 
Old 04-25-2010, 08:59 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Code:
#!/bin/bash
for name in *.xvid
do
    newname=${name%.xvid}
    echo mv "$name" "$newname"
done
 
Old 04-25-2010, 09:07 AM   #4
berrance
Member
 
Registered: Aug 2004
Location: Hull - England
Distribution: Ubunto and slowly switching to debian
Posts: 308

Original Poster
Rep: Reputation: 30
The [ & ] are part of the file name. the file names are like

"video [xvid].avi"

I wand the square brackets removing aswell. I want to remove the " [xvid]" from every file name so the new file name is

"video.avi"
 
Old 04-25-2010, 09:18 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I think you'll need to "escape" the brackets---eg \[xvid\]
 
Old 04-25-2010, 09:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Generally if you give a script that is not working it always helps to provide what your starting with and
how you want it to look. Are we to assume that the file names may contains spaces as in your example:
Quote:
"video [xvid].avi"
Because your output does not only remove the desired string of "[xvid]" but also the prior space.

Assuming we can ignore the space, the following, which builds on catkin's approach, should work:
Code:
#!/bin/bash
for name in *.avi
do
    newname=${name/\[xvid\]/}
    echo mv "$name" "$newname"
done
 
Old 04-25-2010, 09:46 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by berrance View Post
The [ & ] are part of the file name. the file names are like

"video [xvid].avi"

I wand the square brackets removing aswell. I want to remove the " [xvid]" from every file name so the new file name is

"video.avi"
Code:
#!/bin/bash
for name in *\[xvid\].avi
do
    newname=${name/\[xvid\]/}
    echo mv "$name" "$newname"
done
 
Old 04-25-2010, 10:09 AM   #8
GazL
Senior Member
 
Registered: May 2008
Posts: 4,087
Blog Entries: 1

Rep: Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532
No need for a script. There's a command for that:
Code:
rename '[xvid]' '' *.avi
'man rename' for details.
 
Old 04-25-2010, 10:57 AM   #9
berrance
Member
 
Registered: Aug 2004
Location: Hull - England
Distribution: Ubunto and slowly switching to debian
Posts: 308

Original Poster
Rep: Reputation: 30
Code:
rename 's/\ [xvid]//' *.avi
Is the way to go.

Thanks for the help guys

Berrance

Last edited by berrance; 04-26-2010 at 04:42 AM.
 
Old 04-25-2010, 11:10 AM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 4,087
Blog Entries: 1

Rep: Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532Reputation: 1532
Hmm, that looks more like a sed/regex. Looks like you have a different version of 'rename' to me.
The slackware one uses this syntax http://linux.die.net/man/1/rename
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Extracting part of a filename wtaicken Programming 7 01-02-2009 09:01 AM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 07:47 AM
renaming files recursively by replacing a character in filename etechnophile Programming 3 05-05-2007 12:26 AM
Replacing a image filename with a regex pattern in a script jonaster Linux - General 1 09-05-2006 08:02 PM
put date as part of filename box_l Programming 3 07-27-2004 07:17 AM


All times are GMT -5. The time now is 01:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration