Using find to list directories that haven't been accessed since a certain date?












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?










share|improve this question
























  • 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
















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?










share|improve this question
























  • 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














0












0








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?










share|improve this question















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?







find directory date stat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 1:48









Jeff Schaller

38.7k1053125




38.7k1053125










asked Dec 19 '18 at 1:44









leeand00

1,38032341




1,38032341












  • 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


















  • 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
















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




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










3 Answers
3






active

oldest

votes


















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 %pn" |
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 %pn" |
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).






share|improve this answer





























    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.






    share|improve this answer





























      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





      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "106"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489801%2fusing-find-to-list-directories-that-havent-been-accessed-since-a-certain-date%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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 %pn" |
        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 %pn" |
        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).






        share|improve this answer


























          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 %pn" |
          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 %pn" |
          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).






          share|improve this answer
























            1












            1








            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 %pn" |
            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 %pn" |
            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).






            share|improve this answer












            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 %pn" |
            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 %pn" |
            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).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 20 '18 at 0:47









            sudodus

            1,12616




            1,12616

























                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.






                share|improve this answer


























                  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.






                  share|improve this answer
























                    0












                    0








                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 19 '18 at 12:03









                    T. Hajdara

                    212




                    212























                        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





                        share|improve this answer




























                          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





                          share|improve this answer


























                            0












                            0








                            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





                            share|improve this answer














                            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






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 20 '18 at 21:07

























                            answered Dec 20 '18 at 20:01









                            leeand00

                            1,38032341




                            1,38032341






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489801%2fusing-find-to-list-directories-that-havent-been-accessed-since-a-certain-date%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Morgemoulin

                                Scott Moir

                                Souastre