Open Source

Tips and tricks for working on the command line

Vincent Danen believes in the power of the command line as being faster and more powerful than a GUI, once you get the hang of it. Here are some of his favorite tips and shortcuts for working from the CLI.

Anyone that has been reading these tips for any length of time knows that I am a command-line guy. It's faster, more efficient, and more powerful. Sure, a nice GUI is great to look at, but to really get things done, give me the CLI any day. Even to this day I still use a text-mode e-mail client (Mutt) and text-mode IRC client (Irssi). GUI clients that supposedly offer the same functionality do not, and are irritating at best.

Having said that, the CLI can be intimidating and obscure, but it doesn't need to be. There are a number of tips and tricks that can be accomplished on the CLI and so this tip will provide a few of them.

Renaming files is one thing that a good GUI file manager appears to have over the CLI. Usually clicking on a file name will give you the opportunity to edit the name in order to rename it, which is great for files with long names. This can be done easily in a shell too, either by using tab expansion or with this little trick:

$ mv thisisareallylongfilename{,.txt}

This will move the file "thisisareallylongfilename" to "thisisareallylongfilename.txt" without having to type it twice. Directly on the CLI this may not look so fantastic due to tab expansion, but in a shell script, this can make renaming batches of files extremely simple. If you need to rename files with extensions, it is just as easy:

$ mv foo.{jpeg,jpg}

This renames foo.jpeg to foo.jpg.

The rename command is also useful for renaming files in batches. For instance, suppose you had a directory of .php4 files you wanted to rename to .php. The rename command does this quite easily:

$ touch 1.php4 2.php4 3.php 4.php4
$ rename .php4 .php *.php4
$ ls *php*
1.php  2.php  3.php  4.php

Shell expansion characters are often overlooked as well. For instance, if you execute a command and find it needs something extra, you can use the !! character sequence to bring the command back, unedited. For instance, if you try to execute a shell script that does not have the executable bit set, you might see:

$ ~/
-bash: /home/vdanen/ Permission denied
$ sh !!
sh ~/

Other applications for this sequence include re-executing a command via sudo for elevated privileges.

Another set of useful character sequences are:

  • !* which will repeat the last command's arguments (whereas !! does the entire command)
  • !$ to print the final argument
  • !:3 to print the third argument of the previous command.

For instance:

$ ls /tmp/somedir
ls: cannot access /tmp/somedir: No such file or directory
$ mkdir -p !*
mkdir -p /tmp/somedir
$ touch 1 2 3
$ cp !:2 /tmp/somedir
cp 2 /tmp/somedir

The final tip here is to point to one of my favorite Web resources. The [shell-fu]$ Web site currently contains nearly four hundred shell-related tips and is definitely a worthwhile read for anyone interested in becoming more efficient with the CLI.

Get the PDF version of this tip here.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!


Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks