LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-02-2004, 10:33 PM   #1
xiaodown
Member
 
Registered: Jun 2003
Location: Virginia
Distribution: Redhat, Centos, Fedora
Posts: 37

Rep: Reputation: 15
Foreach script to copy files gone wrong... clue?


Background:

My plan is this: I would like to take a hard drive from a Windows box and slap it into a linux box, find every file of a certain extention on the drive, and copy it to a backup directory. I have been trying to do this with a csh script and a foreach loop. A vague pseudo code of this seemingly easy script looks like this:

foreach thingie ( list of files )
copy $thingie /whatever/backup
end

And that's all I keep thinking it should take. But the problem is this.
I have the command:

Code:
find /customers/drive | grep -i jpg
for example, and this outputs:

Code:
/customers/drive/directory/file01.jpg
/customers/drive/directory/sub/file02.jpg
/customers/drive/directory/sub/file03.jpg
etc. BUT when I put it into the foreach loop, the foreach loop interprets spaces in directory names and files incorrectly. So what I end up with is the output of find giving me

Code:
/customers/drive/Documents and Settings/bob/pics/pic01.jpg
BUT since the foreach intereprets things as
foreach var (thing1 thing2 thing3 thing4)

it sees the output of foreach var (`find /customers/drive | grep -i jpg`) as

thing1 = /customers/drive/Documets
thing2 = and
thing3 = Settings/bob/pics/pic01.jpg

And naturally gives me a "cannot stat file 'and' - file cannot be copied".




So basically, is there a way to tell foreach not to interpret spaces in lines as new items in it's list? To tell it the delimeter for a new entry in the list is a newline?

Full script is basically this (so far):

Code:
foreach thing (`find /customers/drive | grep -i jpg`)
cp -backup=t $thing /backup
end

Things I've tried so far: using sed to "escape" the space character via changing the parenthetical phrase for the foreach condition to say:
Code:
foreach thing (`find /customers/drive | grep -i jpg | sed 's/\ /\\ /g'`)
which didn't work either - if you do it from the command line, it gives you files like /customers/drive/Documets\ and\ settings/bob/pic01.jpg, but foreach still sees them as being breaks at the spaces.

Alternatively, is there an easier way to do this?

Thanks for all help, sorry this was so long, trying to give as much info as I could.

~Wx
 
Old 11-02-2004, 10:57 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about something like this....

find /mnt/win -type f -name '*.jpg' -exec cp {} /mnt/backup \;
 
Old 11-02-2004, 11:00 PM   #3
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
find /customer/whatever/dir -iname "*.jpg" -exec cp {} /backup/. \;

[edit] Heh - wish I could type faster while reading messages on IRC ;}[/edit]


Cheers,
Tink

Last edited by Tinkster; 11-02-2004 at 11:01 PM.
 
  


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
need script to copy all files containing a certain string to a directory dc6463 Programming 21 04-18-2013 01:56 PM
Simple script to copy files, getting \r's at end of cmd ElricM Linux - Newbie 2 07-23-2005 11:21 AM
will this script copy only certain files? verbatim Programming 5 04-28-2005 09:28 AM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 07:29 PM
how to copy and paste between files and within files Bheki Linux - General 1 02-05-2002 05:29 AM

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

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