Emmabuntus, Ubuntu, Derivate, Linux, Open Source BackTrack, Linux, distributions, Ubuntu, derivate, securuty, forensic VirtualBox, Linux, Ubuntu, Raring Ringtail synaptic, Ubuntu, Linux, software packages jwplayer, multimedia, Linux, Ubuntu, flash Meshlab, graphic, software, Ubuntu, open source, Linux Synapse, Linux, Ubuntu, raring, Quantal Gimp, Ubuntu, Linux FreeMind, Linux, open source Linux, infographic, history

Running Commands from the Shell

linux_logo Using the Linux shell
Before icons and windows took over computer screens, you typed commands to run most computers.
On UNIX Working with the Linux file systems, from which Linux was derived, the program used to interpret and manage commands was referred to as the shell.

Using the vi text editor in Linux.

No matter which Linux distribution you are using, you can always count on one thing being available to you: the shell. It provides a way to run programs, work with file systems, compile computer code, operate a system, and manage the computer. Although the shell is less intuitive than common graphic user interfaces (GUIs), most Linux experts consider the shell to be much more powerful than GUIs. Shells have been around a long time, and many advanced features have been built into them.

The Linux shell illustrated in this chapter is called the bash shell, which stands for Bourne Again Shell. The name is derived from the fact that bash is compatible with the first UNIX shell: the Bourne shell (represented by the sh command). Other popular shells include the C shell (csh), which is popular among BSD UNIX users, and the Korn shell (ksh), which is popular among UNIX System V users. Linux also has a tcsh shell (a C shell look-alike) and an ash shell (another Bourne shell look-alike).

Several major reasons for learning how to use the shell are:
You will know how to get around any Linux or other UNIX-like system. For example, I can log in to my Red
Hat Linux MySQL server, my bootable floppy router/firewall, or my wife’s iMac and explore and use any of
those computer systems from a shell.

Special shell features enable you to gather data input and direct data output between commands and the Linux file system. To save on typing, you can find, edit, and repeat commands from your shell history. Many power users hardly touch a graphical interface, doing most of their work from a shell.

You can gather commands into a file using programming constructs such as loops and case statements to quickly do complex operations that would be difficult to retype over and over. Programs consisting of commands that are stored and run from a file are referred to as shell scripts. Most Linux system administrators use shell scripts to automate tasks such as backing up data, monitoring log files, or checking system health.

The shell is a command language interpreter. If you have used Microsoft operating systems, you’ll see that using a shell in Linux is similar to — but generally much more powerful than — the interpreter used to run commands in DOS. You can happily use Linux from a graphical desktop interface, but as you grow into Linux you will surely need to use the shell at some point to track down a problem or administer some features.

How to use the shell isn’t obvious at first, but with the right help you can quickly learn many of the most impor tant shell features. This chapter is your guide to working with the Linux system commands, processes, and file system from the shell. It describes the shell environment and helps you tailor it to your needs. It also explains how to use and move around the file system.

Starting a Shell
There are several ways to get to a shell interface in Linux. Three of the most common are the shell prompt, Terminal window, and virtual terminal. They’re discussed in the following sections.

Using the Shell Prompt

If your Linux system has no graphical user interface (or one that isn’t working at the moment), you will most likely see a shell prompt after you log in., Typing commands from the shell will probably be your primary means of using the Linux system.

The default prompt for a regular user is simply a dollar sign:
The default prompt for the root user is a pound sign (also called a hash mark):

In most Linux systems, the $ and # prompts are preceded by your username, system name, and current directory name. For example, a login prompt for the user named jake on a computer named pine with /tmp as the current directory would appear as:
[jake@pine tmp]$
You can change the prompt to display any characters you like — you can use the current directory, the date, the local computer name, or any string of characters as your prompt, for example.
Although there are a tremendous number of features available with the shell, it’s easy to begin by just typing a few commands. Try some of the commands shown in the remainder of this section to become familiar with your current shell environment.
In the examples that follow, the $ and # symbols indicate a prompt. The prompt is followed by the command that you type (and then you press Enter or Return, depending on your keyboard). The lines that follow show the output resulting from the command.

