Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Piping echo to a file owned by root using sudo and tee#
I wanted to create a file with a shell one-liner where the file lived somewhere owned by root.
I tried this but it didn’t work:
1sudo echo '#!/bin/sh2/usr/bin/tarsnap -c \3 -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \4 /home/simon/team-storage' > /root/tarsnap-backup.shRunning echo using sudo didn’t pass through to the > filename bit.
Here’s what did work:
1echo '#!/bin/sh2/usr/bin/tarsnap -c \3 -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \4 /home/simon/team-storage' | sudo tee /root/tarsnap-backup.sh > /dev/nullNo need for sudo on the echo - but it pipes the output to sudo tee which can then write the file to disk.
The > /dev/null at the end supresses any output from the tee command. If you want to see the output you can do this instead:
1echo '#!/bin/sh2/usr/bin/tarsnap -c \3 -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \4 /home/simon/team-storage' | sudo tee /root/tarsnap-backup.shNewsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.