LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-17-2004, 03:09 AM   #1
javeree
Member
 
Registered: Jun 2004
Location: Belgium
Posts: 49

Rep: Reputation: 15
script problem


I am trying to copy a set of files which I want to select with find.
A simplified example would be:

for i in `find . -mtime 5`; do cp $i /dest/; done

The problem is that some of the files i select contain spaces. Such a filename is interpreted by the for command as separate words, and the resuls are that cp gets only parts of the filename at a time.

How can I solve this best ?
My first ideas were to redefine $IFS to only include newlines, but this may result in problems later on (in reality i do more complex things between do and done).
The next idea was to somehow format the output of find so that each filename would be quoted "my filename", but I think the shell would interprete these quotes as \"my filename\" .
The best thing would be if I could make find return my\ filename, but I don't know how to do that. Any ideas ?
 
Old 09-17-2004, 04:14 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Re: script problem

Quote:
Originally posted by javeree
for i in `find . -mtime 5`; do cp " $i" /dest/; done

Cheers,
Tink
 
Old 09-20-2004, 08:43 AM   #3
javeree
Member
 
Registered: Jun 2004
Location: Belgium
Posts: 49

Original Poster
Rep: Reputation: 15
The apostrophes may be required too, but it's not sufficient.

have a look at the output of

for i in `find . -type f -name 'm*'`; do echo "$i"; done

in a folder containing my_filename and my\ filename

./my_filename
./my
filename

As you can see, the loop is run three times, so the problem is not with the specific command within the do loop, but in the way the for loop interprets the output of the find command.
 
Old 09-20-2004, 09:20 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
That *will* work:
1) find . -mtime 5 -print | while read i; do cp "$i" /dest/; done

or those *may* work (to be tested):
2) find . -mtime 5 -exec cp {} /dest/ \;
3) cp $(find . -mtime 5 -printf "\"%p\" ") /dest/

Yves.
 
Old 09-20-2004, 09:38 AM   #5
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 33
4) find . -mtime 5 -print0 | xargs -iXX -0 cp XX /dest
 
Old 09-20-2004, 10:11 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
Excellent! You just showed me how to "correct" the two shortcommings I thought were in xargs (and were not in fact): XX is for being able to append args to the stdin args, isn't it? And print0 and -0 are for separating args by nul instead of blank (-la C), right?

Thanks

Yves.
 
Old 09-20-2004, 10:18 AM   #7
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 33
yes, -i specifies the substitute argument name, which came from stdin
 
Old 12-10-2004, 11:01 AM   #8
javeree
Member
 
Registered: Jun 2004
Location: Belgium
Posts: 49

Original Poster
Rep: Reputation: 15
an easy solution is to temporarily tell bash not to consider space to be a separator:

export IFS=$'\t\n' # tab and newline
for $i in `ls *`; do cp $i /dest/; done
export IFS=$' \t\n' # space tab and newline
 
  


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 script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM
Script file problem (or operator problem) HELP! lostone Programming 2 05-23-2001 02:30 PM
Script file problem (or operator problem) HELP! lostone Linux - General 1 05-17-2001 06:11 PM
Script file problem (or operator problem) HELP! lostone Linux - Newbie 0 05-15-2001 07:59 PM

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

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