Using a Terminal Window
With the desktop GUI running, you can open a terminal emulator program (sometimes referred to as a Terminal window) to start a shell. Most Linux distributions make it easy for you to get to a shell from the GUI. Here are two common ways to launch a Terminal window from a Linux desktop:
Right-click the desktop. In the context menu that appears, look for Shells,New Terminal, Terminal Window, Xterm, or some similar item and select it.
Click on the panel menu. Many Linux desktops include a panel at the bottom of the screen from which you can launch applications. For example, in Red Hat Linux systems, you can select the Red Hat icon, and then choose System Tools Terminal to open a Terminal window.
In all cases, you should just be able to type a command as you would from a shell with no GUI. There are different terminal emulators available with Linux. One of the following is likely to be the default used with your Linux system:
xterm — A common terminal emulator for the X Window System (In fact, I’ve never seen an X Window System for a major Linux distribution that didn’t include xterm). Although it doesn’t provide menus or many special features, it is available with most Linux distributions that support a GUI.
gnome-terminal— The default Terminal emulator window that comes with GNOME. It consumes more system resources than xterm does, and it has useful menus for cutting and pasting, opening new Terminal tabs or windows, and setting terminal profiles.
kterm — The kterm terminal emulator that comes with the KDE desktop environment. With kterm, you can display multi-language text encoding and text in different colors.
If you don’t like the terminal emulator you get by default, type a command name from the Emulator column to try out one of those instead.

Using Virtual Terminals
Many Linux systems, including Fedora and Red Hat Enterprise Linux, start multiple virtual terminals running on the computer. Virtual terminals are a way to have multiple shell sessions open at once without having a GUI running.
You can switch between virtual terminals much the same way that you would switch between workspaces on a GUI. Press Ctrl+Alt+F1 (or F2, F3, F4, and so on up to F6 on Fedora and other Linux systems) to display one of six vir tual terminals.
The next vir tual workspace after the virtual terminals is where the GUI is, so if there are six virtual terminals, you can return to the GUI (if one is running) by pressing Ctrl+Alt+F7. (For a system with four virtual terminals, you’d return to the GUI by pressing Ctrl+Alt+F5.)

Choosing Your Shell
In most Linux systems, your default shell is the bash shell. To find out what your current login shell is, type the following command:
$ echo $SHELL
In this example, it’s the bash shell. There are many other shells, and you can activate a different one by simply typing the new shell’s command (ksh, tcsh, csh, sh, bash, and so forth) from the current shell.
Most full Linux systems include all of the shells described in this section. However, some smaller Linux distributions may include only one or two shells. The best way to find out if a particular shell is available is to type the command and see if the shell starts.
You might want to choose a different shell to use because:
You are used to using UNIX System V systems (often ksh by default) or Sun Microsystems and other Berkeley UNIX–based distributions (frequently csh by default), and you are more comfor table using default shells from those environments.

You want to run shell scripts that were created for a particular shell environment, and you need to run the shell for which they were made so you can test or use those scripts.
You might simply like features in one shell over those in another. For example, a member of my Linux Users Group prefers ksh over bash because he doesn’t like the way aliases are always set up with bash.
Although most Linux users have a preference for one shell or another, when you know how to use one shell, you can quickly learn any of the others by occasionally referring to the shell’s man page (for example, type man bash). Most people use
bash just because they don’t have a particular reason for using a different shell. In Chapter 4, you learn how to assign a different default shell for a user.
The following sections introduce several of the most common shells available with Linux.

Using bash (and Earlier sh) Shells
The name bash is an acronym for Bourne Again Shell, acknowledging the roots of bash coming from the Bourne shell (sh command) created by Steve Bourne at AT&T Bell Labs. Brian Fox of the Free Software foundation created bash, under the auspices of the GNU project. Development was later taken over by Chet Ramey at Case Western Reser ve University.
Bash includes features originally developed for sh and ksh shells in early UNIX systems, as well as some csh features. Expect bash to be the default shell in whatever Linux system you are using, with the exception of some specialized Linux systems (such as those run on embedded devices or run from a floppy disk) that may
require a smaller shell that needs less memory and entails fewer features. Most of the examples in this chapter are based on the bash shell.
Bash can be run in various compatibility modes so that it behaves like different shells. It can be run to behave as a Bourne shell (sh) or as a POSIX-compliant shell (bash --posix), for example, enabling it to read configuration files that are specific to those shells and run shell scripts written directly for those shells, with a greater chance of success.
All of the Linux distributions included with this book use bash as the default shell, with the exception of some bootable Linux distributions, which use the ash shell instead.

Using tcsh (and Earlier csh) Shells
The tcsh shell is the open source version of the C shell (csh). The csh shell was created by Bill Joy and used with most Berkeley UNIX systems (such as those produced by Sun Microsystems) as the default shell.
Many features of the original csh shell, such as command-line editing and its history mechanism, are included in tcsh as well as in other shells. While you can run both csh and tcsh on most Linux systems, both commands actually point to the same executable file. In other words, starting csh actually runs the tcsh shell in csh compatibility mode.

Using ash
The ash shell is a lightweight version of the Berkeley UNIX sh shell. It doesn’t include many of the sh shell’s basic features, and is missing such features as command histories. Kenneth Almquist created the ash shell.
The ash shell is a good shell for embedded systems that have fewer system resources available. The ash shell is about one-sixth the size of bash (about 100k versus 680k for bash). Because of cheaper memory prices these days, however, many embedded and small bootable Linux systems have enough space to include the full bash shell.

