Here are some dd tricks I've come up with over the years..
Cut-and-Paste on unfriendly tty or non-interactive mode bash
If you're in a situation where EOF/^D/^F is not detected you can use dd to transfer text files to a host. Since it will stop reading after a specified amount of bytes automatically.
I used this as recently as last year during a security exercise where we were able to get non-tty shells on a remote host and needed to transfer files in.
In fact, I even did a couple binary files by base64 encoding them and using a slow, but reliable pure-bash base64 decoding script.
dd of=textfile.txt bs=1 count=<size_of_data_in_paste_buffer>
A super cool trick is that while dd is running, if you send it a USR1 signal, it will emit it's current status (bytes read, bytes per second..)
Universal throughput state filter
I wrote this to act as a pure bash progress filter for any program that emits data through stdout. (Note: Pretty much anything will emit data through stdout - for programs that don't, you can cheat if they don't barf on you using /dev/stdout as a filename. But the idea is basically, every time you get X amount of bytes, print hash marks (like old school FTP when you had hash mode on)
(Note) The progress file thing is lame, this was mostly a proof of concept. If I redid it, I'd just use a variable.
dd bs=$BLKSZ of=${TMPFILE} 2>&1 \
| grep --line-buffered -E '[[:digit:]]* bytes' \
| awk '{ print $1 }' >> ${PROGRESS} &
while [[ $(pidof dd) -gt 1 ]]; do
# PROTIP: You can sleep partial seconds
sleep .5
# Force dd to update us on it's progress (which gets
# redirected to $PROGRESS file.
pkill -USR1 dd
local BYTES_THIS_CYCLE=$(tail -1 $PROGRESS)
local XFER_BLKS=$(((BYTES_THIS_CYCLE-BYTES_LAST_CYCLE)/BLKSZ))
if [ $XFER_BLKS -gt 0 ]; then
printf "#%0.s" $(seq 0 $XFER_BLKS)
BYTES_LAST_CYCLE=$BYTES_THIS_CYCLE
fi
done
slice-and-dice files using anonymous shell filehandles
Here's an extremely pseudo-code example of how you can have a signed tar file that you can extract without errors by providing tar input through an anonymous filehandle - without using any tmp files to store partial file data.
generate_hash() {
echo "yay!"
}
# Create a tar file, generate a hash, append it to the end
tar -cf log.tar /var/log/* 2>/dev/null
TARFILE_SIZE=$(stat -f "%z" log.tar)
SIGNATURE=$(generate_hash log.tar)
echo $SIGNATURE >>log.tar
# Then, later, extract without getting an error..
tar xvf <(dd if=$OLDPWD/log.tar bs=1 count=${TARFILE_SIZE})
The tl;dr is: I find dd to be incredibly useful. And these are just the three examples I can think of off the top of my head.