LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-27-2008, 09:50 PM   #1
mk6032
LQ Newbie
 
Registered: Oct 2004
Location: Chattanooga, TN
Distribution: rhel, ubuntu, opensuse
Posts: 13

Rep: Reputation: 0
mass mp3 rename using tr


I found a handy trick on the web to replace spaces with underscores and to change from upper to lowercase:
Code:
for files in *.mp3; do mv "$files" `echo $files | tr ' ' '_'`; done
for files in *.mp3; do mv "$files" `echo $files | tr '[:upper:]' '[:lower:]'`; done
This works great, but only in the directory it's run from. How can I have it search subdirectories recursively?

--edit-- 4/28
bruenig on freenode #linux gave me this tip:

Code:
IFS=$'\n' ; files=( $(find . -type f -name '*.mp3') ) ; for thing in 
"${files[@]}"; do DIR="${thing%/*}" ; FILETMP="${thing##*/}" ; 
FILE="${FILETMP// /_}" ; mv "$thing" "$DIR/$FILE"; done

Last edited by mk6032; 04-27-2008 at 11:38 PM. Reason: answer
 
Old 04-28-2008, 02:50 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by mk6032 View Post
I found a handy trick on the web to replace spaces with underscores and to change from upper to lowercase:
Code:
for files in *.mp3; do mv "$files" `echo $files | tr ' ' '_'`; done
for files in *.mp3; do mv "$files" `echo $files | tr '[:upper:]' '[:lower:]'`; done
This works great, but only in the directory it's run from. How can I have it search subdirectories recursively?

--edit-- 4/28
bruenig on freenode #linux gave me this tip:

Code:
IFS=$'\n' ; files=( $(find . -type f -name '*.mp3') ) ; for thing in 
"${files[@]}"; do DIR="${thing%/*}" ; FILETMP="${thing##*/}" ; 
FILE="${FILETMP// /_}" ; mv "$thing" "$DIR/$FILE"; done
While I don't know if this works in bash ( and I'm too lazy to test it for you ), but in tcsh, I would do something like this:

Code:
for files in `find . -type f | grep 'mp3$'`; do mv "$files" `echo $files | tr ' ' '_'`; done
... which, I believe, still works in bash, but again... haven't tested. Seems a little more clean than the last example you posted.
 
Old 04-28-2008, 06:03 PM   #3
bombpop
LQ Newbie
 
Registered: Jun 2004
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by BrianK View Post
While I don't know if this works in bash ( and I'm too lazy to test it for you ), but in tcsh, I would do something like this:

Code:
for files in `find . -type f | grep 'mp3$'`; do mv "$files" `echo $files | tr ' ' '_'`; done
... which, I believe, still works in bash, but again... haven't tested. Seems a little more clean than the last example you posted.
That doesn't quite work in Bash.

This works for me:
Code:
find . -type f -name '*.mp3' | while read file; do mv "${file}" "${file%/*}"/`echo "${file##*/}" | tr ' ' '_'`; done

Last edited by bombpop; 04-28-2008 at 06:05 PM.
 
  


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
mass rename files cope Programming 4 10-28-2007 11:33 AM
mass mp3 directory renaming schbond Linux - General 4 01-05-2007 11:18 AM
BASH USAGE: mass files rename sirpelidor Linux - General 8 09-20-2005 12:36 AM
program to rename mp3 automatically? buffed317 Linux - Software 1 02-22-2005 07:48 PM
Mass Rename wickdgin Linux - Newbie 2 04-13-2003 02:29 PM


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