Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-21-2005, 06:49 PM
|
#1
|
Member
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64
Rep:
|
moving files that have spaces in variables -bash scripting
I seem to not be able to move or copy files that have spaces that are contained in variables.
for instance
v1=$(ls *.html)
mv $v1 /opt
if files have spaces then i get an error trying to move them because it tries to break up file names.
I have tried
ls -Q
for loops
ls --quoting-style=shell
mv "$v1" /opt
?
Last edited by bhar0761; 09-21-2005 at 09:46 PM.
|
|
|
09-21-2005, 08:46 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
mv "$v1" /opt
should work ...
Cheers,
Tink
|
|
|
09-21-2005, 09:35 PM
|
#3
|
Member
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64
Original Poster
Rep:
|
no that does not work either, i forgot to list it in the tried section.
|
|
|
09-21-2005, 09:53 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
What about a completely different approach?
find -maxdepth 1 -iname "*html" -exec mv "{}" /opt/. \;
Cheers,
Tink
|
|
|
09-21-2005, 10:16 PM
|
#5
|
Member
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64
Original Poster
Rep:
|
Yes that did work, but I need to work with the files stored in a variable before the move.
It is weird to me that the command doesn't work or are we just missing something?
|
|
|
09-21-2005, 10:22 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally posted by bhar0761
no that does not work either, i forgot to list it in the tried section.
|
Hmmm .. what DOES it do when invoked like that?
Cheers,
Tink
|
|
|
09-21-2005, 10:25 PM
|
#7
|
Member
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64
Original Poster
Rep:
|
mv "$var1" /opt
mv: cannot stat `index 1.out\nindex 2.out': No such file or directory
|
|
|
09-21-2005, 11:16 PM
|
#8
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
This is the way I've always done it, but it doesn't seem to be extremely popular:
Code:
#!/bin/bash
# Do initial script stuff
old_ifs=${IFS}
IFS=$'
'
for file in $( ls *.html )
do
mv "${file}" /opt
done
IFS=${old_ifs}
# Do more stuff
|
|
|
09-22-2005, 01:28 AM
|
#9
|
Member
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64
Original Poster
Rep:
|
thank you, that does work
can you explaing IFS ' ' thing? or point me in the right direction
thanks again
|
|
|
09-22-2005, 01:43 AM
|
#10
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
IFS is an environment variable used by the shell to determine where to break a list of items into single items. In other words, the IFS variable contains a list of delimeters that mark the end of an item. (man bash and search for IFS if you want to know the gory details) By default, IFS is defined to include space, tab, and newline. Normally this is what people want. So, the magic hand-waving at the top of the script saves the existing value of IFS in a temporary variable, sets IFS to include only the newline character, and that forces bash to treat files with spaces as a single item. Then, it restores the IFS to the original value in case the script has anything else to do that relies on the original value of IFS.
|
|
|
09-22-2005, 08:30 AM
|
#11
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
ls | cpio -pd /opt
|
|
|
All times are GMT -5. The time now is 01:34 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|