I seem to use find -printf
a lot for these things.
$ which month_files
month_files () {
find . -type f -printf "%TY-%Tm\t%p\n" | grep ^$1 | cut -f2
}
# example run on my ~/.cache directory
$ month_files 2020-05 | wc
1007 1007 49917
Now you can process those files however you like. For example,
mkdir /target/2020-05
month_files 2020-05 | xargs -d"\n" -I % cp % /target/2020-05/
The -I
will force -L 1
(one execution per line). Specifically for the cp
command, you can get better efficiency by using
month_files 2020-05 | xargs -d"\n" cp --target-directory /target/2020-05/
I'm assuming none of the filenames have a newline in them. I don't really hold with that kind of nonsense on my systems, I mean spaces and some special characters are one thing, newlines are quite another. (Or, with apologies to Ian Fleming, "spaces are happenstance; special characters are coincidence, newlines are enemy action"!)