Unix How-To: Nifty One-Liners

Today's collection of one-liners are sure to win some converts. There's nothing like a quick command that saves you a lot of trouble and just might impress your boss and coworkers that you're the cleverest person around.

The first is the cd -" command that takes you to back to whatever directory you were previously located in. Need to bounce between two directories as you make some mighty edits to some configuration files? No problem. With the "cd -" command, you can do just that.

$ cd /usr/local/apps/app1/bin
$ cd -
$ vi myapp.cfg
$ cd -
$ pwd

The next little cutie may be familiar to many. Using commands such as ^this^that^ will change a string (this) in the command just typed to a second string (that) and reissue the command. This can be a great time saver when you're entering long, convoluted commands and need to change one small part of it to take care of the next task in your queue.

$ /usr/local/apps/app1/adduser jdoe

The third one-liner will make a backup of a file with just a few keystrokes. In this example, myfile is copied to myfile.bak.

$ cp myfile{,.bak}

Need to populate a directory with a bunch of files for testing purposes? The following two commands create a list of three-letter file names (aaa to ccc) and then add the files to your directory. The trick is in the {a,b,c} syntax that utilizes brace expansion. As a result of these commands, 27 files (aaa, aab, aac, aba, abb ...) are added to the directory. Obviously, a, b and c can be replaced by any other file name components you might want to use.

$ echo {a,b,c}{a,b,c}{a,b,c}
$ touch {a,b,c}{a,b,c}{a,b,c}
$ ls {a,b,c}{a,b,c}{a,b,c}
aaa  aac  abb  aca  acc  bab  bba  bbc  bcb  caa  cac  cbb  cca  ccc
aab  aba  abc  acb  baa  bac  bbb  bca  bcc  cab  cba  cbc  ccb

The last tidbit for today makes use of the ! for repeating a command that has been recorded in your command history. However, it adds a ":p" to the command which will cause the command to be displayed, but not run. If you want to repeat a command, but want to double check that the command you want to run is really the one you're thinking about, this little trick might become one of your favorites.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2010 IDG Communications, Inc.

IT Salary Survey: The results are in