Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash and dirname with space
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
kentsin
n00b
n00b


Joined: 08 Dec 2002
Posts: 58
Location: Macao

PostPosted: Sat Oct 16, 2004 10:19 am    Post subject: bash and dirname with space Reply with quote

I want to mv files from dir1 to dir2,

for d in * do
if [ ! -d dir2/$d ] then
mkdir dir2/$d
fi
for dd in $d/* do
mv $dd/* dir2/$d
done
done

However, the file and directory names contain space, which make bash make mistake in mkdir and mv

How can I escpae spaces? And if there are other special characters, how can I prevent them broken the script?

Thank you.
Back to top
View user's profile Send private message
gen2fox
Guru
Guru


Joined: 25 May 2004
Posts: 544

PostPosted: Sat Oct 16, 2004 10:28 am    Post subject: Reply with quote

Quote the file or dir name: "dir/$d"

Last edited by gen2fox on Sat Oct 16, 2004 10:31 am; edited 1 time in total
Back to top
View user's profile Send private message
spb
Retired Dev
Retired Dev


Joined: 02 Jan 2004
Posts: 2135
Location: Cambridge, UK

PostPosted: Sat Oct 16, 2004 10:29 am    Post subject: Re: bash and dirname with space Reply with quote

Something like
Code:
for d in * do
    if [ ! -d "dir2/$d" ] then
        mkdir "dir2/$d"
    fi
    for dd in "$d"/*  do
        mv "$dd"/* "dir2/$d"
    done
done
Back to top
View user's profile Send private message
kentsin
n00b
n00b


Joined: 08 Dec 2002
Posts: 58
Location: Macao

PostPosted: Sat Oct 16, 2004 1:15 pm    Post subject: Reply with quote

Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum