LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-23-2012, 07:08 AM   #1
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Rep: Reputation: Disabled
Exclamation How to move or copy files present between two dates to another path folder.


Hey All,

I want to copy files present between two dates to another folder. I tried moving files present between 1st Jan to 30th Jan, 2011 to another folder but couldn't make it. I was able to list it but doing copy or move is creating problem. Please suggest on this.

Thanks in Advance.
Ankit
 
Old 01-23-2012, 08:43 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Hello,

Could you give us an example output of the files in the directory? Without knowing the exact formatting of the filenames, we won't be able to fully assist you. Last thing though, can you share with us what all you have done so far?

Cheers,

Josh
 
Old 01-23-2012, 10:36 AM   #3
hogar.strashni
Member
 
Registered: Dec 2007
Distribution: cp6
Posts: 44

Rep: Reputation: 2
I think there's no name formatting here, Josh. He simply want to copy all files created within certain period of time.

I don't know the exact command to list files that way, but I know how to copy files, once they are listed:

cp [options] `<your list command here>` <destination_directory>

Inside quotes you type the command that list JUST NAMES of those files (alike 'ls', and not alike 'ls -l'). For example:
Quote:
cp `ls bla*` /home/
command will copy all the files starting with "bla" to home directory. Don't forget those EXACT quotes!

Greetings,
Vladimir

Last edited by hogar.strashni; 01-23-2012 at 10:41 AM.
 
Old 01-23-2012, 10:54 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Ahh ok. I read it differently... You can use "-t" to sort by modification time. That being said, you can do a "ls -lt" and then pipe it to grep. You could always look at a sample "grep" of the range you want, and modify to your likings.
 
Old 02-01-2012, 02:32 AM   #5
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
I am trying script to list down the files and it shows me correct file.

But when i am trying to copy it is showing error "cp: cannot access ls uda*"

Please help on how to proceed.

Thanks,
Ankit

Last edited by ankitpandey; 02-01-2012 at 07:13 AM.
 
Old 02-01-2012, 02:58 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The find command already gives a list of the files that match your criteria, hence you don't need to use the ls command. You can either use the -exec predicate of find:
Code:
find /cgn-common-active/c8/temp/uda* -type f -newer ./tmpoldfile ! -newer ./tmpnewfile -exec cp -p '{}' /cgn-common-active/c8/temp/backup \;
or pipe the list to xargs:
Code:
find /cgn-common-active/c8/temp/uda* -type f -newer ./tmpoldfile ! -newer ./tmpnewfile -print0 | xargs -0 -I{} cp -p {} /cgn-common-active/c8/temp/backup
There are subtle differences between the two methods, for example -exec executes the command multiple times, once for each file, whereas xargs execute a single command with multiple arguments. Moreover you need the -print0 predicate and the matching -0 option of xargs to manage files with blank spaces in their name. Hope this helps.
 
1 members found this post helpful.
Old 02-01-2012, 04:28 AM   #7
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks for the help.

The first code is working perfect, but is taking some time as you said it will execute multiple time.

The second code is throwing below error

xargs: illegal option -- 0
xargs: Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]]

Anyways first code is helping me.

Thanks
Ankit
 
Old 02-01-2012, 04:41 AM   #8
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thnxs

Last edited by ankitpandey; 02-01-2012 at 07:17 AM.
 
Old 02-01-2012, 04:53 AM   #9
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
We can close this post

Last edited by ankitpandey; 02-01-2012 at 07:17 AM.
 
Old 02-01-2012, 08:10 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Glad to see you solved the issue. Regarding the -0 option of xargs, it's available on the GNU version. Maybe you have another flavour of the xargs/findutils package.
Quote:
Originally Posted by ankitpandey View Post
We can close this post
Nope. Marking it as SOLVED is enough. Cheers!
 
Old 02-02-2012, 03:35 AM   #11
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Hello,

It seems that it works on limited number of file, it tried this on large number of files so got error " arg list too long" any clue how to get this resolved.

Thanks
Ankit
 
Old 02-02-2012, 07:19 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Indeed there is a limit on the length of the argument list. See this article for a detailed discussion.

The most straightforward method to overcome this issue is a loop, e.g.
Code:
while read file
do
  cp -p "$file" /cgn-common-active/c8/temp/backup
done < <(find /cgn-common-active/c8/temp/uda* -type f -newer ./tmpoldfile ! -newer ./tmpnewfile)
This uses process substitution to feed the loop. Moreover the read statement takes care of files with blank spaces in their name (if any).
 
1 members found this post helpful.
Old 02-02-2012, 08:00 AM   #13
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
is this syntax flavour specific ? I am working on solaris and trying below code, but getting error as "syntax error at line 10: `<' unexpected" then i tried removing space between both (<) then got error as "syntax error at line 10: `(' unexpected".

#!/bin/sh

olddate="201201010001"
newdat="201202012359"
touch -t $olddate ./tmpoldfile
touch -t $newdat ./tmpnewfile
while read file
do
cp -p "$file" /cgn-common-active/c8/temp/backup
done < <(find /cgn-common-active/c8/temp/uda* -type f -newer ./tmpoldfile ! -newer ./tmpnewfile ! -newer ./tmpnewfile)

Is there any modification required. Please suggest

Thanks,
Ankit
 
Old 02-02-2012, 02:40 PM   #14
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Looks like lines 10 and 11 should be on the same line. Have you tried merging them?
 
Old 02-03-2012, 01:50 AM   #15
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
I tried below one as well but still the same error (syntax error at line 9: `<' unexpected)

while read file
do
cp -p "$file" /cgn-common-active/c8/temp/backup done < <(find /cgn-common-active/c8/temp/uda* -type f -newer ./tmpoldfile ! -newer ./tmpnewfile)
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using terminal command -Find files in a folder and copy them to a different folder j-jock Linux - General 4 11-28-2011 03:20 AM
Locate second disk path from ssh and copy a folder to it ASTRAPI Linux - Newbie 8 06-10-2010 08:46 AM
Help me...after used mv command all file missing only folder move to new path scoreone1 Linux - General 9 10-21-2008 12:22 AM
How to automatically move files from one folder into another folder? xmrkite Linux - Software 6 11-05-2006 11:39 AM
Cant move a folder to another path? dwarf007 Linux - Newbie 2 09-19-2004 03:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:16 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration