The Terminal

From TheLinuxVault

Jump to: navigation, search


How-To
The Terminal


This how-to is about: Terminal

What you need to know to use this how-to: Linux

The terminal, also known as the console, is a command line interface, much like the old DOS prompt on Microsoft systems. While the DOS prompt is not very useful for Microsoft Windows users anymore, the terminal on Linux systems is a useful and often necessary tool for effective computer usage and administration. While the task of learning to use the terminal may seem daunting to inexperienced users, it is actually quite simple and easy to use.

Contents

[edit] Starting a Terminal

The first thing one must learn in order to use a terminal, is how to start a terminal. For the inexperienced user who is mostly familiar with graphical interfaces, it is recommended that they use a program to run a terminal in a window. For GNOME users, the default application would be gnome-terminal, and for KDE users, it would be konsole. Where these programs can be found in the menus can vary from distribution to distribution, so you might have to consult your distribution's documentation. Otherwise, you can hit Alt-F2 and type either gnome-terminal or konsole into the dialog box that comes up. Other GUI terminal applications are xterm and aterm.

Another way to get to the terminal is by using one of your system's tty's. A tty is a login server running on your Linux system, that is generally hidden from view of normal users. Most systems have the X server (graphical user interface server) running on tty7, and have command terminals running on tty1 through tty6. To switch to a different tty, use the key command Ctrl+Alt+F<tty#>. For example, to get to tty1 you would hit Ctrl+Alt+F1, and to get to tty4 you would hit Ctrl+Alt+F4. Remember that in order to get back to a graphical environment, hit Ctrl+Alt+F7. When you switch to a text-based tty, you will be prompted for your username and password, and after a successful login, a terminal will start up. The only advantage of using a tty terminal compared to a terminal in a window, is that you don't need to have an X server running to use the tty terminal. Other than that, they are exactly identical.

Regardless of which application you are using to display the terminal, you should get the same command prompt. This prompt will usually be in the format of 'user@hostname:~$' where user is your username and hostname is the name you gave your computer during installation. The ~ indicates that the current directory is the user's home directory, usually located at /home/user. Keep in mind that your prompt may look completely different depending on your distribution's configurations. The '$' at the end of the prompt means that you are currently logged in as a normal, unprivileged user. If you are logged in as the root user, your prompt will have a '#' at the end instead of the '$'.

[edit] Terminal Basics

On a modern Linux system, several terminal programs, also called shells, can be installed at once, although only one will be selected as a user's default. By terminal program, or shell, I refer to the program that displays the command prompt on the screen, and then processes the commands the user types. The default shell for most Linux systems is the program Bash. Bash stands for Bourne Again Shell, as it is based on the old Bourne Shell for UNIX systems. Other shells include ksh (korn shell) and csh (C shell). Each shell has it's own advantages and disadvantages, but most of these concerns are for advanced users and scripting, so they won't be covered here.

As stated above, the main purpose of a shell is to receive text commands from the user, and process them on the system. Mostly this means running other programs via a command line interface. For example, typing the command 'ls' will cause the shell to run the program 'ls', which lists all the files and directories in the current directory. Other commands might cause the shell to do something to itself, and not run other programs. For example, if the user types 'cd' into a shell, it tells the shell to set the current directory as the user's home directory. Now if the user runs the program 'ls' again, it will display a listing of the user's home directory.

[edit] Terminal Navigation

The first three commands a user must learn are 'cd', 'pwd', and 'ls'. These commands are useful in navigating the directory structure of the system. For example, if you want to change directories so that your current directory is /usr/bin, you type:

user@hostname:~$ cd /usr/bin

This will change directories. Depending on your configurations, your prompt may change to reflect the new directory, and will look like:

user@hostname:/usr/bin$

If you want to move up in the directory structure, type:

user@hostname:/usr/bin$ cd ..

You will notice that your current directory has now changed from /usr/bin, to /usr, and your prompt may look like:

user@hostname:/usr$

Sometimes your shell is configured in a way where your current directory is not displayed in a prompt. In this case, type 'pwd' at the prompt to print the working directory:

user@hostname:/usr$ pwd
/usr

To list the contents of the current directory, use the command 'ls':

user@hostname:/usr$ ls
bin  etc  games  include  lib  libexec  local  lost+found  sbin  share  src  X11R6

To list the items in more detail, use the '-l' parameter, like so:

