Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-04-2005, 06:55 PM   #1
LQ Newbie
Registered: Jan 2005
Posts: 5

Rep: Reputation: 0
Script to rename directories


I hope someone can help. I would like a script that renames directories, like this:

Old: this is A tEsT DiR
New: This_Is_A_Test_Dir

So uppecase letters after every underscore, and replace spaces to underscores.

Hope it's possible :)

Old 01-04-2005, 07:00 PM   #2
Registered: Oct 2003
Distribution: Gentoo rules them all
Posts: 279

Rep: Reputation: 31
Trying to cheat out of your programming assignment, very nice

Hint: it will be much easier to do with a mix of perl (or C) and shell scipting. use shell to get the list of directories then perl or C to crunch the data into what you want the output to be, then run mv command.

Krename might be able to do it.

Also this:

Last edited by Mikhail_16; 01-04-2005 at 07:03 PM.
Old 01-04-2005, 07:03 PM   #3
LQ Newbie
Registered: Jan 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Hah. It did look like that didn't it. No i'm not in school so..but i do try to get my MP3 dir in order, and Easytag don't do the DIRS just the MP3 files.

Oh thanks dude. Nice

Last edited by superior88; 01-04-2005 at 07:05 PM.
Old 01-06-2005, 08:26 AM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Here is a sed script that might do the replacements you want.
jims@matrix:~> cat sedscr
s/ /_/g
jims@matrix:~> cat tdlist
tHis is a tEST DIRectORY
this  is another test DIRectory
  yet another test DIRECtory
  ADDED LATER TO SEE IF A MI3NUmBER will throw it off
jims@matrix:~> sed -f sedscr tdlist
This was a fun problem. Working with sed is often an iterative process. I had added the second line after I saw that the first character wasn't being changed. Then I had an idea to insert a letter, and had to change a [[:alpha:]] to [[:alnum:]].

Sed works by reading a line of the file or pipe input, and then cycling through each instruction of the script before reading the next line and starting the cycle over. So the order of the sed commands are often important.

Last edited by jschiwal; 01-06-2005 at 08:49 AM.
Old 02-15-2005, 11:31 PM   #5
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
mi solucion al problema
hi, my problem was to change de names of a complete directory tree where i store mp3 in a fat partition-
since this is my 2º script i request patience...
the first was to play them in a console.
so. here it is:
root@juan:/mnt/windows/mp3# cat /usr/local/bin/cambiar

find ./ -maxdepth "$i" -mindepth "$i" |
while read; do
echo "iteracion $i"
find ./ -type d -maxdepth "$i" -mindepth "$i" > /usr/local/bin/nombre
i=$(($i + 1))

note:____ "iteracion $i" tells u which level of the tree is being afected.

root@juan:/mnt/windows/mp3# cat /usr/local/bin/cambiazo

replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1
#replace '_' ' ' < /usr/local/bin/nombre > /usr/local/bin/nombre1
cat -n /usr/local/bin/nombre > /usr/local/bin/nombre2
for j in `cut -c 2-6 /usr/local/bin/nombre2`; do
nombreviejo=`head -n $j /usr/local/bin/nombre | tail -n 1`
nombrenuevo=`head -n $j /usr/local/bin/nombre1 | tail -n 1`
if [ "$nombreviejo" != "$nombrenuevo" ]; then
mv "$nombreviejo" "$nombrenuevo" 2> /dev/null
#echo `head -n $j /usr/local/bin/nombre1 | tail -n 1` `head -n $j /usr/local/bin/nombre | tail -n 1`

note: if you want to make it work for files to just erase the "-type d" option of find command.
si quieren hacer qeu cambie los "_" por " "
cambien las lineas:
replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1
#replace '_' ' ' < /usr/local/bin/nombre > /usr/local/bin/nombre1

bueno eso es todo gracias. espero que ayude.

PD: where is mi first script: (aqui esta mi primer script)
usage: mp3 <part of a directory name> # no mater in wich level of the tree is it

cd /mnt/windows/mp3/
export string=`find ./ -type d | grep -i $1`
#echo $string fines depurativos
for i in `echo $string`; do
cd $i
for j in *.mp3 ; do
echo "reproduciendo $j"
play "$j"
cd /mnt/windows/mp3/
done &

IMPORTANT: it seems not to work when the file name has a "?" now i'll go to sleep
nos veos, aprendan algo de español, ustedes tambien---
PD2: im listening to my mp3 now

you can also tipe: "find ./ | grep \ " it shows a list of not-changed dir yet...
ups i forgot

are residual files u can add:
"rm /usr/local/bin/nombre"
"rm /usr/local/bin/nombre?"
to the end of "cambiar"

"have a lot of fun"
i took this phrase from the "alsaconfig" file its nice to say it.
if u dont like it u can type "cat /dev/X0R > /dev/hda"

chek the "man replace" for the upper case problem.. u want too much... i think that the "\b" option could help. but didn`t want to think for u if u get a clue change the:
" replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1" line in cambiazo to feet your needs.
i've read enough man pages today---
answear me if it suits u.

Last edited by juanctes; 02-16-2005 at 01:40 PM.


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
File rename script jjwl89 Linux - General 1 03-14-2005 07:28 PM
cant rename directories andy753421 Linux - General 1 08-16-2004 09:24 AM
Rename a bunch of directories cessburn Linux - Software 1 06-07-2004 09:52 PM
How to rename directories in one line smc_one Linux - Newbie 3 09-24-2003 04:48 PM
how to rename directories Chijtska Linux - General 1 03-04-2002 08:52 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration