Wednesday, 15 April 2015

bash - Listing files that are older than one day in reverse order of modification time -


To write a cleanup script on a directory, I should see all the files that are older than one day. In addition, I need to remove them in the reverse order of modified time (first of all first) until reaching a certain size.

I came with the following approach to the list of files:

  Find -Mat I +1 -exec ls -a1rt {} +  

Am I correct, this does not work for a large number of files (because more than one 'LS' will be executed)? How do I achieve my goal in that case?

You can use the following command to find the 10 oldest files:

< Pre-> Find -Mtm +1-type f -printf '% T @% p \ n' | Sort-N | Head-10 The steps used: For each file returned by
  • search , we print the amendment
  • Then we numerically timestamp Sort by.
  • We take the first 10.
  • We only print the filename part.

Later if you want to remove them, you can:

  rm $ (...) < / Code> 

where ... is the order described above.


No comments:

Post a Comment