Introduction to Linux

Summary

A brief introduction to the UNIX operating system, including explanations of some basic commands.

Body

  1. What is UNIX?

  2. Logging In

  3. Logging Out

  4. Editing Files

  5. Basic UNIX Commands

  6. Options

  7. More UNIX Commands

  8. Redirection

  9. Other Helpful Information

  10. Compilers

  11. Where to Go from Here

 

What is UNIX?

UNIX is a ubiquitous operating system used around the world. It can be run on many different kinds of computers. Users generally access UNIX via an interface called a shell, rather than a graphical user interface like that found in most modern computers. The shell allows the user to input commands to perform a vast range of potential actions, such as manipulating files or user account management. Since UNIX machines usually lack a graphical user interface, it has a considerably steeper learning curve as opposed to, say, Windows or macOS. Still, it is more than worthwhile to learn UNIX due to its sheer power and flexibility. 

Over the years there have been numerous operating systems that have mimicked UNIX's design and philosophy. By far the most common of these derivatives has been Linux, which is an open source version of UNIX and is used here at Ohio State. Such operating systems are technically referred to as "UNIX-like" operating systems, but for most purposes the two can be used interchangeably. 

Logging In

The best way to log in to the departmental UNIX machines is to SSH into it from another computer. The process for this differs based on the operating system you're using. There is a full tutorial on how to SSH from Windows and macOS available here. Once you see the prompt appear you will be logged into the machine. (The prompt may look like unit-nc123456$ or similar, depending on if the machine is programmed to use a 'friendly' name or not.)

Logging Out

To log out, enter exit or logout at the shell prompt, or press CTRL+D

Editing Files

There are a number of text editors that are typically built into the UNIX shell. Three of the most common are emacs, vi, and nano. To start any of these edits, just type their name in the command line. Alternately, to edit an existing file or open a new file with a specific name, you can type [editor] [filename]. For example, emacs foo would either open a file named "foo" in emacs if the file already exists, or would open an empty file and save it as foo when told to.

Deciding which editor to use is a matter of personal taste. It should be noted that while nano is probably easiest to learn, both emacs and vi offer far more in terms of flexibility and usefulness. There are many, many resources available online for these editors that will help learn to use them.

Basic UNIX Commands

There are hundreds of commands in UNIX, ranging from ones that are used almost constantly to ones that are seldom used at all. One of the most useful when first learning UNIX is the man command. Whenever you encounter a command you're unfamiliar with, or simply want more information on a command, typing man [command] will bring up documentation about the command, including a description of it and how to correctly use it. Additionally, if you would like to find commands related to keywords, you can use man -k [keyword] and man will display a list of related commands.

Here are some other basic commands: 

ls [dirname] Lists the contents of the specified directory if given, otherwise lists the contents of the current directory.

cp original_filename copy_filename Copies the one file to another with the given copy_filename

mv original_filename new_filename Moves the file into the specified location if new_filenameis a directory, otherwise renames the file new_filename

rm filename Removes the specified filename

more filename Displays the contents of a file one page at a time.

mkdirdirname Make a new directory within the current directory.

rmdir dirnameRemoves a directory (but only if it is empty).

cd dirname Changes the current directory to dirname.

pwd Prints the current directory

lp Prints a file. The printer's names are lw341, lw335, lw404, lw440, and lw212, depending on the room they are in. For example, to print to the 341 printer, you would use: lp -dlw341 filename

Options

While some commands are useful enough in their own right, most have additional options that you can invoke which make them much more flexible. Take the rm command, for example. As we mentioned above, rm [filename] deletes the file specified, but it will not work if [filename] is a directory. It can still be used to delete directories, though, by invoking its recursive option. 

Options are invoked by typing -[option] after the command. Since the recursive option's abbreviation is r or R, typing rm -r [dirname] will delete the entire directory dirname.

f

UNIX options can also be used in combination with one another. For example, say we wanted to delete a whole directory. By default, rm prompts every time it deletes a write-protected file. While this is fine for smaller directories, we don't want to have to respond to a prompt for every file in a directory of, say, 1000 write-protected files. Fortunately rm comes with an option to avoid this. The force option (abbreviated "f") will prevent the rm command from prompting for anything. To use both the recursive and force option, you would type rm -rf [dirname].

