Linux

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:

$ ~/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!

About

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.

22 comments
FatNGristle
FatNGristle

I don't see it mentioned often, but XP has two command interpreters with slightly different command sets: Command.com and CMD.exe I find Command.com more limiting, but note that Windows has a setting for which is the default, so when scripting you'll want to specify in your script which to use or else you may get some users whose pcs behave differently. I'm not particularly knowledgable on it, I've just come across it before.

raymondday
raymondday

On my Ubuntu server I have a text file and to see if I just type "cat cat(tab)" (tab) is the tab key that fills the rest of the text. Here is my file and it my help others on the command line. root@small:~# cat cat4commands cp -rpf /from /to (copy dir) df (show mounted partitions) find / -name name_of_file (find name_of_file starting at / could put /var to start there) rm -rf filename (delete all even dir's with out delete files in them first) rmdir dir_name (junk when you have to delete files in it first) mv oldname newname (renames files) mv /tmp /keep (would move tmp folder to keep folder. Can do file too) ln -s dir_to_link_to dir_to_put_it (made a symbolic link) .tar.gz (do gzip -d filename) (Or put in folder and do: tar -zxvf filename) .tar (do tar -xvf filename) .tgz (do tar -xvzf filename) touch filename (made a new blank file) service smb restart (restart samba windows file server) locate .mpg (find names fast. ".mpg" will find all the mpg videos) top (shows lots of stuff running all ready q quits it) ps -A (list all the things running) btdownloadcurses --max_upload_rate 10 --url http://domain-name-and-path-to-torrent (start P2P my max upload_rate is about 50) screen btdownloadcurses... (start in a new screen. Ctrl-A followed by d to exit out. screen -r to get back in. Or screen -r number is more then 1 screen.) apt-get update apt-get dist-upgrade (This will upgrade Ubuntu. You don't have to reinstall a new core.) apt-get update apt-get upgrade (will upgrrade, but not a new core.) scp -rp 192.168.2.110:/var/www/"Web\ pages" /var/www/ (copy server to server /var/www/Web pages to /var/www/ on 192.168.2.110) irssi (start irc [/connect irc.eskimo.com] then [/join c64friends]) /usr/lib/cgi-bin/awstats.pl -config=domain.tld -update (updates awstats web page) aptitude (Menu-driven, text-based atp-get system) noip2 -S (check noip what IP and name it has) arp -a (To see al IP's on LAN) root@small:~# I named my server small because it's runing on a mini-ITX board and I made the case for it the same size as the mini-ITX board. -Raymond Day

rsteiner
rsteiner

One classic example is JP Software's 4DOS, 4NT, and 4OS2 shells for DOS, Windows, and OS/2. The SELECT command could be used to display a point-and-shoot screen of files matching a filespec, and then one could use arrows to move around and the spacebar key to toggle which files from the specified select list to use as the target of the operation. This means that point-and-shoot aliases like these simple examples (from my own 4DOS and 4OS2 alias file) could be created as follows: TCOPY=select copy (*.*) %1 TMOVE=select move (*.*) %1 TDEL=select del (*.*) TRUN=select %1 (*.*) TEDIT=trun fte TLIST=trun list UNZ=f:\unz.cmd TUNZ=select unz (*.zip) The filespec in parenthesis limits the files which appear in the selection list, so the TUNZ command will only show .ZIP files as potential targets. It's like mixing Midnight Commander panels and bash together. :-)

ron.carlton
ron.carlton

Too often in Linux command line is the only way to edit the config options for otherwise GUI application. As a long term developer I learned a long time ago that software either has to be "easy to use" or "easy to learn." If it is done often it has to be easy to use, if it is done infrequently then it has to be easy to learn. And that is the rub in Linux, using the command line to configure an app is not at all easy to learn. In fact some of the commands I have come across for configuration are just downright goofy.

davidmaxwaterman+techrepublic
davidmaxwaterman+techrepublic

You missed the ':p' thing. This will print the command from history without executing it; but it will put it in history as the previous command, so you can then edit it using whatever method you need - I mostly use it as a safeguard to use in preference to just entering '!325' - instead entering '!325:p' which will allow me to then 'up-arrow' to check I got the right one, and/or edit it before actually executing it.

domster83
domster83

I love the command line. Its quick a dirty and its usually easier to get more info together in one place. For the MS side, the PSTools from Microsoft are a really good tool, for remotely quitting processes, executing files (such as bat files for ad-hoc awkward installations or anything else really), or for getting a lot of info on remote machines. I just wish that XP had a unix cli by default

idallen
idallen

Ubuntu's "rename" command has a different syntax: rename perlexpr [ files ] So you say: $ rename 's/\.php4$/.php/' *.php4

csmith.kaze
csmith.kaze

I have jumped feet first into Linux over the past year and have learned the value of using command line for just about everything. I learned in a Windows-only College, so having to shift gears after school (graduated in May) was not easy for me, though now I am even trying to do all of my stuff in Windows on Command Line, too. :) I feel that, personally, it is faster and, while not as intuitive, it can be easier to type five words as opposed to clicking 17 times. Anyway, thanks for the tips!

wittstock_mark
wittstock_mark

I disagree as I think it is a personal preference. I am able to accomplish tasks much faster with the gui then can otherwise be acheived with the cli.

vdanen
vdanen

If there was one thing that absolutely had to be installed on any DOS or OS/2 system I was using, it was 4DOS and 4OS2. I loved those shells! Never used 4NT... I pretty much bypassed windows altogether, but 4DOS and 4OS2 were the absolute best. Thanks for the nostalgia-prompting! =)

