LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 07: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, 07:58 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
#!/bin/bash
for name in *.xvid
do
    newname=${name%.xvid}
    echo mv "$name" "$newname"
done
 
Old 04-25-2010, 08: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, 08:18 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 08:46 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 09:09 AM   #8
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,904

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
No need for a script. There's a command for that:
Code:
rename '[xvid]' '' *.avi
'man rename' for details.
 
Old 04-25-2010, 09: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 03:42 AM.
 
Old 04-25-2010, 10:10 AM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,904

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:22 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