Bash
There are some nice shortcuts to typing everything all the time. These are called event designators, word designators and modifiers. There is plenty of info on these in the bash man pages, but some real world examples follow:
Download, expand and change directory:
wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz
tar xvf !$:t
cd !$:r:r
Repackaging:
cat myfile-1.0.1.tar.gz | gunzip | bzip2 | !#:1:s/gz/bz2
Forgot to sudo, rerun the previous event with sudo using !!:
nano /etc/hosts
sudo !!
Copying
Copy files merging and preserving permissions:
tar cf - * | ( cd /target; tar xfp -)
Find
Find is a very powerful command and can save a lot of time. Here are a few of my favourite uses:
Finding in files
I have the following script in ~/bin/find_all to find all files excluding some common metadata and generated locations:
#!/bin/bash
# has support for null separator due to usage of spaces in paths
if [ "-0" = "$1" ]; then
print="-print0"
else
print="-print"
fi
prune="( -name target -o -name .svn -o -name .git ) -prune"
match="-type f"
find . $prune -o $match $print
This can then be used to search through the files using grep, e.g.:
find_all | xargs grep TODO
Search and Replace
Find that execs sed on each matched file:
find . -type f -exec sed -i '' 's/FROM/TO/g' {} \;
xmlstarlet
Reformatting xml files
Also known as pretty printing, this makes use of xmlstarlet and I have this script in ~/bin/xmlpp:
#!/bin/bash
files=`find . -type d -name target -prune -o -name *.xml -print`
for file in $files
do
mv $file $file.bak
xml fo $file.bak > $file
rm $file.bak
done
XML Queries
Query xml for particular elements:
curl http://feeds.feedburner.com/railscasts -o rss.xml
xml sel -t -m "/rss/channel/item" -v "title" -n rss.xml > episode-list.txt
xml sel -t -m "/rss/channel/item" -v "enclosure/@url" -n rss.xml > episode-media-urls.txt
Comments
blog comments powered by Disqus