LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   copy all the files in the directory based on the modification date (https://www.linuxquestions.org/questions/programming-9/copy-all-the-files-in-the-directory-based-on-the-modification-date-853319/)

SriniKlr 12-29-2010 07:33 AM

copy all the files in the directory based on the modification date
 
Quote:

Originally Posted by Kenny_Strawn (Post 4090713)
Please wrap [CODE] tags aroung any code posted here. The full source that way could still be posted.

Hi All,

I am trying to copy all the files in the directory based on the modification date (i.e created on Dec 29). Not able to find the proper command for this. This is what I have tried.

'

(none) login: root
#
# cd /mnt/hd/
#
# ls *.log
dbg_0.log dbg_13.log dbg_18.log dbg_22.log dbg_27.log dbg_6.log
dbg_1.log dbg_14.log dbg_19.log dbg_23.log dbg_28.log dbg_7.log
dbg_10.log dbg_15.log dbg_2.log dbg_24.log dbg_3.log dbg_8.log
dbg_11.log dbg_16.log dbg_20.log dbg_25.log dbg_4.log dbg_9.log
dbg_12.log dbg_17.log dbg_21.log dbg_26.log dbg_5.log

# ls -l *.log | grep 'Dec 29'
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_20.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_21.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_22.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_23.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_24.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_25.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_26.log
-rwxr-xr-x 1 root root 52297728 Dec 29 2010 dbg_27.log
-rwxr-xr-x 1 root root 23068672 Dec 29 2010 dbg_28.log

# ls -l | grep 'Dec 29' | cp /mnt/usb/
BusyBox v1.11.1 (2010-12-07 11:33:09 IST) multi-call binary

Usage: cp [OPTION]... SOURCE DEST

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

Options:
-a Same as -dpR
-d,-P Preserve links
-H,-L Dereference all symlinks (default)
-p Preserve file attributes if possible
-f Force overwrite
-i Prompt before overwrite
-R,-r Recurse directories
-l,-s Create (sym)links

Any hint??

Thanks, SriniKlr

grail 12-29-2010 09:06 AM

@SriniKlr - Do not hijack someone else's question!! Please create your own.

salasi 12-29-2010 09:07 AM

Quote:

Originally Posted by SriniKlr (Post 4206515)
Hi All,

I am trying to copy all the files in the directory based on the modification date (i.e created on Dec 29). Not able to find the proper command for this. This is what I have tried.

'

(none) login: root
#
# cd /mnt/hd/
#
# ls *.log
dbg_0.log dbg_13.log dbg_18.log dbg_22.log dbg_27.log dbg_6.log
dbg_1.log dbg_14.log dbg_19.log dbg_23.log dbg_28.log dbg_7.log
dbg_10.log dbg_15.log dbg_2.log dbg_24.log dbg_3.log dbg_8.log
dbg_11.log dbg_16.log dbg_20.log dbg_25.log dbg_4.log dbg_9.log
dbg_12.log dbg_17.log dbg_21.log dbg_26.log dbg_5.log

# ls -l *.log | grep 'Dec 29'
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_20.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_21.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_22.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_23.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_24.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_25.log
-rwxr-xr-x 1 root root 52428800 Dec 29 2010 dbg_26.log
-rwxr-xr-x 1 root root 52297728 Dec 29 2010 dbg_27.log
-rwxr-xr-x 1 root root 23068672 Dec 29 2010 dbg_28.log

# ls -l | grep 'Dec 29' | cp /mnt/usb/
BusyBox v1.11.1 (2010-12-07 11:33:09 IST) multi-call binary

Usage: cp [OPTION]... SOURCE DEST

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

Options:
-a Same as -dpR
-d,-P Preserve links
-H,-L Dereference all symlinks (default)
-p Preserve file attributes if possible
-f Force overwrite
-i Prompt before overwrite
-R,-r Recurse directories
-l,-s Create (sym)links

Any hint??

Thanks, SriniKlr

hints? Well, not the sort that help directly, but may help indirectly:
  • This is a thread hijack. Hijacking is where you take someone else's thread and divert it to your own problem. This is possibly acceptable if your problem is very closely related to the original problem, but your problem isn't. The original thread was about writing your own shell and yours about file permissions and copying. Given that, ab defintio, your problem won't be related to the thread title of the original thread, it, cough, won't get the attention that it deserves. (So, you should have started your own thread.)
  • Additionally, the idea of quoting a bit of text that advises you to use code tags, to improve clarity and then not using seems a bit odd, did you actually think about what the part that you read was saying?

SriniKlr 12-30-2010 12:05 AM

My Apologies for the interrupting the already in progress thread. Advice is noted will be taken care :)

Thanks SriniKlr

Julian Andrews 01-26-2011 10:08 AM

Hi SriniKlr

I think you're looking for the find command.

Code:

find /mnt/hd -mtime n -exec cp {} /mnt/usb/ \;
Where n is the number of days ago the file was modified. If you want to automate this, of course, you'll need code to come up with n, but that shouldn't be too hard.


All times are GMT -5. The time now is 05:52 PM.