LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 03: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 04:30 PM.
 
Old 09-20-2005, 04:36 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

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

Rep: Reputation: 128Reputation: 128
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, 04:41 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
The short of it, by the way, is to use the -q or, better, the --quote=shell options.
 
Old 09-20-2005, 04: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, 04:52 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 04: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, 05:02 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 05:04 PM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
or just
Code:
$ mv *html /temp
 
Old 09-20-2005, 05: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, 05:14 PM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 05:17 PM   #12
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 06: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 06:03 PM.
 
Old 09-20-2005, 06: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, 09:09 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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



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 07:53 AM
BASH Scripting akilles Linux - Newbie 5 11-10-2004 07:26 PM
Bash Scripting Help Aioth Linux - Newbie 8 09-21-2004 11:21 AM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM
Bash scripting kbeaver Programming 5 07-18-2003 08:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:35 AM.

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
Open Source Consulting | Domain Registration