As another answer already explains, -exec
is an action for find
, you can't use it as a shell command. On contrary, xargs
and grep
are commands, and you can't use them as find
actions, just like you can't use pipe |
inside find
.
But more importantly, even though you could use ls
and grep
on find
's result just to move files older than some amount of time, you shouldn't. Such pipeline is fragile and fails on many corner cases, like symlinks, files with newlines in name, etc.
Instead, use find
. You'll find it quite powerful.
For example, to mv
files modified more than 7 days ago, use the -mtime
test:
find -name 'fsimage*' -mtime +7 -exec mv '{}' /some/dir/ \;
To mv
files modified on a specific/reference date, e.g. 2017-10-20
, you can use the -newerXY
test:
find -name 'fsimage*' -newermt 2017-10-20 ! -newermt 2017-10-21 -exec mv '{}' /some/dir/ \;
Also, if your mv
supports the -t
option (to give target dir first, multiple files after), you can use {} +
placeholder in find
for multiple files, reducing the total number of mv
command invocations (thanks @CharlesDuffy):
find -name 'fsimage*' -mtime +7 -exec mv -t /some/dir/ '{}' +