LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-20-2005, 04:43 PM   #1
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Rep: Reputation: 15
ls *.html bash scripting


i would like to be able to enclose this variable command var1=$(ls *.html) with quotes.
for instance, if there are files in the directory like:
hello world.html and hello.html
when i pass the ls *.html to the command line, i want it to return quoted

so when i call the variable var1
echo $var1
"hello world.html" "hello.html:

and not
hello world.html hello.html

one way of doing it is using sed but is there another way.
ls | sed -e 's/^/"/' -e 's/$/"/g'

Last edited by bhar0761; 09-20-2005 at 05:30 PM.
 
Old 09-20-2005, 05:36 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Code:
export IFS='
'
for i in $(ls -1 | grep ' '); do echo $i; done
hth --Jonas
 
Old 09-20-2005, 05:41 PM   #3
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
Check out the tidbit I put in the LQ wiki a few weeks ago here: http://wiki.linuxquestions.org/wiki/...n_in_Filenames
 
Old 09-20-2005, 05:41 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
The short of it, by the way, is to use the -q or, better, the --quote=shell options.
 
Old 09-20-2005, 05:46 PM   #5
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
Talking

wow, that is pretty funny, i even scanned through the man page for ls and i missed. what an idiot i am.

thank you much.......
 
Old 09-20-2005, 05:52 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
Hey, I've been using Linux for 6-7 years and only noticed that option last month. In some cases, you may need to make sure it's using the /bin/ls and not the shell built-in ls (only some shells have a built in, but most of those don't support the -q options).
 
Old 09-20-2005, 05:58 PM   #7
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
cool, so im trying to do this in order to move files but when i try this command to move the files, it returns and error
files in the directory are
h m.html
q d.html

#!/bin/bash
for file in `ls --quote-style=shell *.html`
do mv $file /temp
done

> ./m.sh
"h
mv: cannot stat `"h': No such file or directory
w.html"
mv: cannot stat `w.html"': No such file or directory
"q
mv: cannot stat `"q': No such file or directory
d.html"
mv: cannot stat `d.html"': No such file or directory

why the errors?
i have tried adjusting the ls command but no luck
 
Old 09-20-2005, 06:02 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
Have you tried:
Code:
find . -name '*.html' -exec mv {} /temp \;
Note that this will also do subdirectories of the current directory. Adding '-maxdepth 0' will disable this recursion.
 
Old 09-20-2005, 06:04 PM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
or just
Code:
$ mv *html /temp
 
Old 09-20-2005, 06:14 PM   #10
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
Yes that worked, thank you
but it is there an explanation why the other command didn't work. i would sure love to be able to use the ls command
the reason being that I am tying to save the files as a variable so i can call them to do different things before the move command

converting them to ps
printing them and then moving to a different directory
find . -name '*.html' -exec mv {} /temp \;
 
Old 09-20-2005, 06:14 PM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
Quote:
Originally posted by jonaskoelker
or just
Code:
$ mv *html /temp
That too. Though I'd include the . as well to avoid "guidetohtml" (just an example) getting moved, as in
Code:
$ mv *.html /temp
Always be as specific as possible and you end up with fewer unintended consequences.
 
Old 09-20-2005, 06:17 PM   #12
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
You know, I think I made a mistake. I believe the proper option is --quoting-style. My apologies for that.
I just tested
Code:
for file in `ls --quoting-style=shell *.html`
do mv $file /temp
done
with great success.
 
Old 09-20-2005, 07:01 PM   #13
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
i just copied and tried it but i got the same error..i will keep trying.

Code:
#!/bin/bash
for file in `ls --quoting-style=shell *.html`
do mv $file /temp
done

Last edited by bhar0761; 09-20-2005 at 07:03 PM.
 
Old 09-20-2005, 07:43 PM   #14
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
Interesting, I can seem to get to use the "for" script

i even went to the basics and tried it line by line
files in directory:
index 1.html index 2.html
var1=$(ls -Q *.html)
mv $var1 /temp
cp $var1 /temp
neither command work s

but if i actually write
mv "index 1.html" "index 2.html" /temp
it works just fine

What do you think?
Did the "for script work for you?
 
Old 09-20-2005, 10:09 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
The for script works fine for me. Please place an "echo" before the mv in the script so we can see what commands it wants to execute and then post the output here.
 
  


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
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 08:53 AM
BASH Scripting akilles Linux - Newbie 5 11-10-2004 08:26 PM
Bash Scripting Help Aioth Linux - Newbie 8 09-21-2004 12:21 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 02:10 AM
Bash scripting kbeaver Programming 5 07-18-2003 09:35 PM


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