Testing directories with timestamp:
~] ls -alFh
drwxr-xr-x 11 root root 4.0K Feb 15 10:51 ./
drwxr-xr-x 3 root root 4.0K Feb 15 10:51 ../
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-08/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-09/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-10/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-11/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-12/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-13/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-14/
drwxr-xr-x 2 root root 4.0K Feb 15 11:01 2022-02-15/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-16/
regexp in find
For find a directories with regexp, you can use:
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]'
./2022-02-13
./2022-02-09
./2022-02-14
./2022-02-08
./2022-02-11
./2022-02-12
./2022-02-10
./2022-02-15
Time condition in find
Time conditions in find - the best way is to use -newerXY
-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally 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 specifications 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 examined, this test will fail for any files where the birth time is unknown.
example:
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]' ! -newermt "2022-02-15 10:52:00" -print
./2022-02-13
./2022-02-09
./2022-02-14
./2022-02-08
./2022-02-11
./2022-02-12
./2022-02-10
Delete directories
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]' ! -newermt "2022-02-15 10:52:00" -exec rm -r {} \;
find: ‘./2022-02-13’: No such file or directory
find: ‘./2022-02-09’: No such file or directory
find: ‘./2022-02-14’: No such file or directory
find: ‘./2022-02-08’: No such file or directory
find: ‘./2022-02-11’: No such file or directory
find: ‘./2022-02-12’: No such file or directory
find: ‘./2022-02-10’: No such file or directory
~] ls -alFh
total 16K
drwxr-xr-x 4 root root 4.0K Feb 15 11:13 ./
drwxr-xr-x 3 root root 4.0K Feb 15 10:51 ../
drwxr-xr-x 2 root root 4.0K Feb 15 11:01 2022-02-15/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-16/