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 03-10-2007, 03:39 AM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
path problem


hi All,

I would like to copy "less*.dat" files in each "DH*" directory into "file_less*.dat" like:


for dir in DH* ; do
for file in `find $dir -type f -name "less*.dat" -print`; do
of=file_"$(basename $file)"; cp $file $of;
done
done

but this copy everything in root-directory of DH* directories. How to copy files into correcponding directories?

thanks
oliver
 
Old 03-10-2007, 04:39 AM   #2
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Code:
for dir in DH* ; do
    for file in `find $dir -type f -name "less*.dat" -print`; do
        of=file_"$(basename $file)"; cp $file $dir/$of; 
    done
done
Note: in the cp command you should add $dir to the destination.
--

Last edited by omnio; 03-10-2007 at 04:52 AM.
 
Old 03-10-2007, 05:39 AM   #3
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
thank you Omnio.
 
Old 03-10-2007, 07:22 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Instead of using this:
Code:
for file in `find ...`; do
  ...
done
Consider this:
Code:
find ... |while read file; do
  ...
done
The difference is subtle, but important. If the number of files found is large, the first will fail due to a size limitation on the execution substitution. Also, files with spaces in the name will not be handled correctly in the first case, but can be in the seconds case (assuming $file is properly quoted when it is used).
 
Old 03-10-2007, 10:49 AM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by matthewg42
Instead of using this:
Code:
for file in `find ...`; do
  ...
done
Consider this:
Code:
find ... |while read file; do
  ...
done
The difference is subtle, but important. If the number of files found is large, the first will fail due to a size limitation on the execution substitution. Also, files with spaces in the name will not be handled correctly in the first case, but can be in the seconds case (assuming $file is properly quoted when it is used).
The first is unlikely to fail due to the number of files found. If it were an external command, an "Argument list too long" error is possible, but because 'for' is a shell builtin, it is limited only by available memory. Spaces and other pathological characters will still be a problem, so it is never the best way to write such a loop.

The second will not work if there are leading or trailing spaces, or backslashes, in any of the filenames. To deal with those, it should be:
Code:
find ... | while IFS= read -r file
This will still fail if any names contain newlines.
 
  


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
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
Problem with $PATH halfpower Linux - Newbie 4 08-30-2005 12:49 PM
Problem with path kvelan SUSE / openSUSE 2 07-26-2005 12:06 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM
PATH problem? makai_wi Linux - Newbie 18 11-12-2002 12:56 PM

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

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