LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-21-2005, 06:49 PM   #1
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Rep: Reputation: 15
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.
 
Old 09-21-2005, 08:46 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
mv "$v1" /opt
should work ...


Cheers,
Tink
 
Old 09-21-2005, 09:35 PM   #3
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
no that does not work either, i forgot to list it in the tried section.
 
Old 09-21-2005, 09:53 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What about a completely different approach?

find -maxdepth 1 -iname "*html" -exec mv "{}" /opt/. \;


Cheers,
Tink
 
Old 09-21-2005, 10:16 PM   #5
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
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?
 
Old 09-21-2005, 10:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 09-21-2005, 10:25 PM   #7
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
mv "$var1" /opt
mv: cannot stat `index 1.out\nindex 2.out': No such file or directory
 
Old 09-21-2005, 11:16 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
Old 09-22-2005, 01:28 AM   #9
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
thank you, that does work
can you explaing IFS ' ' thing? or point me in the right direction


thanks again
 
Old 09-22-2005, 01:43 AM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 09-22-2005, 08:30 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
ls | cpio -pd /opt
 
  


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
Shell Scripting and Files With Spaces Matir Programming 12 08-17-2005 02:43 AM
Bash Scripting Directories with Spaces gtwilliams Linux - Software 3 07-06-2005 04:16 PM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 01:50 PM
server variables in bash scripting basher400 Linux - Newbie 4 04-11-2005 07:04 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 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
Open Source Consulting | Domain Registration