LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   move files based on date (http://www.linuxquestions.org/questions/programming-9/move-files-based-on-date-4175441016/)

graphicsmanx1 12-11-2012 02:32 PM

move files based on date
 
I wanted to make some code that would move files based on date. I have no code and just thought it would be cool to practice and learn how to make something like this. I would use mv but I didnt know how to read file creation based on date.

serafean 12-11-2012 02:35 PM

Hi,

you'll need to use the find command. Take a look at the -exec and -ctime options.
I suppose you are familiar with man pages.

Serafean

graphicsmanx1 12-11-2012 02:45 PM

yes I am and also familiar with find for file types but I was unaware find could detect by date

EDIT:
found man find -printf format %Ak and has a nice section based on time

serafean 12-12-2012 02:12 PM

find is an extremely powerful tool. Whenever you need to get a set of files based on some criteria, check find whether it has the option you need.
Also a hint : you can search in man pages, press / and type, it'll search forward in the page, press ? and then type, it'll search backwards. Extremely useful.
Hint 2 : when searching for an option, search for <space>-{option_letter}, for instance " -ctime";

happy bashing!

Serafean

Habitual 12-12-2012 02:59 PM

Quote:

I wanted to make some code that would move files based on date. I have no code and just thought it would be cool to practice and learn how to make something like this. I would use mv but I didnt know how to read file creation based on date.
"cool to practice" should be searching the local man pages :)

Code:

man mv
...
-u, --update
    move  only when the SOURCE file is newer than the destination file or when the destination file is missing.
...

does mention the word 'date'.

mv'ing files about using 'mv' has nothing to do with date.

What you could check out is a backup script to "move files based on date.", now that would be "cool".

graphicsmanx1 12-12-2012 04:57 PM

Quote:

Originally Posted by Habitual (Post 4848000)
"cool to practice" should be searching the local man pages :)

Code:

man mv
...
-u, --update
    move  only when the SOURCE file is newer than the destination file or when the destination file is missing.
...

does mention the word 'date'.

mv'ing files about using 'mv' has nothing to do with date.

What you could check out is a backup script to "move files based on date.", now that would be "cool".

hard to search for something when you dont know what its called.. hence the question which was already answered with wise input. Thanks :D

David the H. 12-16-2012 06:45 AM

Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


To match files by date and time, you generally use the -mmin/-amin/cmin, -mtime/-atime/-ctime, or the -newer/-anewer/-cnewer tests. The latter grouping allows you to compare against times other than the present by first using touch to create a temporary file with the date you want to compare it against.


gnu find has recently added a -newerXY test, which allows you to pass it a date string directly, without needing an external file. The "XY" characters are replaced with the type of test you want to do, and the optional date string accepts the same formats as date's -d option.

e.g. to locate all files with mtime newer than Dec. 1st:

Code:

find . -type f -newermt "Dec 01 2012" -print

bigearsbilly 12-18-2012 01:49 AM

Quote:

Originally Posted by graphicsmanx1 (Post 4847227)
yes I am and also familiar with find for file types but I was unaware find could detect by date

Then surely you are not familiar with it ;)

Get into the habit of reading the manual pages.
It can save hours and hours of pointless work!


All times are GMT -5. The time now is 01:02 AM.