find . -type f -newermt 2021-09-17
prints one per line, the regular (f
type) files whose last-m
odification time is newer
than the 2021-09-17T00:00:00.000000000
local t
ime, in the current directory or in any level of subdirectories (but not following symlinks). That includes files last-modified on that day, but also later (including those with modification times in the future).
That output is not post-processable reliably¹. If you wanted to delete those files, you'd use -exec rm -f {} +
in find
, or -delete
supported by a few find
implementations, most of those that do support -newermt
anyway.
But in any case, yes, it would delete any file it is able to, including configuration files, your documents, pictures, etc including hidden ones or the ones in hidden directories.
If you wanted to exclude hidden files (including those in hidden directories such as ~/.config
), and only delete files last-modified on that day (and not later), you'd need:
LC_ALL=C find . -name '.?*' -prune -o \
-type f -newermt 2021-09-17 ! -newermt 2021-09-18 -exec rm -f {} +
You can't use -delete
there as -delete
(which implies -depth
) is incompatible with -prune
.
But even then, I would not run that blindly on your home directory.
If you wanted to only delete the files in the current directory and not in subdirectories, you could make it:
LC_ALL=C find . -maxdepth 1 ! -name '.*' \
-newermt 2021-09-17 ! -newermt 2021-09-18 -type f -delete
If using the zsh
shell, you can also do:
autoload age
rm -f -- *(.e['age 2021-09-17'])
zsh globs do skip hidden files by default contrary to find
.
Or recursively (still skipping hidden files and dirs):
rm -f -- **/*(.e['age 2021-09-17'])
¹ as the files are newline delimited and newline is as valid as any a character in a file name. More about that at Why is looping over find's output bad practice?
find
command? In short, is there some additional way to tell these files apart from files that you do not want to remove? – Kusalananda Sep 20 '21 at 14:32-name "[^.]*"
to the search filter, at least for GNUfind
– Philippos Sep 20 '21 at 14:38find | xargs
in the way that you are suggesting, asxargs
will split filenames on whitespace and potentially delete the wrong files. (There are easy solutions to this.) – Chris Davies Sep 20 '21 at 15:10$HOME/.cache
directory mentioned by the OP. Perhaps adding-maxdepth 1
to your suggestion would be beneficial. – Chris Davies Sep 20 '21 at 15:11-name '[^.]*'
or its POSIX equivalent-name '[!.]*'
would also fail to match non-hidden files whose name is not valid text in the locale. – Stéphane Chazelas Sep 21 '21 at 07:12