Linux shell commands
I do lots of scripting. Sometimes I save an intersting or commonly used command here.
[edit] Echo shell commands
At times I want to echo a command before execution. This is very useful in script debugging. set -x does this automatically; set +x disables.
[edit] Mirror web pages
Instructions on mirroring a single page using wget(1):
Actually, to download a single page and all its requisites (even if they exist on separate websites), and make sure the lot displays pro- perly locally, this author likes to use a few options in addition to -p: wget -E -H -k -K -p http://server.com/path/
[edit] In-place word replacement with Perl
Perl regex replaces across lines from the command line:
perl -0pi -e 's{Apples}{Oranges}' file
[edit] Rip audio CD
LAME encoding. Stereo, constant bit rate (192Kbps), high-quality MPEG-1 layer 3.
cdparanoia -B -d /dev/dvd for wav in *.wav ; do lame -m s --cbr -b 192 -h $wav $wav.mp3 && rm $wav done
[edit] IDE hotswap
sync umount /dev/hdN* hdparm -U /dev/hdN (remove old drive, put in new drive) hdparm -R /dev/hdN mount /dev/hdN*
[edit] Calculate total from stdin
Add from stdin. Credit.
ls -al | awk '{sum = sum + $5} END {print sum}'
[edit] Scp named files matching regex
Crufted together this one today:
grep -l -e staff@bwerp -e support@bwerp * | xargs tar cz | ssh aziz.bwerp.net 'tar xz'
Grepped a list of e-mails for two regexes, then tar'ed them with the help of xargs and copied them to a remote macine with SSH. I couldn't scp the files directly, since they had the colon character (":") in the name and scp kept balking at me.
[edit] Sub-grep a grep
Here's another use of grep:
grep -rilZ jaharmi Maildir | xargs -0 grep -i password
I couldn't remember my password on Jeremy's website, so I grepped all of my e-mail files mentioning "jaharmi" for lines containing "password". (Yet another advantage of using Maildir.)
[edit] List all files within *.tar
List all the files in a list of tar archives:
ls *.tgz | while read line ; do
tar tfz $line | while read lin ; do
echo $line: $lin
done
done
[edit] Convert PNG to PDF
Convert a batch of PNG files to a PDF:
for img in *.png ; do
base=$(basename "$img" .png)
convert -page 100% -density 360x360 "$img" "${base}.ps"
done
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOUTPUTFILE=assembled.pdf -r360 -g1816x1686 *.ps
Substitute the appropriate image resolution as the argument to -density and -r, and the image width and height to -g. The PDF wants all pages to be the same size, so I wouldn't changing the size between images. These steps will yield a full-resolution PDF sized to the image.
[edit] Method 2
You can combine PDFs instead of postscript files.
for img in File*.gif ; do base=$(basename "$img" .gif); convert -density 200x200 "$img" "${base}.pdf"; done
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=assembled.pdf -r200 File*.pdf
Watch out for name collision in the second line if you run it multiple times. (Don't try to add a PDF to itself.)
[edit] Export screen(1) window number to remote shell
screen(1) assigns a number to each window. This window number can be displayed within the shell prompt:
bash-3.0$ PS1='\u@\h[$WINDOW]:\w\$ ' adam@shed[0]:~$
This screen can be exported to remote shells opened via SSH:
SSH_VAR_EXPORT="'export WINDOW=$WINDOW && cd && exec /bin/bash --login'"
function alias_ssh { eval alias $1=\"ssh -t $2 $SSH_VAR_EXPORT\" ; }
alias_ssh home machine.isp.net
home will now open an SSH connection, exporting the current window number.
[edit] Setting Terminal Window Titles
echo -ne "\e]2;my new title\a"
The following will update the active tab label in iTerm for Mac OS X:
echo -ne "\e]2;my tab label\a"