View Kai Wetlesen's LinkedIn profileView Kai Wetlesen's profile
Table Of Contents

Not everyone is born with a command of the Unix operating environment. As one would expect with anything, using Unix or Linux takes a little getting used to. These tutorials have been written to help make your transition to the Unix world smoother.

Getting Started with the Unix Shell

On Unix and it's derivatives, the vast majority of your work will be done with a command line user interface. While it may seem intimidating to a first-time user, the Unix command shell is a very powerful tool. Just like any power tool, it needs to be learned. In contrast to a graphical user interface, the system is controlled by a sequence of commands typed by the user.

When you first log into a Unix system, you'll be presented with a command prompt. The command prompt may differ depending on the system and configuration. Some common prompts you might be presented with are:

		you@computer /present/directory $ █
		[OR]
		[you@computer /present/directory]$ █
		[OR]
		(computer:/present/directory) you% █
		[OR]
		% █
		[OR]
		$ █
		

While these all might look very different and , they're very similar. With the exception of the last two, each prompt tells you the name of the computer which you have logged into, your username (represented by you), as well as the directory you are currently "in". More on that later. When presented with the other two prompts, we can view our present working directory (which directory we're in) by typing `pwd`, who we are by typing `whoami`, and what machine we're logged into by typing hostname. Anyhow, the presence of any of these command prompts is an indicator that the system is ready for you to tell it what to do.

Note on directories: A directory is conceptually equivalent to what Windows users would call a "folder". It is a special file which contains files and other directories. It is the basis for file organization and location in Unix.

The small box is the cursor. That shows you where text will appear as you type it in. To run a program, type it's command in then press [Enter] or [Return] on your keyboard. This will cause the program to run and display it's interface or results. For example, typing `whoami[Enter]pwd[Enter]` will print your username and present working directory like so:

		kaiw@valhalla /present/directory $ whoami
		kaiw
		kaiw@valhalla /present/directory $ pwd
		/some/directory
		kaiw@valhalla /present/directory $ █
		

Now that we are familiar with how the command line works, let's look at how to send data to programs. Aside from separately asking the you for something, programs can also be given parameters on the command line by something called "switches" and "arguments". A switch is simply an option that modifies how the program runs, and an argument is data that the program uses. They'll look like this:

kaiw@voyager /present/directory $ command -s argument  <-- Command with a switch 's',
                                                           and an argument.
[OR]

kaiw@voyager /present/directory $ command --switch argument  <-- Command with a long switch 'switch',
                                                                 and an argument
		

In each of this example, these commands will start a program called "command" with an option called 's' or "switch", and pass in "argument". Perhaps argument is a filename to work with, or has some other significance to the program. Those operational details will be in the manual file for each program.

Note, argument order can matter! It doesn't always matter, but some applications expect a certain order of arguments. Say you were using a program called process, which took an input file, did something to it, then printed it to an output file. It might be called like this: 'process input output'. If you were to transpose the names of your input and output files, you would probably overwrite your input file!

Now that you have an idea about how the command prompt works and how it's used, try it for yourself! Before you do though, let's just look at one very important command. That command is "man". Man (short for manual) is the compendium of all documentation for each command available on each system. Simply type "man commandname" to display commandname's documentation assuming commandname exists and it's documentation exists. Start by typing in "man man" and looking at what happens! Once you're done with that, look at the table of frequently used commands below, and start exploring!

Frequently Used Unix Commands

The following list contains frequently used commands used with a Unix based system. It is by no means an exhaustive list. You should look up the manual page for each one of these commands to gain a full understanding of what the command does!

