More about the Linux command line |
Last month we did a whirlwind tour of the command line, quickly introducing some of the most basic commands. We learned the cp command for copying files, cd for changing directories, mv for moving files, mkdir for making directories and rmdir for removing directories. We also learnt the ls command for listing the files in the current directory. Unfortunately, space prevents us from covering all the Linux commands in detail, so instead, we're going to use this space to talk about how and why the Linux command line is structured the way it is. We'll also look at where to find documentation on basic Linux commands. The Linux command line, like its Unix counter-part, is structured much like a spoken language. There are actions you can perform (such as listing files), modifiers ù called flags ù that alter the behaviour of those actions, and various constructs for stringing together individual commands to perform more sophisticated actions. The basic philosophy behind the Linux command line is that there are several tools that each perform a single job. For example, we've already seen how the ls command lists the files in a directory. That's what it does, and it does it well. By default, the ls command will just list the files in the current directory, but it can also list them in other ways. For example, the "ls ût" command will list the files by time (or most recently modified). Another example is the "-r" flag, which will reverse the output of the ls command. You can also combine flags, so if you want to list the most recently modified files in reverse order, you can use the "ls -t ûr" command, or, more conveniently, "ls ûtr". There are plenty more flags to the ls command ù too many, in fact, to list here. Just be aware that each command has several modes of operation, each modifying output slightly. The next interesting thing about the command line is that you can combine commands to perform more powerful operations. For example, you can take the output from one command, and pipe the results into another command. While there are a myriad of possibilities for this, I can site a command I used just 10 minutes ago as a useful example ù I needed to count the lines of code I had written over the last week. There is a command called "cat", which is short for "concatenate". In this case, cat joins together the text from two files. For example, if you type cat file1.txt, the contents of file1.txt will be listed on the screen. Likewise, if you type cat file1.txt file2.txt, the contents of file1.txt will be displayed, followed directly by the contents of file2.txt. There's also a command for counting words (and lines and characters) called "wc" (word count). So, to count the lines of code that I had written, I issued a command something like: "concatenate all of the files that end in .java and then pipe the output to the wordcount program, displaying only the number of lines". The actual command is: ls *.java | wc -l. Sure, it may look a bit cryptic, but I think you will agree that it's easier than opening the individual files in a word processor, counting the lines, writing down the totals and then adding them up. Hopefully by now you are beginning to get a glimpse of some of the power of the command line. There is little that can't be accomplished, often much more quickly, than with graphical tools. But, like the rest of Linux, you will only get out of the command line what you put in. In this case, you will need to put in the effort to learn the commands. Fortunately, there are several sources of information that ship with Linux. If you installed Red Hat off the PC World cover CD (March 99), you will most likely have installed a great deal of useful documentation as well. The first place to start looking is the Getting started guide from the Linux Documentation Project (LDP). You can usually find the LDP documents in the /usr/doc/LDP directory (if they are installed on your system), or, in Red Hat, by clicking on the link from the default startup page in Netscape. If you don't find the documentation on your system, you can always find the latest information at metalab.unc.edu/mdw/LDP/. Look specifically for the Linux Installation and Getting Started Guide; chapter 3 deals with basic commands. You can also find help for each command by typing the command, followed by --help. For example, to find more information on the ls command, type ls --help. Your early days at the command line are likely to be a bit frustrating, but stick with it, and next thing you know, you'll be whipping out commands like a pro. - Travis Simon |
Category:linux Issue: August 1999 |
These Web pages are produced by Australian PC World © 1999 IDG Communications