A Friendly Introduction to the Command-Line

This past week, I wrote a little introduction to the Unix command-line, and I liked it so darned much, I thought I'd reproduce it here. If you don't have any interest in Unix, Linux, or the command-line, this will probably bore the snot out of you.


Having just a little knowledge of the command line can go a long way, and if you learn the right tools to begin with, you'll be better equipped to teach yourself the rest.

Let's start with the basic stuff...

A Unix command-line is a user interface that allows you to control the computer by entering commands, which can be refined by the addition of arguments -- i.e. text entered after the command. Additionally, you can cause one command to feed information to the next (called piping), and thus accomplish complex tasks with only a basic toolset.

In this primer, we'll be covering the use of the BASH shell, which is the default command-line interpreter on most Linux systems.

What's the very first step? Learning not to be afraid. The command-line may look scary, what with all of its weird names and arcane symbols and giant fangs and claws, but it really just wants to be your friend. In fact, the command-line is more afraid of you than you are of it.

Useful Commands

  • man -- displays a manual page for a command/program. Takes a program name as an argument. Example: man man <--shows its own man page.
  • apropos -- displays a list of programs whose descriptions match your search term, sort of like your own personal google. Example: I'm searching for a text editor on my system, so I type apropos editor at the command line and hit enter.
  • ls -- lists contents of current directory (or a directory specified as an argument).
  • cd -- change directory. This is your primary method of "moving around." Takes a directory name as an argument. Example: cd /home/spectre/text-files
  • cat -- Meow. Err, umm... Asks the system to spit out the contents of a file. Not very elegant, but gets the job done. Can also be used to combine 2 or more files, but that'll wait till later. Takes file names as arguments.
  • less -- a text-reader of sorts. You can use less to read text-files, and you can also use it to make it easier to read another command's output. We'll cover this a little later.
  • grep -- search for a specific string of text. Takes two arguments; search item and file. Example: grep jenny phonebook.txt <--should return the line with Jenny's phone number on it.
  • pwd -- tells you which directory you're in right now. Think of pwd as the "You are here" command.
  • cp -- copy a file from one location to another (or from one name to another). Takes two arguments: the source file and the target.
  • mv -- move a file from one location to another (or from one name to anoher). Takes two arguments: the source file and the target.
  • ps -- lists currently running processes. More informative with the x argument --> ps x
  • kill -- tries to shutdown a currently running process. Takes a 4-digit numerical process identifier as an argument, which you can find with ps (see above).
  • rm -- removes a file. Be careful; rm is permanent.
  • mkdir -- make a directory.
  • rmdir -- remove a directory. Only works on empty directories.
  • clear -- clears all the junk from your screen, leaving nothing but a lonely little command prompt. Useful when you're feeling frazzled.
  • su -- Super-user! Type this in and the system will ask you for the root password, then POOF! Now you're root. You are the master of all you survey. Use with care; the system assumes root knows what he's doing.
  • logout -- for when you're done with the terminal. This will not restart the computer, only log you out. Can also be accomplished by pressing ctrl-d

Tricks & Shortcuts

These are some symbols that the command line uses to make your life easier.

  • * -- means "anything and everything." For example, if I wanted to see every file in a directory that started with the letter a, I'd enter this at the command-line: ls a*
  • ? -- means "any single character." In this example, I've got a bunch of files in a directory, but I only want to see a list of numbered photos I know I left in there. ls picture?.jpg
  • . -- I know it's hard to see, but that's a period. He may not seem like much, be he can be pretty darned useful. All by itself, the period represents the current directory. How's that useful? Let's say I want to copy a file from another directory, I might try this: cp /home/spectre/some_useful_file ./
  • .. -- Seems those periods have ganged up. Two periods means the directory above this one. So, if I cd .. from /home/spectre/files I will end up in /home/spectre
  • | -- That's not an I like India or L like Lima. It's a pipe, and can be found on the same key as the backslash. A pipe tells the command-line to take the output of one command, and jam it into the next one. This allows you to do things like use less to read a really long directory list: ls | less
  • ~ -- That's the tilde, which can be found (usually) just left of the 1 key. On the command-line, it's a shortcut to your home directory. For example, if I want to return to my home directory (/home/spectre) I would type cd ~ and hit enter.
  • > -- tells the command-line to write the output to a file. I want a list of all the files in my current directory, so I'll type: ls > list_of_files.txt
  • >> -- Great, now the brackets are ganging up too. This tells the command-line to add stuff to the end of a file. Continuing the example above, I've already got a file with a directory list in it, and I want to add the contents of another directory: ls /some/other/directory >> list_of_files.txt

Hints and Tips

  • Tired of typing so dang much? Try out auto-completion! Once you've typed in the first couple letters of something you want, hit the tab key. The command-line will try to fill in the rest for you, and it's pretty good at guessing. If it doesn't fill it in, you probably haven't entered enough letters to make it unique. Hit tab twice quickly to show a list of items that match your current entry.
  • Want to repeat the last thing you typed? Hit the up-arrow. The shell keeps a record of everything you've done, and you can scroll through it with the up and down arrow keys.
  • Not sure how a command works? You can always try the man page, but if you're in a hurry, type the command name followed by a space and --help then hit enter. Example: cp --help

Unix Directory Structure

To get you started, here's a quick map of some key directories.

  • / -- This is the mother of all directories. Also called root.
  • /bin -- short for binaries. This is where many of your programs/commands live.
  • /sbin -- like /bin, but full of programs you probably won't find interesting.
  • /etc -- this is where system-wide configuration files live.
  • /boot -- where the kernel lives. Don't fool around in here.
  • /root -- a little confusing. This is root user's home directory.
  • /var -- this is a home for stuff that changes frequently, like log files and what-not.
  • /usr -- not what you might think. This is where nearly all of your installed programs live.
  • /home -- this is probably what you thought /usr was. This is where your personal stuff, and the personal stuff of any other users on the system lives.
  • /lib -- libraries. Not real interesting to you as a new user.
  • /opt -- basically like /usr, but not as frequently used.

Parting Words

That should be enough to get you started. Everything else, you can either find on your system or online with a little search in your search engine of choice. Of course, you can also ask me if it tickles your fancy. Good luck, and may the source be with you!

See... wasn't that exciting? Damn straight it was. That's not the end of it, either. In the next few days, I'll have some even more pulse-pounding news, including the premier of the brand new Giant Roblog logo! Stay tuned, true believers. I promise it'll be worth the wait.

Labels: , , , , ,


Post a Comment

<< Home