Clicky

Consider a directory with the following files below.  Lets assume the current time is 1am on Dec 13.  I want to move all files where the year, month and day of the last modified is not today.  The script will run shortly after midnight every night to mv yesterdays files.  So all but the last file in the list below should be moved.

Using the find . -type f -name "*.dat" -mtime +1 method doesn't work because it looks for files older than 24 hours I think.  


$ ls -ltr *.dat
-rw-r--r-- 1 initiate initgrp 1079 Nov 18 17:50 input-20111118-1734.dat
-rw-r--r-- 1 initiate initgrp  602 Dec  7 12:02 input-20111207-1202.dat
-rw-r--r-- 1 initiate initgrp  173 Dec  7 12:21 input-20111207-1221.dat
-rw-r--r-- 1 initiate initgrp  348 Dec  9 14:53 input-20111209-1256.dat
-rw-r--r-- 1 initiate initgrp  514 Dec 12 15:47 input-20111212-1519.dat
-rw-r--r-- 1 initiate initgrp    0 Dec 12 23:34 tester.dat
-rw-r--r-- 1 initiate initgrp  173 Dec 13 00:03 input-20111213-0003.dat

asked 12/12/2011 01:17

donnatronious's gravatar image

donnatronious ♦♦


2 Answers:
This one was kind of tough, but this little script will get it done - remember after you create it, to make it executable (chmod 755 myscript.sh)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
#!/bin/sh

DESTINATION="/path/to/move/files/to"

for FILE in *.dat; 
do
        MODSECS=`date --reference=$FILE +%s`
        TODAY=`date +%Y-%m-%d`
        MIDNIGHT=`date --date "$TODAY 00:00:00" +%s`
        if [ $MODSECS -lt $MIDNIGHT ]; then
                mv $FILE $DESTINATION
        fi
done
link

answered

xterm's gravatar image

xterm

Assuming you have GNU find, use the -daystart flag, eg:
1:
find . -type f -daystart ! -mtime 0 -name "*.dat"
link

answered 2011-12-12 at 22:11:47

Tintin's gravatar image

Tintin

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

Asked: 12/12/2011 01:17

Seen: 198 times

Last updated: 12/13/2011 04:50