On Sun May 12 10:07:30 2024 Страхиња Радић wrote:
> 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"

I know, just fingers habit. :-)

>
>   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).
> 

What about the following, better?

---------------------------------------------
# Remove files from target directory
date=$(date +%H%M%S)
delete_list=/tmp/delete_$date
source_list=/tmp/source_$date
target_list=/tmp/target_$date

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

cd &&
if [ -s $delete_list ]; then
        echo "Deleting on ${target}:"
        rm -vrf $(cat $delete_list)
fi

# Clean
rm $source_list $target_list $delete_list
---------------------------------------------


Thanks for your recomendations!


-- 
Walter

Reply via email to