To do this in the first way - you cannot directly process {}
, but you can pass it as an argument to bash
, and then process it as a positional parameter. So instead of:
-exec tar czf {}_$(date +%Y_%m_%d_%H_%M_%S).tar.gz {} \;
You can use:
-exec bash -c 'tar czf "$0_$(date +%Y_%m_%d_%H_%M_%S).tar.gz" -C / "${0#/}"' {} \;
Instead of using -exec
to execute tar
directly, a bash
subshell is called, passing the tar
command as the argument for the -c
option. {}
is passed as the first argument. This is then accessible inside the subshell as $0
. ${0#/}
is a parameter expansion which removes a single slash from the start of $0
.
To do it the second way, you must first redirect stderr so that you can use grep -v
on it, then redirect it back to stderr so that any other error messages will be processed normally. You can add this to the end of your command:
find ... \; 2> >(grep -v "tar: Removing leading" >&2)
This redirects stderr into a process substitution, which then performs the grep
command on the data and redirects it back to stderr - so any lines which grep
matches will be removed, everything else will pass through unchanged.