apotheon
apotheon

I find that, in many cases, configuration is neither easy to use nor easy to learn in MS Windows, because so much of system configuration is done through the Registry. The moral of the story is simple: The GUI is not the Universal Solvent.

neilb
neilb

I think that you'll be pleasantly surprised. :)

apotheon
apotheon

"[i]I feel that, personally, it is faster and, while not as intuitive, it can be easier to type five words as opposed to clicking 17 times.[/i]" With greater familiarity will come a greater sense of its "intuitiveness", at least if you're using a well designed CLI. I'm afraid that a DOS-like interface may never entirely measure up to that of an MS Windows style GUI interface for intuitiveness, if only because it lacks so many of the tools we on the Unix side of the hedge know and love (like manpages, powerful shell features, and better text search tools).

pointzerotwo
pointzerotwo

Even for someone comfortable with both GUIs and CLIs, the best tool for the job depends on the job. To change one or two letters in the middle of a 30-char filename, a GUI is probably 5 times faster. To rename 50 files from *.htm to *.html, a CLI is probably 50 times faster.

apotheon
apotheon

If you prefer to search for the way to do something over and over again, using the GUI is definitely the way to go. When you don't remember how to do things, and don't want to learn it so you'll remember it next time, it's often quicker and easier to get things done in this manner with a point and click GUI than with a CLI. If you prefer to know what you're doing -- and, if you don't know yet, to learn what you're doing so you'll know it last time -- it's much quicker and easier to get things done with a CLI where you can just tell the computer "do this", rather than having to meander through a series of menus and icon clicks like you do with a point and click GUI. So . . . yeah, I guess it's a matter of preference. The GUI is for those who prefer to remain ignorant of the most efficient way to get things done. The CLI is for those who like to learn the most efficient way to do something, just in case they'll have to do it (or something like it) again some time. My preference is usually for knowledge, not ignorance. Your mileage may vary.

j-mart
j-mart

I found when I started with Linux, I used GUI at first, but as time and experience is gained for many tasks I now find using shell is much more efficeint. This transition is a gradual proccess, with tasks that are often performed are the first you become familiar with. A simple example use GUI file manager to search for a file, then compare speed and eficiency using "whereis" in a terminal, dead simple, much faster. You don't have to take on CLI all at once, but as your Linux experience grows there are many positives in building up your knowledge of these tools.

dwang00
dwang00

When it comes to repeatable, scheduled tasks that we face as administrators, it is always good to learn and use the CLI.

ron.carlton
ron.carlton

I am not saying that Windows is easy to figure out. On a Windows forum I would complain plenty. As a developer I have spent a lot of time in the Windows Registry and I hate it. But end users shouldn't have to even know about Registry or Command Line.

david.hunt
david.hunt

I generally agree that Windows folks tend to go for the GUI approach. Unfortunately, these days it is too often because they have never been taught how to use a CMD> prompt. I have long been using DOS scripting to do many repetitive tasks, but also employ some Windows binaries of tools such as egrep, awk, sed, sleep, test, touch.... Try detecting some incorrect lines in an application's ".ini" file, and editing the values for every user in a corporate environment with Windows Policy... give me a break :-) Really easy with egrep and awk. Such tools also make editing Windows Registry entries and Keys a breeze. Microsoft realised after Win2K that they had made a grave mistake in eliminating a lot of script commands in favour of GUI tools and set about a deliberate strategy of introducing a comprehensive set in Win2K3. Have a look in the Win2K3 Resource kit. If you apply yourself, you can even script editing of Active Directory. By the way; my introduction to Unix was a VT100....what GUI ;-)

eclypse
eclypse

I agree with apotheon here. To really understand what the GUI is doing, you need to understand how it is done through the command line. I actually read the DOS 6.0 manual and that made it so much easier to understand any other command line. I've always thought that Windows has tried to "dumb down" their administrator requirements. If I can plug a computer in better than someone else, I can be a Windows administrator, too. It always scares me when I hear someone saying they're in IT and they're a sysadmin and you mention a couple of things that a real admin would know and they look at you with that vacant expression. *sigh*

TheGratefulNed
TheGratefulNed

Try this next time you're on a DOS/Windows box and need to find a file: C:\> dir /s filename.ext The /s recurses through subdirectories, so make sure you're at the HD root to search the whole drive or at least as high up as you want to search.

apotheon
apotheon

Yes: Win2k and Win2k3 started (in some cases re)introducing a lot of missing functionality to the MS Windows command line interface. More impressively, the PowerShell has a heck of a lot of functionality. In fact, I'd say it has more functionality than really belongs in a standard command shell. Put all that together, and you've got something usable. No: On the other hand, you're not going to log into any Win2k3 system, bring up a command line interface window, and have the wonders of lsof and grep at your fingertips by default. Even PowerShell isn't there by default. You have to jump through a lot of hoops to get anywhere near the total functionality installed by default on a modern open source Unix-like system such as FreeBSD or Debian GNU/Linux. Furthermore, that kind of power isn't very damned portable, since moving from a system where you have all your favorite tools installed to another system means all that functionality is gone now. Sometimes, you just don't have the authority to install those tools on a system -- or the time to screw with it. Sometimes, you need to be able to write scripts that other people can use without having to install half a dozen extra command line utilities, too. Also . . . you referred to the Linux command line. We should really be talking about the power of the Unix command line, though. Linux is just a reimplementation of that, and is far from the only place to get the same significant functionality.