Using ksh
The ksh shell was created by David Korn at AT&T Bell Labs and is the predecessor of the sh shell. It became the default and most commonly used shell with UNIX System V systems. The open source version of ksh was originally available in many rpm-based systems (such as Fedora and Red Hat Enterprise Linux) as part of the pdksh package. Now, however, David Korn has released the original ksh shell as open source, so you can look for it as par t of a ksh software package in most Linux systems.

Using zsh
The zsh shell is another clone of the sh shell. It is POSIX-compliant (as is bash), but includes some different features, such as spell checking and a different approach to command editing. The first Mac OS X systems used zsh as the default shell, although now bash is used by default.

Exploring the Shell
Once you have access to a shell in Linux, you can begin by typing some simple commands. The “Using the Shell in Linux” section later in this chapter provides more details about options, arguments, and environment variables. For the time being, the following sections will help you poke around the shell a bit.
If you don’t like your default shell, simply type the name of the shell you want to try out temporarily. To change your shell permanently, use the usermod command. For example, to change your shell to the csh shell for the user named chris, type the following as root user from a shell:

# usermod -s /bin/csh chris

Checking Your Login Session
When you log in to a Linux system, Linux views you as having a particular identity, which includes your username, group name, user ID, and group ID. Linux also keeps track of your login session: it knows when you logged in, how long you have been idle, and where you logged in from.
To find out information about your identity, use the id command as follows:
$ id
uid=501(chris) gid=105(sales) groups=105(sales),4(adm),7(lp)
In this example, the username is chris, which is represented by the numeric user ID (uid) 501. The primar y group for chris is called sales, which has a group ID (gid) of 105. The user chris also belongs to other groups called adm (gid 4) and lp (gid 7). These names and numbers represent the permissions that chris has to
access computer resources. (Permissions are described in the “Understanding File Permissions” section later in this chapter.)
You can see information about your current login session by using the who command. In the following example, the -u option says to add information about idle time and the process ID and -H asks that a header be printed:
$ who -uH
chris tty1 Jan 13 20:57 . 2013
The output from this who command shows that the user chris is logged in on tty1
(which is the monitor connected to the computer), and his login session began at 20:57 on Januar y 13. The IDLE time shows how long the shell has been open without any command being typed (the dot indicates that it is currently active). PID shows the process ID of the user’s login shell. COMMENT would show the name of the remote computer the user had logged in from, if that user had logged in from another computer on the network, or the name of the local X display if you were using a Terminal window (such as :0.0).

Checking Directories and Permissions
Associated with each shell is a location in the Linux file system known as the current or working directory. Each user has a directory that is identified as the user’s home directory. When you first log in to Linux, you begin with your home director y as the current directory.
When you request to open or save a file, your shell uses the current directory as the point of reference. Simply provide a filename when you save a file, and it is placed in the current directory. Alternatively, you can identify a file by its relation to the current directory (relative path), or you can ignore the current directory and identify a file by the full director y hierarchy that locates it (absolute path). The structure and use of the file system is described in detail later in this chapter.
To find out what your current director y is, type the pwd command:

$ pwd

In this example, the current/working directory is /usr/bin. To find out the name of your home director y, type the echo command, followed by the $HOME variable:

$ echo $HOME

Here the home directory is /home/chris. To get back to your home directory, just type the change directory (cd) command. (Although cd followed by a directory name changes the current directory to the directory that you choose, simply typing cd with no directory name takes you to your home directory):

$ cd
Instead of typing $HOME, you can use the tilde (~) to refer to your home directory.
So, to return to your home directory, you could simply type: cd ~
To list the contents of your home director y, either type the full path to your home directory, or use the ls command without a directory name. Using the -a option to ls enables you to view the hidden files (dot files) as well as all other files. With the -l option, you can see a long, detailed list of information on each file. (You can put multiple single-letter options together after a single dash, for example, -la.)