Commanddescription
manDisplays documentation associated with a command or library.
aproposFinds commands whose description matches a given keyword.
pwdDisplays the present working directory, i.e. what directory you are in.
cdChanges directory to whatever directory chosen on the command line.
lsLists all files in the current directory, or a directory given by an argument.
touchCreates (touches) a blank file with the filename given by an argument, or timestamps an existing file.
rmRemoves a file given by an argument.
mkdirMakes an empty directory. Names the directory based on the command line argument.
rmdirRemoves an empty directory specified by an argument. If the directory isn't empty the command will fail!
catShort for concatenate. Prints contents of a text file to the screen.
fgBrings a suspended or background program to the foreground.
bgSends a suspended or active program to the background.
calDisplays a calendar on the command line.
bcBasic calculation and logic evaluator.
vi (or vim)Visual Text Editor: a very nice and powerful text editing program (my personal favorite).
emacsEditing Macros: another very powerful text editor.
picoPico: a very simple text editor (great for beginners).
nanoNano: a very simple text editor that's very similar to pico (only on some systems).
tarTape Archiver: formerly used for archiving to tape, now used for manipulating .tar, .tgz & .tar.gz file archives.
gccGnu Compiler (for) C: compiles C code into an executable program.
g++Gnu Compiler for C++: compiles C++ code into an executable program.
javacJava Compiler: compiles Java code into a Java object file.
javaJava Runtime: reads and runs a Java object file.
gdbGnu DeBugger: debugs a program compiled by gcc or g++
gdbtuiGnu DeBugger: same as gdb, but with a nice runtime display.
valgrindValgrind: Verifies your program by checking for logical failures and memory leaks.
Convert Unix Text Files to Windows Format

One thing to consider when moving back and forth between Windows and Unix is the difference in line terminators. This can mean the difference between a program running perfectly or crashing and dying. While Unix, Solaris, Linux, and FreeBSD use only a line feed character (\n for newline) at the end of a line and can interpret a carriage return, line feed (\r\n), Windows requires the double character terminator. In order to compensate for this, several programmers have written small programettes whose sole purpose is to convert \n into \r\n and vice versa.

On Unix based systems this program is called unix2dos. The usage format is this:

		you@computer ~ $ unix2dos myfile convertedfile
		

Of course the converse of this command is dos2unix and converts Windows formatted files into Unix end line format.

	
		you@computer ~ $ dos2unix myfile convertedfile
		

Naturally you will substitute 'myfile' for the name of whatever file you want to copy. You need to run this command on any text or source file prior to copying it to a Windows machine! After you have done this, you can use WinSCP to copy from the Unix system to the Windows system. For more on how to use WinSCP see the ATC Lab Coordinator.

Getting Started with Perl on Unix

You can run Perl on just about any modern Unix-based system. This includes Unixware, Solaris, Linux (all distributions), and (yes) Mac OS X. Essentially any *nix system with Perl installed is capable of running your Perl code! Before you can run the code from the command line, you'll need to set some options.

To begin you will need to create a source file. The filename should end with .pl so you can identify it as a Perl script. You can use Vim, Emacs, Nano, or your favourite Unix text editor.

		you@computer ~ $ vim script.pl
		  [OR]
		you@computer ~ $ emacs script.pl
		  [OR]
		you@computer ~ $ nano script.pl
		

When your editor comes up, the absolute FIRST line you should type in is the 'shebang' statement. It usually looks like this:

		#!/usr/bin/perl -w
		

This will direct your computer to run your perl script using the perl interpreter located in /usr/bin and pass it the -w flag to enable warnings (this could be very useful!). Once you have the file written, save and quit your editor. After this, you'll need to change the permissions on the file with chmod. The file must have execute permissions for anyone who needs to run the program.

		you@computer ~ $ chmod a+x script.pl
		

After that, you're ready to run your script (assuming it works)! Simply type in the filename at the command line (you might have to prefix it with ./ for it to work) and it'll run.

		you@computer ~ $ script.pl 
		  [OR]
		you@computer ~ $ ./script.pl
		

That's it! You're now all set to work with Perl scripts on a Unix system.

This website conforms to the XHTML 1.0 Strict standard published by W3. Read more...