LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-10-2005, 10:26 AM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
Multiple File Rename


Is there a way I can do a rename of a large number of file at once? For example:

I want to rename (technically replace) any (, ), [, or ] in the file name of my mp3's. My script has an issue with those in the file name, and I would like to replace the ( or [ with "- " and just remove the ] or ). Is that something that can be done, or should I continue to do it by hand?

Thanks,
Adam Windisch
 
Old 11-10-2005, 10:33 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Can you show an example of what they look like?
 
Old 11-10-2005, 10:40 AM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
unfortunately they all have a different look, but for the most part it's "Artist - Title.mp3" sometime a title will be in there. Most of the songs that I need to rename have the brackets or parentheses at the end before .mp3.
 
Old 11-10-2005, 10:53 AM   #4
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
If this is anything that takes more than 5 minutes of work for anyone on this forum to figure out, then I don't want to bother anyone. I just wanted to see if there was an easy way to do this. If not, I can change them manually.
 
Old 11-10-2005, 11:27 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Maybe something like this. If it works ok, you can remove the echo from
echo mv -v "$dir/$i" "$dir/$j" 2> /dev/null
Code:
#!/bin/bash
#Example: ./test /home/images
usage()
{
   echo "Usage: $0 Directory_Name"
   exit 1;
}

test -d "$1" || usage
dir="$1"

ls $dir | \
while read i; do
    j=`echo "$i" | tr -d '[' | tr -d ']' | tr ',' '-'`
    echo mv -v "$dir/$i" "$dir/$j" 2> /dev/null
done
 
Old 11-10-2005, 11:51 AM   #6
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Nice script homey, could you please also explain it to benefit others. I for one, would love to know what part is sent to /dev/null, obviously it is the brackets and stuff but how
 
Old 11-10-2005, 11:59 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
The 2> /dev/null part is just to get rid of noise when you have a file name doesn't get changed. You get a message saying the old file and new file are the same.

This is the part that gets rid of brackets and replaces ( , ) with ( - )
tr -d '[' | tr -d ']' | tr ',' '-'
 
Old 11-10-2005, 12:02 PM   #8
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Sorry to bug again but why couldn't the noise be directed with ">". What is the need for the 2?

Thanks for explaining.
 
Old 11-10-2005, 12:11 PM   #9
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
That replaces , with -? I think I typed my request incorectly. I would like ( and [ to be replaced by - and ) and ] to be removed. Sorry about the confusion.
 
Old 11-10-2005, 12:13 PM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
The message is an stderr so that is redirected to /dev/null
If you don't want to see what the mv command is doing, remove the -v

Output redirection:
0 = stdin input device such as keyboard
1 = stdout output device usually the console
2 = stderr error message usually displayed at the console
Example: ls nofile > test 2>&1 This sends the error message to a file called test.
Example: ls | tee test This sends output to the screen and to a file called test.
 
Old 11-10-2005, 12:17 PM   #11
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
*linuxlala stands up and does the salami act for homey
salami salami salami

Thanks buddy.

Cheers.
 
Old 11-10-2005, 12:19 PM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
That replaces , with -? I think I typed my request incorectly. I would like ( and [ to be replaced by - and ) and ] to be removed. Sorry about the confusion.
Like this?
Code:
#!/bin/bash
#Example: ./test /home/images
usage()
{
   echo "Usage: $0 Directory_Name"
   exit 1;
}

test -d "$1" || usage
dir="$1"

ls $dir | \
while read i; do
    j=`echo "$i" | tr -d ')' | tr -d ']' | tr '(' '-' | tr '[' '-'`
    echo mv -v "$dir/$i" "$dir/$j" 2> /dev/null
done
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
How can i Rename file? Khmer Linux - Newbie 4 10-21-2005 08:09 AM
Multiple file rename jrdioko Linux - Newbie 10 11-07-2004 05:25 PM
How do I rename a file? Thaidog Linux - Newbie 5 10-10-2003 10:28 PM
How to rename a file? juanb Linux - General 4 03-19-2003 11:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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