$ ls -la /home/chris
total 158
drwxrwxrwx 2 chris sales 4096 May 12 13:55 .
drwxr-xr-x 3 root root 4096 May 10 01:49 ..
-rw------- 1 chris sales 2204 May 18 21:30 .bash_history
-rw-r--r-- 1 chris sales 24 May 10 01:50 .bash_logout
-rw-r--r-- 1 chris sales 230 May 10 01:50 .bash_profile
-rw-r--r-- 1 chris sales 124 May 10 01:50 .bashrc
drw-r--r-- 1 chris sales 4096 May 10 01:50 .kde
-rw-rw-r-- 1 chris sales 149872 May 11 22:49 letter
^ ^ ^ ^ ^ ^ ^
col 1 col 2 col 3 col 4 col 5 col 6 col 7
Displaying a long list (-l option) of the contents of your home director y shows you more about file sizes and directories. The total line shows the total amount of disk space used by the files in the list (158 kilobytes in this example). Directories such as the current directory (.) and the parent director y (..) — the directory above the current directory — are noted as directories by the letter d at the beginning of each entry (each directory begins with a d and each file begins with a -). The file and directory names are shown in column 7. In this example, a dot (.) represents /home/chris and two dots (..) represents /home. Most of the files in this example are dot (.) files that are used to store GUI properties (.kde directory) or shell properties (.bash files). The only non-dot file in this list is the one named letter.
Column 3 shows the directory or file owner. The /home directory is owned by root, and everything else owned by the user chris, who belongs to the sales group (groups are listed in column 4).
In addition to the d or -, column 1 on each line contains the permissions set for that file or directory. (Permissions and configuring shell property files are described later in this chapter.) Other information in the listing includes the number of links to the item (column 2) the size of each file in bytes (column 5) and the
date and time each file was most recently modified (column 6).
The number of characters shown for a directory (4096 bytes in these examples) reflects the size of the file containing information about the directory. While this number can grow above 4096 bytes for a directory that contains a lot of files, this number doesn’t reflect the size of files contained in that directory.

Checking System Activity
In addition to being a multiuser operating system, Linux is also a multitasking system. Multitasking means that many programs can be running at the same time. An instance of a running program is referred to as a process. Linux provides tools for listing running processes, monitoring system usage, and stopping (or killing) processes when necessar y.
The most common utility for checking running processes is the ps command. Use it to see which programs are running, the resources they are using, and who is running them. Here’s an example of the ps command:

$ ps -au

root 2146 0.0 0.8 1908 1100 ttyp0 S 14:50 0:00 login -- jake
jake 2147 0.0 0.7 1836 1020 ttyp0 S 14:50 0:00 -bash
jake 2310 0.0 0.7 2592 912 ttyp0 R 18:22 0:00 ps -au

In this example, the -a option asks to show processes of all users who are associated with your current terminal, and the -u option asks that usernames be shown,as well as other information such as the time the process star ted and memor y and CPU usage. The concept of a terminal comes from the old days, when people worked exclusively from character terminals, so a terminal typically represented a single person at a single screen. Now you can have many “terminals” on one screen by opening multiple Terminal windows.
On this shell session, there isn’t much happening. The first process shows that the user named jake logged in to the login process (which is controlled by the root user). The next process shows that jake is using a bash shell and has just run the ps -au command. The terminal device ttyp0 is being used for the login session.
The STAT column represents the state of the process, with R indicating a currently running process and S representing a sleeping process.
The USER column shows the name of the user who started the process. Each process is represented by a unique ID number referred to as a process ID (PID). (You can use the PID if you ever need to kill a runaway process.) The %CPU and %MEM columns show the percentage of the processor and random access memor y, respectively, that the process is consuming. VSZ (virtual set size) shows the size of the image process
(in kilobytes), and RSS (resident set size) shows the size of the program in memor y.
START shows the time the process began running, and TIME shows the cumulative system time used. (Many commands consume very little CPU time, as is reflected by 0:00 for processes that haven’t even used a whole second of CPU time.)
Many processes running on a computer are not associated with a terminal. A normal Linux system has many processes running in the background. Background system processes perform such tasks as logging system activity or listening for data coming in from the network. They are often started when Linux boots up and run continuously until it shuts down. To page through all the processes running on your Linux system, add the pipe (|) and the less command to ps –aux, like this:

$ ps -aux | less

A pipe lets you direct the output of one command to be the input of the next command, so in this example, the output of the ps command (a list of processes) is directed to the less command, which lets you page through that information. Use the spacebar to page through and type q to end the list. You can also use the arrow keys to move one line at a time through the output.

Exiting the Shell
To exit the shell when you are done, type exit or press Ctrl+D.
You’ve just seen a few commands that can help you quickly familiarize yourself with your Linux system. There are hundreds of other commands that you can try.
You’ll find many in the /bin and /usr/bin directories, and you can use ls to see a directory’s command list: ls /bin, for example, results in a list of commands in the /bin. Then use the man command (for example, man hostnameto see what each command does. There are also administrative commands in /sbin or /usr/sbin

If you liked this article, subscribe to the feed by clicking the image below to keep informed about new contents of the blog:


Related Post

    160x600     step

Linux Links

Share on Google Plus

About Hugo Repetto

Ubuntu is a Linux distribution that offers an operating system predominantly focused on desktop computers but also provides support for servers. Based on Debian GNU / Linux, Ubuntu focuses on ease of use, freedom in usage restriction, regular releases (every 6 months) and ease of installation.
    Blogger Comment
    Facebook Comment


Post a Comment