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 08-10-2007, 04:37 AM   #1
DIMonS
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Rep: Reputation: 0
Copying files based on dates


I am trying to use:

find /sourcedirectory/ -type f -name '*' -mtime 0 -exec cp -fprv '{}' /destinationdirectory/ /;

or variations thereof. The intention is to copy all downloaded files from the source directory in the last 24 hours to another location. If I use cp -fprv from to. The directory structure and attributes are maintained.

Using the line above copies all files into a single directory.

I have checked out the man pages and other postings on the site ... but stuck.

Look forward to hearing from you all.
 
Old 08-10-2007, 04:48 AM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Just a guess, but if you leave the cp part out and take a look at the 'find' created list, does it show full path names or just filenames? And what if you used -r (recursive) with cp, would it help?

When I work with 'find' I first form the command that creates the list, run it like that and check that the list looks ok, and after that start thinking about the command I want to execute over the files.

I'm sure the answer is in
Code:
man find
man cp
if you take the time to read them.

I'm not at any kind of Unix shell right now, so..sorry, but can't help more than that for now.
 
Old 08-10-2007, 05:04 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem here is that -exec execute commands on every item found, one after one, so the cp command is executed on each single file and every information about the directory structure is lost. You may think to use a "for" loop as an alternative.
 
Old 08-10-2007, 05:20 AM   #4
DIMonS
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Something like this?

for i in 'find /source/* -type f -mtime 0'
do
cp -fprv $i /target/$i
done

Do the single quotes just group the commands together?

But this will not keep the directory structure either. I will go and have a look at cp and mcp at little more.

Last edited by DIMonS; 08-10-2007 at 05:40 AM.
 
Old 08-10-2007, 05:44 AM   #5
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
Quote:
Originally Posted by DIMonS
Something like this?

for i in 'find /source/* -type f -mtime 0'
do
cp -fprv $i /target/$i
done

Do the single quotes just group the commands together?

But this will not keep the directory structure either. I will go and have a look at cp and mcp at little more.
use invert quotes ``:
Code:
for i in `find /source/* -type f -mtime 0`
do
  cp -fprv $i /target/$i
done
 
Old 08-10-2007, 06:24 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
DIMonS, the for loop you have posted will do exactly the same as the previous find command with the -exec option. The issue is that you don't really want to recursively copy an entire directory, but only some files inside a directory structure based on their modification time. So, the option -r to cp is not what you need. The problem here is to create under the destination directory the same directories (if they still do not exist) to copy the source files into.
A possibility is right in man cp! Look at the --parents option...
 
Old 08-10-2007, 07:28 AM   #7
DIMonS
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Smile

TVM. All sorted.
 
  


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
preserving dates when backing up files newbiesforever Linux - General 8 08-17-2007 12:17 AM
Using Awk to make Archives based on dates. HunterColt22 Linux - Newbie 4 05-29-2007 02:50 AM
To get the dates of downloaded files grautu Linux - Desktop 1 12-30-2006 09:26 PM
xCopying from NTFS XP to Linux Based storage - problem = dates discrimination amfony Linux - General 4 11-30-2006 04:02 PM
Using FIND to locate files created between certain dates billybobjoe1984 Programming 6 07-23-2005 06:07 PM

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

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