Nix: Echo and sudo for files owned by root
If you’re not root, and you want to modify a file owned by root (say
/root/tarsnap-backup.sh
), this won’t work:
sudo echo '#!/bin/sh
/usr/bin/tarsnap -c \
-f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \
/home/simon/team-storage' > /root/tarsnap-backup.sh
But this will:
echo '#!/bin/sh
/usr/bin/tarsnap -c \
-f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \
/home/simon/team-storage' | sudo tee /root/tarsnap-backup.sh > /dev/null
Via @simonw.
Leave a comment