The Unix Command Line

So, you’ve heard all about Linux this and Unix that and feel like you’re the only one on the face of the earth who hasn’t played with it, seen it, or knows what it’s all about. Fear not fellow computer jockeys! Unix is a lot easier than it looks. Sure, it’s a complex mismatch of distributions, kernels, alpha and beta programs, but it works and it works well. This little How-To is designed to give you that first step in exploring the big bad world of Unix. So, without further ado…

The Unix GUI (called X, X11, or X Windows) functions just like any Graphic User Interface should. You’ve got a mouse, some menu bars and you have the ability to drag and drop things to your hearts content. Sure it’s pretty and sure it’s fast, but the real power of the Unix OS lays not in the easy-to-use GUI, but its command line. Even if you’re running X, chances are you’ll have a load of terminal windows open and cursors blinking at your beck and call. The command line, while esoteric, complicated and archaic, is fast. Damn fast. And if you know what you’re doing, you can create all sorts of elegant commands that do everything you need in only a fraction of the time GUI takes, without even having to touch the mouse!

 This session will cover very rudimentary commands, such as:

  • telnet
  • pwd
  • ls
  • cd
  • absolute & relative pathnames
  • more
  • head
  • tail
  • less

 

Getting onto the system

Depending on how you’ve got access to a Unix system, you’ll probably need to connect to the machine differently. A system administrator might have set you up with an account to use, or you’ve got Unix shell access through your ISP. Even if you’ve gone through installing Unix without a clue how to use it, the first thing you’ll have to do is connect to the machine and log in. Easy enough. You can connect to your Unix system through any other Unix system by running the telnet command. If, for example you wanted to log into the machine rocketdog, you would type:

telnet rocketdog

You can also enter the IP address instead of the hostname if you choose to. You can even connect to a Unix system through Windows telnet (which sucks big time, but usable here.) Simply choose the run option from the start menu and type “telnet” and the domain name information. However you connect to your Unix system, you’ll be greeted with a prompt for a login and password. Be careful with that shift key though, as Unix is case-sensitive. You’ll then be prompted for a TERM type, like this:

TERM=(ansi):

It’s probably safest to use something like ansi or vt100 for the time being. If something comes up, like (unknown term type), type in ansi and hit return. So, if you’ve logged in as a user account you’ll probably see something like this:

[dan@rocketdog]$_
Now is when the fun begins…

You’re at a blinking cursor without any clue on where to go. First, we’ll figure out where you are with the pwd command. Go ahead and type:

pwd

Something like /home/dan Or /user/dan should pop up.

pwd stands for “Print Working Directory” and will show you where you currently are in the file system. Right now you are in your home directory. Every time you log in you’ll start in your home directory. Directories and subdirectories are separated by a forward slash: “/” and NOT a backslash like DOS uses. Using pwd will let you know where you are ANYWHERE in the file system.

Ok, so X-marks the spot and now you know where you are. So, what’s in your directory? Try running this:

ls

A whole bunch of files come up. ls stands for “list” and works exactly like DIR does in DOS. The output will list all of the files in your current directory. Let’s see what we’ve got:

Desktop
Office51
newfile
relnotes.ps
staroffice
Mail
UNIX.txt
navigator-v408.tar
nsmail
relnotes.txt
Getting all the directory details…

 

Bunch of files. Or are they? The ls command, like DIR in DOS, has options you can use when executing. Unix command line options are knows as flags and are represented by a single dash before the option “-“. For example, try this:

ls -l

The -l flag means “long” so you’re going to see the lengthy display of what’s in your directory:

drwxr-xr-x 5 dan dan 1024 Jul 23 13:21 Desktop drwx—— 2 dan dan 1024 Sep 17 14:23 Mail drwxr-xr-x 26 dan root 1024 Sep 16 15:27 Office51 -rw-r–r– 1 dan root 4714 Sep 29 13:12 UNIX.txt -rw-rw-r– 1 dan dan 10106880 Sep 16 11:30 navigator-v408.tar -rw-rw-r– 1 dan dan 7 Jul 23 14:18 newfile drwx—— 2 dan dan 1024 Jul 23 14:45 nsmail -rw-rw-r– 1 dan dan 115407 Sep 3 12:27 relnotes.ps -rw-rw-r– 1 dan dan 54387 Sep 3 12:27 relnotes.txt drwxrwxr-x 3 root root 1024 Sep 16 13:46 staroffice

 Whew! What a bunch of garble. Let’s start from the left side and move to the right column by column. The first column shows whether the item is a file or directory. If it’s listed as a “d”, it’s a directory, otherwise, the “-” indicates it’s a file. The various rwxr-xr-x entries next to it show the permissions attached to the file (much more on this later!) the next column shows the link count (if its a file) or the number of subdirectories within (if it’s a directory). The next column shows the owner of the file (dan) and the group (dan or root) associated with the file. The next column shows how large the file is in block sizes (in increments of 512 bytes). The date field shows the last time the file was access/modified, and the last column shows the name of the file and or directory. There are other options for the ls command, too. The ls -a command displays hidden files while the ls -f command displays the files in columns (taking up less space.) You can even execute multiple options using only one flag. To display both the hidden files in a column format, type

