Linux Tutorial

Opening a terminal window

Linux commands are shell commands that execute in a terminal. You can start up a terminal by clicking on the Ubuntu logo in the upper left hand corner of the screen and typing "terminal" into the search area. There are actually many different shells (with different features) that can be used in Linux, but the default one that opens up in the Terminal program is bash, the Bourne Again Shell.

A shortcut for opening a terminal window is <Ctrl-Alt-T>. Another useful approach is opening a directory and right clicking in a blank area. One of the options should be Open in Terminal, which opens the Terminal program in the directory you've navigated to.

Basic commands

The most common Linux command is ls.

ls

The command ls with no arguments lists the contents of the current directory. If you want to see the details of the files, give it the -al arguments.

ls -al

The command pwd prints the directory path of the current directory, where the shell is currently focused.

pwd

The directory path printed is a list of the directories necessary to reach the current directory from the root directory.

To create a new directory use mkdir. Below we will create a directory called lab1.

mkdir lab1

To change the current directory to another directory, use the cd command. Below we will change to the lab1 directory we just created and then list its contents

cd lab1
ls

If you ran these commands, you would see that there are no files in lab1. If you ran the ls -al command, you would see the files . and .. listed. One dot means the current directory and two dots means the parent directory. To change to the parent directory type the following.

cd ..

Your home directory is the directory where all your files are stored and is where the current directory points when you log in. To change the current directory to your home directory type cd with no arguments.

cd

Sometimes you would like to go back to the previous command. To do that in most shells, hit the <up arrow> on your keyboard. Hit it several times to review a history of your commands. To execute the current command displayed, hit <enter>. You may also hit the <down arrow> to go to the next command.

If you have made an error while typing, you may use the <left arrow>, <right arrow>, or <backspace> to move the cursor to correct it.

You don't need to type all the characters for a file or command. Once you have typed a few letters of a file name, you can hit <tab> to complete the rest of the name. If the prefix matches several files, you may see a list of files that match.

You can also refer to a list of files with the wildcard character *. In a file name, this character represents zero or more arbitrary characters.

ls *.c

This command should list all files ending with .c.

Command Reference

Here is a summary of the commands above as well as a few more.

Command Description
ls Lists the contents of the current directory
ls -al Lists the contents of the current directory in detail, including hidden files
pwd Prints the path of the current directory
mkdir new-directory Create a new directory
cd directory-path Change to a directory
cd Change to home directory
cp old-file new-file Copy a file from old-file to new-file
mv old-file new-file Rename a file from old-file to new-file
rm file Remove a file
echo "message" Outputs the string "message", useful in scripts
<up arrow> Get previous command
<down arrow> Get next command
<tab> Path completion

Editors

The default text editor in Ubuntu is GNOME Text Editor. It's a decent text editor with syntax highlighting for a wide range of languages. You can find it by looking through the Ubuntu applications. If you are at the command line, you can type gted & to start a new instance of GNOME Text Editor. The ampersand (&) starts the application in the background, meaning that the command line won't be frozen until it ends. For most windowed programs, you want to start them in the background. Likewise, for most command-line programs, you don't.

gted &

The xemacs editor is another great editor. It has a lot of complicated hot key combinations that will allow you to edit files in a powerful way, once you master them. To run xemacs, type xemacs from the command line, assuming it's installed.

xemacs &

You can use xemacs using the pull-down menus like other editors with a graphical user interface. If for some reason you are stuck in the prompt at the bottom line type <Ctrl-]>. If you're interested in using xemacs or emacs, download a copy of the GNU Emacs reference card.

To quit emacs or xemacs, type <Ctrl-x Ctrl-c>.

Linux file system

The Linux file system is a hierarchy of directories that start with the root directory (/). You can list the contents of this directory like you would any other.

ls /

Some other important directories include the following.

Directory Description
/dev All the devices of the machine (terminals, printers, drives, etc.)
/etc Administration
/usr/include Header files used for C programs
/usr/bin Commands
~/ Your home directory
~user/ The home directory of the user named user