Дана 24/05/11 07:41PM, Walter Alejandro Iglesias написа:
> Today I realized that the loop above is not necesary:
>
> -----------------------------------------------------------
> dirs=$(echo "$files" | grep '/$')
>
> cd && find $dirs | sort | uniq > $source_list
> cd $target && find $dirs | sort | uniq > $target_list
> diff $source_list $target_list |\
> awk '/^> / { print "'$target'/" $NF }' > $delete_list
>
> cat $delete_list | sed 's/^/delete /'
> rm -rf $(cat $delete_list | xargs)
>
> # Clean
> rm $source_list $target_list $delete_list
> ----------------------------------------------------------------
A few notes:
- You don't need a backslash after a pipe (|) or a list operator (||
and &&) - a line ending with a pipe is an incomplete pipeline. So
(with added quoting):
diff "$source_list" "$target_list" |
awk '/^> / { print "'"$target"'/" $NF }' > "$delete_list"
As an example for a list operator, the second line beginning with cd
could also be written as:
cd "$target" &&
find "$dirs" | sort | uniq > "$target_list"
This works even when entering commands interactively from the command
line.
- Before the `rm -rf` line, a useless use of cat[1]:
sed 's/^/delete /' "$delete_list"
- The xargs is unnecessary in `rm -rf $(cat $delete_list | xargs)`;
BTW, that line is vulnerable to weird pathnames (for example,
those including spaces, line feeds and special characters).
[1]: https://porkmail.org/era/unix/award