Looking awesome with one liners
Let's face it: there comes a time in ever developer's life when they want to start looking like an awesome *nix guru. Nothing impresses other newbie developers more than typing a seemingly massive command into a *nix shell and having magic happen.
Let me give you an example. I've got a whole bunch of modified files in an Subversion checkout that I want to revert. The obvious choice is to go for:
# svn revert -R *
But why stop there? Why not make yourself look awesome at the same time? You could easily have reverted those files with a mass of piped commands instead - looking awesome in the process.
Let's get a list of modified files, first:
# svn status
Let's grep out any modified files. We know that the line of any modified files starts with a big, fat capital 'M'.
# svn status | grep -e '^M'
Awesome! Now we're only showing the modified files! We've still got that big, fat ol' 'M' at the start of each line though. Let's get rid of it by using 'awk' to print only the second part of each line:
# svn status | grep -e '^M' | awk '{print $2}'
Wahey! We're almost there. All we need to do now is pass the output to the 'svn revert' command. We can do that by wrapping it all in back-ticks (`). Anything enclosed in back-ticks will be evaluated by the shell before the rest of the command is run:
# svn revert `svn status | grep -e '^M' | awk '{print $2}'`
Now we're really awesome! Not only have we reverted the modified files, but we looked awesome in the process!
Just remember: it's not all about looking awesome. You've actually learnt something today: how to use awk, grep and back-ticks (to look awesome).
First posted: Fri, 22 May 2009 21:01:14 +0000