ls -af 

. 		.mc 			navigator-v408.tar
.. 		.ee 			Office51
.Xdefaults 	.ICEauthority 		relnotes.txt
.bash_logout 	.gnome-help-browser 	UNIX.txt
.bash_profile 	.gnome-desktop 		.gimp
.bashrc 		.gtkrc 			relnotes.ps
.kde 		.netscape 		kthememgr
.kderc 		nsmail 			Mail 
Desktop 		.newsrc-news 		.sversionrc
.gnome 		newfile 			.user.rdb
.Xauthority 	.bash_history 		staroffice
UNIX.sdw		.xsession-errors
gnome_private 	.gnp
.enlightenment 	.xauth 

 Notice how all of the files preceded with a “.” are hidden files? These files usually pertain to configuring your environment when you log in. There are other items such as .history (or in the above case, .bash history) that keeps a record of the commands you’ve used. There’s a crapload of files in there. Let’s say you were looking for a specific file called “.bash_profile”. You can use ls to show you whether or not a particular file is in the directory.

ls -l .bash_profile

-rw-r--r--   1 dan     dan        282 Sep 16 14:46 .bash_profile  

 Like DOS, you can use the asterisk as a wildcard character to complete the filename if you are unsure what the file is called or if you don’t feel like typing it all out:

ls .bash*

or

ls *profile
Moving between directories…

 

Ok, so you should have a pretty good idea what’s in and around your directory at this point. How do you move between directories? Easy: the cd command (change directory.) Let’s say I wanted to move into the Mail directory to see what kinds of mail folders I have:

cd Mail

Remember that Unix is case-sensitive, so watch your typing. Now, do an ls to see what’s in the directory:

inbox
outbox
sent-mail
trash

Simple enough. Nothing interesting here, so let’s go back up a directory. But first, do an ls -a

ls -a

. 
..
.inbox.index
.outbox.index
.sent-mail.index
.trash.index
inbox
outbox
sent-mail
trash

Notice the two top entries there? “.” and “..” are directories. It’s a shorthand used in moving about and executing files. The “.” means “this directory.” The “..” means the “parent directory”, or “one directory above”. You can save a hell of a lot of time using these shorthand notations. For example, if you wanted to move up a level in the directory, you would simply type:

cd ..

Much easier than typing

cd /home/dan

 The “.” and “..” comes in handy especially when moving files about (more on that later!) So, you’re now back at your home directory again. There’s another trick to cd. Just typing cd will always take you back to your home directory no matter where you are in the system.

Let’s talk about absolute and relative path names for a second. This is an absolute path name:

/home/dan/Mail/inbox

This is a relative path name:

Mail/inbox

What’s the difference? Absolute path names always start at the root directory “/” and move down the hierarchy to the specified file or directory. Relative pathnames depend on where you are in the file system. Let’s say you wanted to get to the /home/dan/Mail/inbox directory. One way would be to type:

cd /home/dan/Mail/inbox

No matter where you are in the system, you will be taken to the inbox directory. Because you specified an absolute pathname – the system will take you where you want to go. Let’s say you were already in /home/dan. You could use the relative pathname to get to your inbox simply by typing:

cd Mail/inbox

Notice the absent “/” on the relative pathname. The command line interprets this as a relative pathname, and will look for the Mail/inbox directory in your current working directory (/home/dan). Of course, this wouldn’t work if you were in the /home/dans/staroffice directory, because there is no Mail/inbox directory in /home/dans/staroffice. Sometimes it’s easy to get lost, but if you can master these tricks, you’re able to save yourself a hell of a lot of time and effort. Last item is a quickie, but one you’ll use often.

Taking the absolute and relative paths…
Getting a grasp on your files…

 

So you’ve got tons of files. How do you look at them? You can use several commands: more, less, head, tail, cat… each one displays a file differently. Let’s check out more to see how it reads a file:

more newfile

 Notice how you scroll through the file’s contents, and the cursor keeps track of where you are in the file by using percentages. Using the Enter key will advance you line by line, while the space bar will advance you an entire screen. You can drop out of the file at any time (depending on what flavor of Unix you’re running) by hitting <ctrl-z> <delete> or <ctrl-d>.

So, take a look and cruise around your Unix system. Check out files to see what they’re all about. The two commands head and tail look at either the first few lines or last few lines of a file, respectively. To use the head command to see the first ten lines of a file, type:

head -10 newfile

Or, to see the last twenty lines of a file, type:

tail -10 newfile

These are mostly used to see what’s in the file. There are more commands for looking at files too, cat and lesscat spits the entire file out to the display, and unlike the more command, won’t wait for user input to finish. Try it with a larger file and you’ll get the idea. The less command is a little more helpful than the more command because it lets you traverse the file using the arrow keys up and down. You don’t have to re-read the file through more if there was something you missed early in the file. And the space bar and enter keys work the same way with the more command.

 You should have a pretty good idea of how to get around in a Unix system now. Remember that you can look at the manual page for any command by typing man and then the command:

man less

So now you know a little bit about what Unix is, and how to use it. Impress your friends and make new enemies by showing them your command line prowess. Next time we’ll talk about the powerful, but esoteric world of file permissions.