On CBS.com: Watch Paul McCartney perform now

Linux and Open Source

Host: Jack Wallen
Contact

Here are some 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:

$ ~/foo.sh
-bash: /home/vdanen/foo.sh: Permission denied
$ sh !!
sh ~/foo.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. Read his full bio and profile.

Print/View all Posts Comments on this blog

GUI vs CLI wittstock_mark@... | 11/11/08
it's not preference but function... dan_wang@... | 11/11/08
It's a case of learning as you go j-mart@... | 11/11/08
Windows CLI Searches Are Faster Too! TheGratefulNed@... | 11/11/08
Yeah, it's preference. apotheon | 11/11/08
Re: Yeah, it's preference. eclypse | 11/13/08
Depends on the job more than personal preference pointzerotwo@... | 11/12/08
thanks for the tips! csmith.kaze | 11/11/08
"not as intuitive" apotheon | 11/11/08
Bring the Power of Linux CLI tools to Windows Scripts david.hunt@... | 11/11/08
Yes and no . . . apotheon | 11/12/08
RE: Tips and tricks for working on the command line idallen@... | 11/11/08
RE: Tips and tricks for working on the command line domster83@... | 11/11/08
Install Microsoft PowerShell neilb@... | 11/11/08
RE: Tips and tricks for working on the command line davidmaxwaterman+techrepublic@... | 11/11/08
RE: Tips and tricks for working on the command line ron.carlton@... | 11/11/08
Meanwhile . . . apotheon | 11/11/08
true enough ron.carlton@... | 11/18/08
Some command lines allow for point-and-shoot operations. rsteiner@... | 11/12/08
4DOS and 4OS2... fond memories vdanen | 11/13/08
RE: Tips and tricks for working on the command line raymondday@... | 11/12/08

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Archives

TechRepublic Blogs



500 Things Every Technology Professional Needs to Know
Did you know Microsoft's RegClean does not work with XP but you can use shareware to clean your registry? Did you know most wireless access points don't have encryption enabled by default? Did you know there are 500 tidbits of information contained in TechRepublic's 500 Things Every Technology Professional Needs to Know that will help you become a successful IT professional.
Buy Now
IT Help Desk Survival Guide, Third Edition
TechRepublic's IT Help Desk Survival Guide, Third Edition provides tools and recommendations to help you better manage help desk services, improve end-user support, troubleshoot frustrating hardware issues, identify quick fixes to vexing Windows problems, and help users make the most of Microsoft Office 2003.
Buy Now

Smartphones

advertisement
Click Here