Organically Grown Software ™

 About Us   Grown Software   Custom Hardware   Support 

Administration Concepts Unix Primer Maintenance Tasks Trouble Shooting

Unix Primer

Getting comfortable with the command line

Why use a command line?

Command line interfaces have been around since the stone age of computers. Their persistence in the face of ever improving graphical interfaces is a testament to their usefulness. Some tasks are simply more efficient to do with a command. There are standard commands to accomplish just about anything. Commands can be easily used in combinations to achive specific results. Sequences of commands can be recorded and run as programs. Commands provide a natural scripting language for automating our day to day tasks. Commands are closely related to Unix philosophy. Consider a brief quote from an interview with Doug McIlroy, head of the department from which Unix started.

"Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface."

-- Doug McIlroy as quoted in A Quarter Century of Unix - Peter H. Salus

This quote succinctly describes commands. In Unix, we expect many small tools (commands) which can work together to tackle complex tasks. Large monolithic applications may be available for Unix, but they tend to offer high level functionality which is often based on command line tools. The command line toolset that Unix offers provides a robust and indispensable interface to Unix systems.

Why are there so many names for text environments?

A command environment is called a shell, terminal, console, and xterm, among other things. The terminal and console were historically text based screens where input could be typed and results could be seen. One of the early programs to interpret commands typed at a console or terminal was sh, and using it (as well as other command interpreters like csh, tcsh, ash, bash, and korn) has become known as using a shell. Xterm is a program which displays a shell in the window of a graphical environment (The graphical environment most common for unix is called X). Today, most of these terms are used interchangably.

I see a graphical desktop. How do I open a command shell?

This depends on your graphical environment. For KDE users, click on the kde button in the lower left corner of your screen and look under the "System" submenu for a program entry called "Terminal Program (Konsole)". Click on it. For fluxbox, blackbox, or fvwm users, just right click on your desktop anywhere and select "xterm" from the menu that pops up. Many servers will not have a graphical interface running, so you log in and are immediately given a shell.

Understanding the Unix file system

Files and directories

Pictures, documents, programs, music, and other "stuff" on your computer are called files. Directories (called folders on some operating systems) are special files that allow us to organize other files. Directories form a tree with other directories and files in them. The top of that tree is called the root directory. All files are located somewhere down a chain of directories from the root directory. A file path is the chain of names which describe where a file is. The file path for the root directory is /.

The Unix file system hierarchy

Unix has many conventions for where files of certain types are placed in the file system. A few useful ones to note might be:

DirectoriesFiles Found Here
/ - usually only other directories are found in the root directory
/home/ - user directories are found here
/root/ - special home directory for the super user
/var/log/ - most log files are found here (logs record errors, warnings, and information messages for programs)
/bin/ and /usr/bin/ - these two directories contain most user programs and commands (any directory named bin usually holds programs) "bin" stands for binary executable files (programs)
/sbin/ and /usr/sbin/ - these two directories hold system programs and administrative tools
/lib/ and /usr/lib/ and /usr/local/lib/ - "lib" directories have shared object code or library code (other operating systems use DLLs or dynamically linked libraries)
/dev/ - the device file system allows programs to find devices (a printer is often /dev/lp0 or /dev/usb/lp0)
/etc/ - look in /etc/ to find configuration files for the whole computer
/tmp/ - tmp is used as a temporary storage location for running programs to use
/proc/ - proc shows us many of the kernel parameters and internal settings
/usr/ - holds sharable, read only data
/var/ - hold "variable" information: things that changes with time
/opt/ - for add on application packages

More in depth information on the file system hierarchy can be found at the Filesystem Hierarchy Standard.

File and directory commands

There are many common file and directory commands. Most commands take arguments which can modify their function. Arguments consist of options, (usually prefixed by a - as in -al) file names, urls, or other information passed to the program. Commands and arguments are separated by spaces. For more information on any particular command, use the man command followed by the command you are interested in (man stands for manual). For example typing:
$ man ls
will show you the manual page for the ls command. The output of a man command will be broken into pages. The space bar will display the next page and the "b" key will take you back a page. Type "q" to quit.

All commands are executed in relation to the command environment. The current working directory is part of the environment. When you open a shell, your current working directory is your home directory, /home/<user name>/. Lets examine some of the common file and directory commands.

CommandExampleExplanation
pwd pwd - (Print Working Directory) - displays the current working directory
ls ls /home/chris/pictures - (List) - lists the contents of a directory.
cd cd /home/chris/pictures - (Change Directory) - changes the current working directory. The directory to change to should be specified after the command.
more more /var/log/messages - displays the contents of a text file page by page (use space bar for next page, "b" key for back, "q" for quit).
mkdir mkdir /home/chris/reports - (Make Directory) - creates a new directory. Specify the name of the new directory following the command.
mv mv salesStatistics.txt reports/ - (Move) - moves a file to a new location or name. The original file is removed.
cp cp backup.bz arch/2004-12-3.bz - (Copy) - copies a file to a new location or name. The original file remains.
rm rm oldBackup.tar.gz - (Remove) - deletes a file. Directories can be deleted with the -R option.
touch touch salesFigures.txt - The touch command changes the last modification date of a file or directory to the current date and time.

Now that you are familiar with a few of the more common commands, you may want to try things out or look at a more in depth tutorial. Here are some additional resources around the internet.

About Us | Grown Software | Custom Hardware | Support
Valid XHTML 1.0! Powered by Gentoo Linux Powered by the Apache Web Server