user@hostname:/usr$ ls -l
drwxr-xr-x   2 root root  81920 2007-09-03 10:57 bin
drwxr-xr-x   3 root root   4096 2007-08-20 11:56 etc
drwxr-xr-x   2 root root   4096 2007-05-24 21:22 games
drwxr-xr-x 100 root root  12288 2007-08-21 08:09 include
drwxr-xr-x 233 root root 131072 2007-09-03 10:57 lib
drwxr-xr-x   3 root root   4096 2007-08-23 10:38 libexec
drwxr-xr-x  11 root root   4096 2007-05-07 07:15 local
drwx------   2 root root  16384 2007-04-22 12:19 lost+found
drwxr-xr-x   2 root root  12288 2007-08-30 08:05 sbin
drwxr-xr-x 418 root root  12288 2007-09-02 10:45 share
drwxrwsr-x   7 root src    4096 2007-06-08 20:27 src
drwxr-xr-x   3 root root   4096 2007-04-15 06:50 X11R6

The 'd' at the begining of the line indicates that the listed item is a directory. You can use 'cd' to change to a directory using a pathname relative to your current directory:

user@hostname:/usr$ cd sbin
user@hostname:/usr/sbin$ pwd
/usr/sbin

Using these three commands, you can navigate to any part of the directory structure you have permissions to. It is also useful to know that calling 'cd' with no parameters will always take you back to your home directory:

user@hostname:/usr/sbin$ cd
user@hostname:~$pwd
/home/user

[edit] The man Command

Often times, you find yourself needing to learn about how to use a specific command. What you may not know, is that the best resource available to you is already at your fingertips. This is where you need the man command. The man command will display the manual page for a specified command. For example, if you wanted to see all the different options you can use to run the 'ls' command, simply type:

user@hostname:~$ man ls

Use the Up/Down arrows and PageUp/PageDown to navigate through the manual page. When you no longer wish to view the manual page, press 'q'.

[edit] Specifying Current Directory and Specifying the Parent Directory and Specifying Your Home Directory

There are times when you may need to specify your current directory or the parent directory of the current directory. For this, we use the notation '.' and '..' respectively. For example, you may want to use the program cp to copy a file from one directory to the current directory. The command for this would be:

user@hostname:~/ cp /home/user/somedirectory/somefile .

This will copy the file 'somefile', which is located at /home/user/somedirectory, to the current directory, which in this case is /home/user. Perhaps you wanted to copy the file to /home, but you didn't want to change directories. This could be done with the command:

user@hostname:~/ cp /home/user/somedirectory/somefile ..

Furthermore, perhaps you didn't want to type out /home/user for the first parameter. In this case, you can replace your user directory with '~', and leave the rest of the path alone:

user@hostname:~/ cp ~/somedirectory/somefile ..

[edit] Bash History

The Bash shell includes a feature where you can browse though the history of commands you have entered. To do this, simply press the UP ARROW on the keyboard to scroll backwards in history through all of your previously entered commands

[edit] Bang (!)

A '!' (pronounced bang) can be typed at the beginning of a command in order to run the previous command that began with the letters following the '!'. For example, if the last time I ran the vim editor, I called:

user@hostname:~$ vim /etc/X11/xorg.conf

I could call the same command simply by typing:

user@hostname:~$ !vim

If the desired command was the last one to be called that began with the letter 'v', I could do the same thing just by calling:

user@hostname:~$ !v


[edit] Tab Completion

The Bash shell (and other shells) have a feature called 'tab completion' that is extremely useful to both the novice and experienced user. If the user types the first few letters of a command, and then hits the TAB key, the shell will attempt to fill in the rest with whatever command starts with those letters. For example, if the user types 'gnomet' and then hits TAB, the shell will fill in the rest with the command 'gnometris', which launches a gtk-based Tetris clone. If 'gnometris' is not installed, and there are no other commands that begin with 'gnomet', the shell does nothing. If the user just types 'gnome' and hits TAB, nothing will happen the first time, because more than one command begin with 'gnome'. If the user then hits TAB a second time, the shell will list all the possible matches. This will also work with parameters that need to be passed in with a command. For example, if the user types 'cd /ho' and hits tab, the shell will automatically fill in the rest, so that the command is now 'cd /home'. It is suggested that you play around with tab completion to get a feel for how it works. It will save you lots of time typing, and can help you to find the names of commands.

Personal tools