Recursively find ONLY the latest logs in all sub-folders and save to file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I am working on a RHEL7 box. I need to recursively find all the LATEST (and only the latest) .log files in each sub-folder below a certain hierarchy and list their full paths in a file 'all_logs.txt'.



My find command looks like this and works to return full paths to ~6000 records:



cd $SEARCHDIR
find . -iname computer_import_*.log > all_logs.txt


The problem here is that this find command is finding ALL computer_import_.log where I just need the latest computer_import_.log file in each sub-folder searched.










share|improve this question



























    up vote
    -1
    down vote

    favorite












    I am working on a RHEL7 box. I need to recursively find all the LATEST (and only the latest) .log files in each sub-folder below a certain hierarchy and list their full paths in a file 'all_logs.txt'.



    My find command looks like this and works to return full paths to ~6000 records:



    cd $SEARCHDIR
    find . -iname computer_import_*.log > all_logs.txt


    The problem here is that this find command is finding ALL computer_import_.log where I just need the latest computer_import_.log file in each sub-folder searched.










    share|improve this question

























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I am working on a RHEL7 box. I need to recursively find all the LATEST (and only the latest) .log files in each sub-folder below a certain hierarchy and list their full paths in a file 'all_logs.txt'.



      My find command looks like this and works to return full paths to ~6000 records:



      cd $SEARCHDIR
      find . -iname computer_import_*.log > all_logs.txt


      The problem here is that this find command is finding ALL computer_import_.log where I just need the latest computer_import_.log file in each sub-folder searched.










      share|improve this question















      I am working on a RHEL7 box. I need to recursively find all the LATEST (and only the latest) .log files in each sub-folder below a certain hierarchy and list their full paths in a file 'all_logs.txt'.



      My find command looks like this and works to return full paths to ~6000 records:



      cd $SEARCHDIR
      find . -iname computer_import_*.log > all_logs.txt


      The problem here is that this find command is finding ALL computer_import_.log where I just need the latest computer_import_.log file in each sub-folder searched.







      awk sed grep find






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      38.2k1475123




      38.2k1475123










      asked 2 days ago









      SSDdude

      726




      726




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          I tried with below command and it worked fine



          find . -type f -iname "computer_import_*.log" -daystart -mtime -1 > all_logs.txt





          share|improve this answer




















          • I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
            – SSDdude
            2 days ago










          • Will check and update
            – Praveen Kumar BS
            2 days ago

















          up vote
          0
          down vote













          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 > all_logs.txt


          This will find all files that match the -iname argument, restricted to being modified within 10-days of the run. See the find manpages for more details.



          Note that the -iname argument is quoted so that find interprets the argument rather than the shell before find runs.



          If you want to see what's returned based on these criteria do, for example:



          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 -exec ls -ld +


          This will perform an ls on each file returned, thus showing the last modification date. Change the value of -mtime <+-N> to your taste. A value of -90 would return files modified in the last 90 days. A positive value of +90 would return files modified over 90 days ago.



          ** ALTERNATIVE INTERPRETATION **



          Given the requirement to find only the most recent file in any and all sub-directories, do something like this:



          find $SEARCHDIR -type d > /var/tmp/mydirs
          while read -r line
          do
          ls -alt "$line" | grep -Ev "^(d|total)" | head -1 >> /var/tmp/mylogs
          done < /var/tmp/mydirs


          The ls orders the contents of a directory as newest to oldest modification date. The grep eliminates directory entries and total size lines. head snips the first file (if any) from the directory being examined.






          share|improve this answer






















          • Thx, but this wont work because I could have the latest log be 3mo old or more.
            – SSDdude
            2 days ago










          • All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
            – SSDdude
            2 days ago










          • See the alternative interpretation update.
            – JRFerguson
            2 days ago










          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',
          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%2f482161%2frecursively-find-only-the-latest-logs-in-all-sub-folders-and-save-to-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          I tried with below command and it worked fine



          find . -type f -iname "computer_import_*.log" -daystart -mtime -1 > all_logs.txt





          share|improve this answer




















          • I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
            – SSDdude
            2 days ago










          • Will check and update
            – Praveen Kumar BS
            2 days ago














          up vote
          0
          down vote













          I tried with below command and it worked fine



          find . -type f -iname "computer_import_*.log" -daystart -mtime -1 > all_logs.txt





          share|improve this answer




















          • I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
            – SSDdude
            2 days ago










          • Will check and update
            – Praveen Kumar BS
            2 days ago












          up vote
          0
          down vote










          up vote
          0
          down vote









          I tried with below command and it worked fine



          find . -type f -iname "computer_import_*.log" -daystart -mtime -1 > all_logs.txt





          share|improve this answer












          I tried with below command and it worked fine



          find . -type f -iname "computer_import_*.log" -daystart -mtime -1 > all_logs.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Praveen Kumar BS

          1,055138




          1,055138











          • I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
            – SSDdude
            2 days ago










          • Will check and update
            – Praveen Kumar BS
            2 days ago
















          • I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
            – SSDdude
            2 days ago










          • Will check and update
            – Praveen Kumar BS
            2 days ago















          I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
          – SSDdude
          2 days ago




          I should have been more clear sorry... it does work and returns ~70 records and they all look like the latest. The problem is there should be ~400 records returned by the command. My speculation is that this is because I could easily have the latest record be from last year. Perhaps its working for you because you don't have records that old? Thx very new at this!
          – SSDdude
          2 days ago












          Will check and update
          – Praveen Kumar BS
          2 days ago




          Will check and update
          – Praveen Kumar BS
          2 days ago












          up vote
          0
          down vote













          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 > all_logs.txt


          This will find all files that match the -iname argument, restricted to being modified within 10-days of the run. See the find manpages for more details.



          Note that the -iname argument is quoted so that find interprets the argument rather than the shell before find runs.



          If you want to see what's returned based on these criteria do, for example:



          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 -exec ls -ld +


          This will perform an ls on each file returned, thus showing the last modification date. Change the value of -mtime <+-N> to your taste. A value of -90 would return files modified in the last 90 days. A positive value of +90 would return files modified over 90 days ago.



          ** ALTERNATIVE INTERPRETATION **



          Given the requirement to find only the most recent file in any and all sub-directories, do something like this:



          find $SEARCHDIR -type d > /var/tmp/mydirs
          while read -r line
          do
          ls -alt "$line" | grep -Ev "^(d|total)" | head -1 >> /var/tmp/mylogs
          done < /var/tmp/mydirs


          The ls orders the contents of a directory as newest to oldest modification date. The grep eliminates directory entries and total size lines. head snips the first file (if any) from the directory being examined.






          share|improve this answer






















          • Thx, but this wont work because I could have the latest log be 3mo old or more.
            – SSDdude
            2 days ago










          • All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
            – SSDdude
            2 days ago










          • See the alternative interpretation update.
            – JRFerguson
            2 days ago














          up vote
          0
          down vote













          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 > all_logs.txt


          This will find all files that match the -iname argument, restricted to being modified within 10-days of the run. See the find manpages for more details.



          Note that the -iname argument is quoted so that find interprets the argument rather than the shell before find runs.



          If you want to see what's returned based on these criteria do, for example:



          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 -exec ls -ld +


          This will perform an ls on each file returned, thus showing the last modification date. Change the value of -mtime <+-N> to your taste. A value of -90 would return files modified in the last 90 days. A positive value of +90 would return files modified over 90 days ago.



          ** ALTERNATIVE INTERPRETATION **



          Given the requirement to find only the most recent file in any and all sub-directories, do something like this:



          find $SEARCHDIR -type d > /var/tmp/mydirs
          while read -r line
          do
          ls -alt "$line" | grep -Ev "^(d|total)" | head -1 >> /var/tmp/mylogs
          done < /var/tmp/mydirs


          The ls orders the contents of a directory as newest to oldest modification date. The grep eliminates directory entries and total size lines. head snips the first file (if any) from the directory being examined.






          share|improve this answer






















          • Thx, but this wont work because I could have the latest log be 3mo old or more.
            – SSDdude
            2 days ago










          • All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
            – SSDdude
            2 days ago










          • See the alternative interpretation update.
            – JRFerguson
            2 days ago












          up vote
          0
          down vote










          up vote
          0
          down vote









          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 > all_logs.txt


          This will find all files that match the -iname argument, restricted to being modified within 10-days of the run. See the find manpages for more details.



          Note that the -iname argument is quoted so that find interprets the argument rather than the shell before find runs.



          If you want to see what's returned based on these criteria do, for example:



          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 -exec ls -ld +


          This will perform an ls on each file returned, thus showing the last modification date. Change the value of -mtime <+-N> to your taste. A value of -90 would return files modified in the last 90 days. A positive value of +90 would return files modified over 90 days ago.



          ** ALTERNATIVE INTERPRETATION **



          Given the requirement to find only the most recent file in any and all sub-directories, do something like this:



          find $SEARCHDIR -type d > /var/tmp/mydirs
          while read -r line
          do
          ls -alt "$line" | grep -Ev "^(d|total)" | head -1 >> /var/tmp/mylogs
          done < /var/tmp/mydirs


          The ls orders the contents of a directory as newest to oldest modification date. The grep eliminates directory entries and total size lines. head snips the first file (if any) from the directory being examined.






          share|improve this answer














          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 > all_logs.txt


          This will find all files that match the -iname argument, restricted to being modified within 10-days of the run. See the find manpages for more details.



          Note that the -iname argument is quoted so that find interprets the argument rather than the shell before find runs.



          If you want to see what's returned based on these criteria do, for example:



          find $SEARCHDIR -type f -iname "computer_import_*.log" -mtime -10 -exec ls -ld +


          This will perform an ls on each file returned, thus showing the last modification date. Change the value of -mtime <+-N> to your taste. A value of -90 would return files modified in the last 90 days. A positive value of +90 would return files modified over 90 days ago.



          ** ALTERNATIVE INTERPRETATION **



          Given the requirement to find only the most recent file in any and all sub-directories, do something like this:



          find $SEARCHDIR -type d > /var/tmp/mydirs
          while read -r line
          do
          ls -alt "$line" | grep -Ev "^(d|total)" | head -1 >> /var/tmp/mylogs
          done < /var/tmp/mydirs


          The ls orders the contents of a directory as newest to oldest modification date. The grep eliminates directory entries and total size lines. head snips the first file (if any) from the directory being examined.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          JRFerguson

          9,54732329




          9,54732329











          • Thx, but this wont work because I could have the latest log be 3mo old or more.
            – SSDdude
            2 days ago










          • All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
            – SSDdude
            2 days ago










          • See the alternative interpretation update.
            – JRFerguson
            2 days ago
















          • Thx, but this wont work because I could have the latest log be 3mo old or more.
            – SSDdude
            2 days ago










          • All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
            – SSDdude
            2 days ago










          • See the alternative interpretation update.
            – JRFerguson
            2 days ago















          Thx, but this wont work because I could have the latest log be 3mo old or more.
          – SSDdude
          2 days ago




          Thx, but this wont work because I could have the latest log be 3mo old or more.
          – SSDdude
          2 days ago












          All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
          – SSDdude
          2 days ago




          All I appreciate all the feedback but still no luck. It is sounding to me that there is no real good way to search sub-dirs. and extract ONLY the SINGE absolute latest log file in said directory. None of these answers do that. Thx.
          – SSDdude
          2 days ago












          See the alternative interpretation update.
          – JRFerguson
          2 days ago




          See the alternative interpretation update.
          – JRFerguson
          2 days ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482161%2frecursively-find-only-the-latest-logs-in-all-sub-folders-and-save-to-file%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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?