This one-liner got me thinking about find:
ls -ltr |grep 'May 12'|awk '{print $9;}'|xargs rm -v
At first glance, I consider it’s bad, but after checking find manpage, I would say it’s not good, but not bad, either. There is no apparent option for searching by file date, however, the functionality is there, -newerXY:
-newerXY reference Compares the timestamp of the current file with reference. The reference argument is nor‐ mally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the comparison. a The access time of the file reference B The birth time of the file reference c The inode status change time of reference m The modification time of the file reference t reference is interpreted directly as a time Some combinations are invalid; for example, it is invalid for X to be t. Some combinations are not implemented on all systems; for example B is not supported on all systems. If an invalid or unsupported combination of XY is specified, a fatal error results. Time specifi‐ cations are interpreted as for the argument to the -d option of GNU date. If you try to use the birth time of a reference file, and the birth time cannot be determined, a fatal error message results. If you specify a test which refers to the birth time of files being exam‐ ined, this test will fail for any files where the birth time is unknown.
It’s long and wordy, basically, you need to replace X and Y with character from the list. X represents the file timestamp and Y is the reference timestamp. Using this test as if asking is X newer than Y.
Here, t is the one to use, the reference is evaluated as if using date -d. For example, you want to find files modified on or after 2012-04-16, you run:
find -newermt '2012-04-15 23:59:59'
As long as it can be parsed by date -d, you can use it. In this case, we use local time. For files modified on 2012-04-16 or before, you run:
find -not -newermt '2012-04-17'
Combining two to form an intersection:
find -newermt '2012-04-15 23:59:59' -not -newermt '2012-04-17'
For a date range, [date_start, date_end], you do:
find -newermt '<date_start - 1 day> 23:59:59' -not -newermt '<date_end + 1 day>'
Finding by specific date is a special case, i.e. date_start == date_end. For deletion, using -delete (Never use | xargs ... or -exec rm {} \;, when there is built-in option for the task):
find -newermt '<date_start - 1 day> 23:59:59' -not -newermt '<date_end + 1 day>' -delete
Now, it’s a better and more efficient one-liner, three pipes to just one command.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.