0

Using the stat command one can see and format the last date a directory was accessed. The adate

Is it possible to use find to find the directories that were changed before a certain date using a test of some sort?

  • The fundamental difficulty with this is that find will change the access time of directories when it opens them. – Tavian Barnes Dec 20 '18 at 18:03
1

Shellscripts

You can try the following shellscripts using only find, sed, sort (and echo for the Usage part). find can do what might be done with stat. The only difference is a ! character, that negates the test -newerat.

olderdate:

#!/bin/bash

if [ $# -ne 2 ]
then
 echo "Usage:   $0 <reference date> <directory> "
 echo "Example: $0 2018-11-30 ."
 exit
fi

find "$2" -type d ! -newerat "$1" -printf "%AY-%Am-%Ad %AT  %p\n" |
 sed -e 's%\..*  /%     /%' \
     -e 's%\..*  \.%     .%' | sort

newerdate:

#!/bin/bash

if [ $# -ne 2 ]
then
 echo "Usage:   $0 <reference date> <directory> "
 echo "Example: $0 2018-11-30 ."
 exit
fi

find "$2" -type d -newerat "$1" -printf "%AY-%Am-%Ad %AT  %p\n" |
 sed -e 's%\..*  /%     /%' \
     -e 's%\..*  \.%     .%' | sort

You specify not only date, but also time, hours, hours:minutes or hours:minutes:seconds if you quote the second parameter,

./olderdate '2018-12-19 18' /path

Comment about 'automatic' modification of the access time

I did some testing, and noticed that in some cases find or stat will modify the access date and time of directories that are searched. It seems like this will happen, when something has been changed in the directory, but the access time of the directory itself has not been updated.

In these cases the access time will be set to the current time. But when find or stat will search the directory again, the access date and time will remain the same (unless something has been changed again in the directory).

0

Here is something you can get started on:

MY_DATE="2018-12-19 06:30"
for i in $(find . -type d)
do
    if [[  $(date -d"$(stat $i | grep ^Access | tail -1 | awk '{print $2 " " $3}')" +%s) -gt $(date -d"$MY_DATE" +%s) ]]
    then
        echo $i
    fi
done

This will not work with directories that have spaces in their path.

The script simply loops through all directories found from the place where you are running it from (you can change this by modifying the find command) and running stat on them, comparing the Access date to the Date supplied in MY_DATE variable.

0

Is this about right?

$ cd ~

# adate for ./Pictures/
$ stat -c %x ./Pictures/
2018-05-18 11:08:44.326743397 -0400

# adate for ./Music/
$ stat -c %x ./Music/
2018-05-18 11:08:44.325743396 -0400

# Change das adate for ./Music/:
$ ls -la ./Music/
drwxr-xr-x. 2  leeand00 leeand00     6 May 18 2018  .
drwx------. 24 leeand00 leeand00  4096 Dec 11 09:54 .. 

# Read the adate for ./Music/ to see that it changed:
$ stat -c %x ./Music/
2018-12-19 23:35:04.789892164 -0500

# Read the adate from ./Pictures/ to see that it did not change:
$ stat -c %x ./Pictures/
2018-05-18 11:08:44.326743397 -0400

# Don't re-invent the wheel:
# `-type d` (look for folders only)
#
# `-maxdepth 1` (Only look inside this directory for folders, 
#                don't descend lower than that)
#
# `-amin -300` ()
#
$ find . -maxdepth 1 -type d -amin -300
.
./Music

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.