سؤال

I googled and read a lot of blogs, never find this sort of question. I got a script, compress.sh, which is called via a cronjob. The cronjob executes well, and send a mail to root which is wrapped to my personnal email.

I receive a lot of :

tar: Removing leading `/' from member names
tar: Removing leading `/' from member names

Two solutions and two questions !

FIRST WAY, in the command :

find /dir1/dir2/dir3/ -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d -exec tar czf {}_$(date +%Y_%m_%d_%H_%M_%S).tar.gz {} \;

I know where to place the "-C /" parameter, but I ignore how to cut the leading "/" present in the "{}".

SECOND WAY, in the above command, where to place the grep -v "tar : removing leading ..."in order to filter and send the specific error message to /dev/null ?

Note that i'd prefer the first (the correct) way, but i wonder how to work with the second too.

NB : it is because I guess that if it is the only error message, once filtered, I will not receive a mail ... only if one other problem occurs.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

First way:

You can use bash to remove the initial /.

find /dir1/dir2/dir3 -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d -exec bash -c 'a="{}" ; a=${a#/} ; tar czf $a_$(date +%Y_%m_%d_%H_%M_%S).tar.gz $a' \;

Second way:

You can pipe standard error to grep and filter out the message you don't want.

blah |& grep -v "Removing leading"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top