More UNIX Commands

Once you've mastered the previous commands and feel more comfortable in a unix environment, you may want to start using somewhat more advanced commands. Here are a few to get you started:

grep pattern_string filename Search for the specified pattern_string in the file filename. Any lines that contain the specified string will be printed to the shell.

kill process_id Terminates the specified process.

ps Displays a list of the processes you are running. ps -ef lists all the processes on the system. This can be combined with grep to search for specified processes (see the Pipes section below). 

top Lists all processes in a different format.

rwho Lists all the users on the system.

passwd Changes your password.

Redirection

By default, most basic commands print their output to the shell. For example, ls just displays all of its output on the command line; it doesn't only display some information, and it doesn't create any new files. However, if desired, we can use redirection achieve both of these goals. Although you can learn about redirection if you wish (there are various resources online), we will cover two basic types in this article.

Bracket Stream Redirection

Suppose we want the output of a particular command to be stored in a file rather than output to the terminal. This is easily achieved via the bracket operators, i.e. >> and >. (There are also corresponding << and < operators for giving input to a command.) The >> and > operators redirect the output of a program to a destination other than the terminal. For example, ls > foo.txt would replace foo.txt with the contents of the current directory, i.e. what it would've printed to the terminal. On the other hand, we could append this information by using ls >> foo.txt. The key difference between >> and > is that > overwrites the contents of the file, if any, and >> appends to it.

Pipes

Another type of redirection operator is the pipe operator, which is | (i.e. the vertical line above "enter" on QWERTY keyboards; not lower-case L or upper-case i). The pipe operator essentially sends output from one program to another for further processing. One way this is commonly used is to filter output. For example, recall that ps -ef returns all of the processes running on a system and grep searches a file for a given string. These can be combined with the pipe operator to filter out some processes we aren't concerned about. Putting it together, we could find all the processes which are run by root with ps -ef | grep root.

Other Helpful Information

You can hit the tab key to complete a filename while at the shell prompt, as long as you've typed enough of the filename to ensure that there is only one matching filename.

Control-C will generally kill something that is currently running. Control-Z, on the other hand, will only suspend a process; to kill it you need to type fg, and then Control-C. (There are other ways as well, see bash or ps and kill).

Control-s and control-q have traditionally been pause and unpause characters, so if you are stuck and think you might have hit control-s, try hitting control-q. Emacs uses control-s in the save command, so if this keeps freezing your screen, you will have to set your terminal program not to interpret them. In telnet on the Macs, this can be done by going under Session to Setup Keys and making them all blank.

A shortcut for your current directory is '.' (i.e., a single period). Your current parent directory is '..', i.e. two periods. So if you would like to refer to a file, say foobarin your parent directory, it could be accessed by ../foobar. This is referred to as a relative path. For more information, there is a complete explanation of relative and absolute paths here.

There are multitudes resources on the web for learning more about UNIX/Linux commands.

Compilers

Compilers are also available via the command line. The Fortran77 compiler is used via the f77 command; the C compiler is gcc; the C++ compiler is g++; the gfortran command compiles Fortran90/95. All of these compilers have similar syntax and options, but you can always read the man page for more information. The general syntax to compile a program, using Fortran as an example, would be f77 prog.f.  Assuming there were no compile-time errors, this would create an executable called a.out, which you can run by typing ./a.out. (Don't forget the preceding ./ as it is necessary to tell the shell where the executable is.) You can also call the executable something else by using the -o option. For instance, f77 -o foo prog.fwould name the executable "foo" instead of "a.out".

Where to Go from Here

Like many things, Linux gets easier the more you use it. Log in and try some things out, and don't be afraid to ask the Internet for help or consult our department documentation. You'll be a pro in no time!

Details

Details

Article ID: 34297
Created
Wed 7/26/17 9:52 AM
Modified
Tue 11/14/23 5:14 PM

Related Articles

Related Articles (1)

A guide for opening a remote desktop connection with FastX2.