You are not logged in.

#1 07 Dec 2011 15:34

pittendrigh
Member
From: Bozeman, MT
Registered: 07 Dec 2011
Posts: 1
Website

Changing directory names that contain spaces

I frequently get DVDs (usually images) from customers who want me to create an image gallery.
Filenames and directory names that contain spaces are annoying.

A simple sedfilenames script is easy.
Changing directory names is more difficult. 

First stab (with param checking and usage omitted) below.
The following blows up because a $dir named "my images" evalutates to "my" in the mv line.

find $searchwhere -depth -type d -name "*$changefrompattern*" | while read dir;
do
   base=`basename "$dir"`
   dirname=`dirname "$dir"`
   newbase=`echo "$base" | sed "s/$changefrompattern/$changetopattern/g"`
   mv "$dir" "$dirname/$newbase"   
done

This seems to work ....although I don't know why

find $searchwhere -depth -type d -name "*$changefrompattern*" | while read dir;
do
   base=`basename "$dir"`
   dirname=`dirname "$dir"`
   newbase=`echo "$base" | sed "s/$changefrompattern/$changetopattern/g"`
   mv "$dir" "$dirname/$newbase"
done

QUESTIONS
1) Is there any way to make the first failed attempt (using a for find loop) to work?
2) What is it (in the second loop that does work) about find pipe to while read that makes things work as desired?

Offline

#2 26 Apr 2012 05:38

creperie
Member
Registered: 26 Apr 2012
Posts: 2

Re: Changing directory names that contain spaces

i did not read the forums guidelines.
you may want to pose your question at unix and linux forum dot something.
a search engine query of : unix and linux forum, would show the target.


the subtle taste of silence

Offline

#3 07 May 2012 13:54

kevink
Member
Registered: 20 Feb 2012
Posts: 9

Re: Changing directory names that contain spaces

Try escaping the quotes, like this:

mv \"$dir\" \"$dirname/$newbase\"

The quotes you're using aren't gaining you anything, because $dir and "$dir" are both going to evaluate to two strings instead of one.

Offline

Board footer

Powered by