Applies to openSUSE Leap 15.0

14 Shell Basics

When working with Linux, you can communicate with the system almost without ever requiring a command line interpreter (the shell). After booting your Linux system, you are usually directed to a graphical user interface that guides you through the login process and the following interactions with the operating system. The graphical user interface in Linux is initially configured during installation and used by desktop environments such as KDE or GNOME.

Nevertheless, it is useful to have some basic knowledge of working with a shell because you might encounter situations where the graphical user interface is not available. For example, if some problem with the X Window System occurs. If you are not familiar with a shell, you might feel a bit uncomfortable at first when entering commands, but the more you get used to it, the more you will realize that the command line is often the quickest and easiest way to perform some daily tasks.

For Unix or Linux, several shells are available which differ slightly in behavior and in the commands they accept. The default shell in openSUSE® Leap is Bash (GNU Bourne-Again Shell).

The following sections will guide you through your first steps with the Bash shell and will show you how to complete some basic tasks via the command line. If you are interested in learning more or rather feel like a shell power user already, refer to Chapter 15, Bash and Bash Scripts.

14.1 Starting a Shell

Basically, there are two different ways to start a shell from the graphical user interface which usually shows after you have booted your computer:

  • you can leave the graphical user interface or

  • you can start a terminal window within the graphical user interface.

While the first option is always available, you can only make use of the second option when you are already logged in to a desktop such as KDE or GNOME. Whichever way you choose, there is always a way back and you can switch back and forth between the shell and the graphical user interface.

If you want to give it a try, press CtrlAltF2 to leave the graphical user interface. The graphical user interface disappears and you are taken to a shell which prompts you to log in. Type your username and press Enter. Then type your password and press Enter. The prompt now changes and shows some useful information as in the following example:

 1   2   3
tux@linux:~>

1

Your login.

2

The hostname of your computer.

3

Path to the current directory. Directly after login, the current directory usually is your home directory, indicated by the ~ symbol (tilde) .

When you are logged in at a remote computer the information provided by the prompt always shows you which system you are currently working on.

When the cursor is located behind this prompt, you can pass commands directly to your computer system. For example, you can now enter ls -l to list the contents of the current directory in a detailed format. If this is enough for your first encounter with the shell and you want to go back to the graphical user interface, you should log out from your shell session first. To do so, type exit and press Enter. Then press AltF7 to switch back to the graphical user interface. You will find your desktop and the applications running on it unchanged.

When you are already logged in to the GNOME or the KDE desktop and want to start a terminal window within the desktop, press AltF2 and enter konsole (for KDE) or gnome-terminal (for GNOME). This opens a terminal window on your desktop. As you are already logged in to your desktop, the prompt shows information about your system as described above. You can now enter commands and execute tasks just like in any shell which runs parallel to your desktop. To switch to another application on the desktop just click on the corresponding application window or select it from the taskbar of your panel. To close the terminal window press AltF4.

14.2 Entering Commands

As soon as the prompt appears on the shell it is ready to receive and execute commands. A command can consist of several elements. The first element is the actual command, followed by parameters or options. You can type a command and edit it by using the following keys: , , Home, End, <— (Backspace), Del, and Space. You can correct typing errors or add options. The command is not executed until you press Enter.

Important
Important: No News Is Good News

The shell is not verbose: in contrast to some graphical user interfaces, it usually does not provide confirmation messages when commands have been executed. Messages only appear in case of problems or errors —or if you explicitly ask for them by executing a command with a certain option.

Also keep this in mind for commands to delete objects. Before entering a command like rm (without any option) for removing a file, you should know if you really want to get rid of the object: it will be deleted irretrievably, without confirmation.

14.2.1 Using Commands without Options

In Section 14.6.1, “Permissions for User, Group and Others” you already got to know one of the most basic commands: ls, which used to list the contents of a directory. This command can be used with or without options. Entering the plain ls command shows the contents of the current directory:

tux > ls
bin Desktop Documents public_html tux.txt
tux > 

Files in Linux may have a file extension or a suffix, such as .txt, but do not need to have one. This makes it difficult to differentiate between files and folders in this output of the ls. By default, the colors in the Bash shell give you a hint: directories are usually shown in blue, files in black.

14.2.2 Using Commands with Options

A better way to get more details about the contents of a directory is using the ls command with a string of options. Options modify the way a command works so that you can get it to carry out specific tasks. Options are separated from the command with a blank and are usually prefixed with a hyphen. The ls -l command shows the contents of the same directory in full detail (long listing format):

tux > ls -l
drwxr-xr-x 1 tux users     48 2015-06-23 16:08 bin
drwx---r-- 1 tux users  53279 2015-06-21 13:16 Desktop
drwx------ 1 tux users    280 2015-06-23 16:08 Documents
drwxr-xr-x 1 tux users  70733 2015-06-21 09:35 public_html
-rw-r--r-- 1 tux users  47896 2015-06-21 09:46 tux.txt
tux > 

This output shows the following information about each object:

drwxr-xr-x1 12 tux3 users4 485 2006-06-23 16:086 bin7

1

Type of object and access permissions. For further information, refer to Section 14.6.1, “Permissions for User, Group and Others”.

2

Number of hard links to this file.

3

Owner of the file or directory. For further information, refer to Section 14.6.1, “Permissions for User, Group and Others”.

4

Group assigned to the file or directory. For further information, refer to Section 14.6.1, “Permissions for User, Group and Others”.

5

File size in bytes.

6

Date and time of the last change.

7

Name of the object.

Usually, you can combine several options by prefixing only the first option with a hyphen and then write the others consecutively without a blank. For example, if you want to see all files in a directory in long listing format, you can combine the two options -l and -a (show all files) for the ls command. Executing ls -la shows also hidden files in the directory, indicated by a dot in front (for example, .hiddenfile).

The list of contents you get with ls is sorted alphabetically by filenames. But like in a graphical file manager, you can also sort the output of ls -l according to various criteria such as date, file extension or file size:

  • For date and time, use ls -lt (displays newest first).

  • For extensions, use ls -lx (displays files with no extension first).

  • For file size, use ls -lS (displays largest first).

To revert the order of sorting, add -r as an option to your ls command. For example, ls -lr gives you the contents list sorted in reverse alphabetical order, ls -ltr shows the oldest files first. There are lots of other useful options for ls. In the following section you will learn how to investigate them.

14.2.3 Bash Shortcut Keys

After having entered several commands, your shell will begin to fill up with all sorts of commands and the corresponding outputs. In the following table, find some useful shortcut keys for navigating and editing in the shell.

Shortcut Key

Function

CtrlL

Clears the screen and moves the current line to the top of the page.

CtrlC

Aborts the command which is currently being executed.

ShiftPage ↑

Scrolls upwards.

ShiftPage ↓

Scrolls downwards.

CtrlU

Deletes from cursor position to start of line.

CtrlK

Deletes from cursor position to the end of line.

CtrlD

Closes the shell session.

,

Browses in the history of executed commands.

14.3 Getting Help

If you remember the name of command but are not sure about the options or the syntax of the command, choose one of the following possibilities:

--help/-h option

If you only want to look up the options of a certain command, try entering the command followed by a space and --help. This --help option exists for many commands. For example, ls --help displays all the options for the ls command.

Manual Pages

To learn more about the various commands, you can also use the manual pages. Manual pages also give a short description of what the command does. They can be accessed with man followed by the name of the command, for example, man ls.

Man pages are displayed directly in the shell. To navigate them, use the following keys:

  • Move up and down with Page ↑ and Page ↓

  • Move between the beginning and the end of a document with Home and End

  • Quit the man page viewer by pressing Q

For more information about the man command, use man man.

Info Pages

Info pages usually provide even more information about commands. To view the info page for a certain command, enter info followed by the name of the command (for example, info ls).

Info pages are displayed directly in the shell. To navigate them, use the following keys:

  • Use Space to move forward a section (node). Use <— to move backward a section.

  • Move up and down with Page ↑ and Page ↓

  • Quit the info page viewer by pressing Q

Note that man pages and info pages do not exist for all commands. Sometimes both are available (usually for key commands), sometimes only a man page or an info page exists, and sometimes neither of them are available.

14.4 Working with Files and Directories

To address a certain file or directory, you must specify the path leading to that directory or file. There are two ways to specify a path:

Absolute Path

The entire path from the root directory (/) to the relevant file or directory. For example, the absolute path to a text file named file.txt in your Documents directory might be:

/home/tux/Documents/file.txt
Relative Path

The path from the current working directory to the relevant file or directory. If your current working directory is /home/tux, the relative path file.txt in your Documents directory is:

Documents/file.txt

However, if your working directory is /home/tux/Music instead, you need to move up a level to /home/tux (with ..) before you can go further down:

../Documents/file.txt

Paths contain file names, directories or both, separated by slashes. Absolute paths always start with a slash. Relative paths do not have a slash at the beginning, but can have one or two dots.

When entering commands, you can choose either way to specify a path, depending on your preferences or the amount of typing, both will lead to the same result. To change directories, use the cd command and specify the path to the directory.

Note
Note: Handling Blanks in Filenames or Directory Names

If a filename or the name of a directory contains a space, either escape the space using a back slash (\) in front of the blank or enclose the filename in single quotes. Otherwise Bash interprets a filename like My Documents as the names of two files or directories, My and Documents in this case.

When specifying paths, the following shortcuts can save you a lot of typing:

  • The tilde symbol (~) is a shortcut for home directories. For example, to list the contents of your home directory, use ls ~. To list the contents of another user's home directory, enter ls ~USERNAME (or course, this will only work if you have permission to view the contents, see Section 14.6, “File Access Permissions”). For example, entering ls ~tux would list the contents of the home directory of a user named tux. You can use the tilde symbol as shortcut for home directories also if you are working in a network environment where your home directory may not be called /home but can be mapped to any directory in the file system.

    From anywhere in the file system, you can reach your home directory by entering cd ~ or by simply entering cd without any options.

  • When using relative paths, refer to the current directory with a dot (.). This is mainly useful for commands such as cp or mv by which you can copy or move files and directories.

  • The next higher level in the tree is represented by two dots (..). In order to switch to the parent directory of your current directory, enter cd .., to go up two levels from the current directory enter cd ../.. etc.

To apply your knowledge, find some examples below. They address basic tasks you may want to execute with files or folders using Bash.

14.4.1 Examples for Working with Files and Directories

Suppose you want to copy a file located somewhere in your home directory to a subdirectory of /tmp that you need to create first.

Procedure 14.1: Creating and Changing Directories

From your home directory create a subdirectory in /tmp:

  1. Enter

    tux > mkdir /tmp/test

    mkdir stands for make directory. This command creates a new directory named test in the /tmp directory. In this case, you are using an absolute path to create the test directory.

  2. To check what happened, now enter

    tux > ls -l /tmp

    The new directory test should appear in the list of contents of the /tmp directory.

  3. Switch to the newly created directory with

    tux > cd /tmp/test
Procedure 14.2: Creating and Copying Files

Now create a new file in a subdirectory of your home directory and copy it to /tmp/test. Use a relative path for this task.

Important
Important: Overwriting of Existing Files

Before copying, moving or renaming a file, check if your target directory already contains a file with the same name. If yes, consider changing one of the filenames or use cp or mv with options like -i, which will prompt before overwriting an existing file. Otherwise Bash will overwrite the existing file without confirmation.

  1. To list the contents of your home directory, enter

    tux > ls -l ~

    It should contain a subdirectory called Documents by default. If not, create this subdirectory with the mkdir command you already know:

    tux > mkdir ~/Documents
  2. To create a new, empty file named myfile.txt in the Documents directory, enter

    tux > touch ~/Documents/myfile.txt

    Usually, the touch command updates the modification and access date for an existing file. If you use touch with a filename which does not exist in your target directory, it creates a new file.

  3. Enter

    tux > ls -l ~/Documents

    The new file should appear in the list of contents.

  4. To copy the newly created file, enter

    tux > cp ~/Documents/myfile.txt .

    Do not forget the dot at the end.

    This command tells Bash to go to your home directory and to copy myfile.txt from the Documents subdirectory to the current directory, /tmp/test, without changing the name of the file.

  5. Check the result by entering

    tux > ls -l

    The file myfile.txt should appear in the list of contents for /tmp/test.

Procedure 14.3: Renaming and Removing Files or Directories

Now suppose you want to rename myfile.txt into tuxfile.txt. Finally you decide to remove the renamed file and the test subdirectory.

  1. To rename the file, enter

    tux > mv myfile.txt tuxfile.txt
  2. To check what happened, enter

    tux > ls -l

    Instead of myfile.txt, tuxfile.txt should appear in the list of contents.

    mv stands for move and is used with two options: the first option specifies the source, the second option specifies the target of the operation. You can use mv either

    • to rename a file or a directory,

    • to move a file or directory to a new location or

    • to do both in one step.

  3. Coming to the conclusion that you do not need the file any longer, you can delete it by entering

    tux > rm tuxfile.txt

    Bash deletes the file without any confirmation.

  4. Move up one level with cd .. and check with

    tux > ls -l test

    if the test directory is empty now.

  5. If yes, you can remove the test directory by entering

    tux > rmdir test

14.5 Becoming Root

root, also called the superuser, has privileges which authorize him to access all parts of the system and to execute administrative tasks. He or she has the unrestricted capacity to make changes to the system and has unlimited access to all files. Therefore performing some administrative tasks or running certain programs such as YaST requires root permissions.

14.5.1 Using su

In order to temporarily become root in a shell, proceed as follows:

  1. Enter su. You are prompted for the root password.

  2. Enter the password. If you mistyped the root password, the shell displays a message. In this case, you have to re-enter su before retyping the password. If your password is correct, a hash symbol # appears at the end of the prompt, signaling that you are acting as root now.

  3. Execute your task. For example, transfer ownership of a file to a new user which only root is allowed to do:

    tux > chown wilber kde_quick.xml
  4. After having completed your tasks as root, switch back to your normal user account. To do so, enter

    tux > exit

    The hash symbol disappears and you are acting as normal user again.

14.5.2 Using sudo

Alternatively, you can also use sudo (superuser do) to execute some tasks which normally are for roots only. With sudo, administrators can grant certain users root privileges for some commands. Depending on the system configuration, users can then run root commands by entering their normal password only. Due to a timestamp function, users are only granted a ticket for a restricted period of time after having entered their password. The ticket usually expires after a few minutes. In openSUSE, sudo requires the root password by default (if not configured otherwise by your system administrator).

For users, sudo is convenient as it prevents you from switching accounts twice (to root and back again). To change the ownership of a file using sudo, only one command is necessary instead of three:

tux > sudo chown wilber kde_quick.xml

After you have entered the password which you are prompted for, the command is executed. If you enter a second root command shortly after that, you are not prompted for the password again, because your ticket is still valid. After a certain amount of time, the ticket automatically expires and the password is required again. This also prevents unauthorized persons from gaining root privileges in case a user forgets to switch back to his normal user account again and leaves a root shell open.

14.6 File Access Permissions

In Linux, objects such as files or folders or processes generally belong to the user who created or initiated them. There are some exceptions to this rule. For more information about the exceptions, refer to Book “Security Guide”, Chapter 10 “Access Control Lists in Linux”. The group which is associated with a file or a folder depends on the primary group the user belongs to when creating the object.

When you create a new file or directory, initial access permissions for this object are set according to a predefined scheme. As an owner of a file or directory, you can change the access permissions for this object. For example, you can protect files holding sensitive data against read access by other users and you can authorize the members of your group or other users to write, read, or execute several of your files where appropriate. As root, you can also change the ownership of files or folders.

14.6.1 Permissions for User, Group and Others

Three permission sets are defined for each file object on a Linux system. These sets include the read, write, and execute permissions for each of three types of users—the owner, the group, and other users.

The following example shows the output of an ls -l command in a shell. This command lists the contents of a directory and shows the details for each file and folder in that directory.

Example 14.1: Access Permissions For Files and Folders
-rw-r----- 1 tux users      0 2015-06-23 16:08 checklist.txt
-rw-r--r-- 1 tux users  53279 2015-06-21 13:16 gnome_quick.xml
-rw-rw---- 1 tux users      0 2015-06-23 16:08 index.htm
-rw-r--r-- 1 tux users  70733 2015-06-21 09:35 kde-start.xml
-rw-r--r-- 1 tux users  47896 2015-06-21 09:46 kde_quick.xml
drwxr-xr-x 2 tux users     48 2015-06-23 16:09 local
-rwxr--r-- 1 tux users 624398 2015-06-23 15:43 tux.sh

As shown in the third column, all objects belong to user tux. They are assigned to the group users which is the primary group the user tux belongs to. To retrieve the access permissions the first column of the list must be examined more closely. Let's have a look at the file kde-start.xml:

Type

User Permissions

Group Permissions

Permissions for Others

-

rw-

r--

r--

The first column of the list consists of one leading character followed by nine characters grouped in three blocks. The leading character indicates the file type of the object: in this case, the hyphen () shows that kde-start.xml is a file. If you find the character d instead, this shows that the object is a directory, like local in Example 14.1, “Access Permissions For Files and Folders”.

The next three blocks show the access permissions for the owner, the group and other users (from left to right). Each block follows the same pattern: the first position shows read permissions (r), the next position shows write permissions (w), the last one shows execute permission (x). A lack of either permission is indicated by -. In our example, the owner of kde-start.xml has read and write access to the file but cannot execute it. The users group can read the file but cannot write or execute it. The same holds true for the other users as shown in the third block of characters.

14.6.2 Files and Folders

Access permissions have a slightly different impact depending on the type of object they apply to: file or directory. The following table shows the details:

Table 14.1: Access Permissions For Files And Directories

Access Permission

File

Folder

Read (r)

Users can open and read the file.

Users can view the contents of the directory. Without this permission, users cannot list the contents of this directory with ls -l, for example. However, if they only have execute permission for the directory, they can nevertheless access certain files in this directory if they know of their existence.

Write (w)

Users can change the file: They can add or drop data and can even delete the contents of the file. However, this does not include the permission to remove the file completely from the directory as long as they do not have write permissions for the directory where the file is located.

Users can create, rename or delete files in the directory.

Execute (x)

Users can execute the file. This permission is only relevant for files like programs or shell scripts, not for text files. If the operating system can execute the file directly, users do not need read permission to execute the file. However, if the file must me interpreted like a shell script or a perl program, additional read permission is needed.

Users can change into the directory and execute files there. If they do not have read access to that directory they cannot list the files but can access them nevertheless if they know of their existence.

Note that access to a certain file is always dependent on the correct combination of access permissions for the file itself and the directory it is located in.

14.6.3 Modifying File Permissions

In Linux, objects such as files or folder or processes generally belong to the user who created or initiated them. The group which is associated with a file or a folder depends on the primary group the user belongs to when creating the object. When you create a new file or directory, initial access permissions for this object are set according to a predefined scheme. For further details refer to Section 14.6, “File Access Permissions”.

As the owner of a file or directory (and, of course, as root), you can change the access permissions to this object.

To change object attributes like access permissions of a file or folder, use the chmod command followed by the following parameters:

  • the users for which to change the permissions,

  • the type of access permission you want to remove, set or add and

  • the files or folders for which you want to change permissions separated by spaces.

The users for which you can change file access permissions fall into the following categories: the owner of the file (user, u), the group that own the file (group, g) and the other users (others, o). You can add, remove or set one or more of the following permissions: read, write or execute.

As root, you can also change the ownership of a file: with the command chown (change owner) you can transfer ownership to a new user.

14.6.3.1 Examples for Changing Access Permissions and Ownership

The following example shows the output of an ls -l command in a shell.

Example 14.2: Access Permissions For Files and Folders
-rw-r----- 1 tux users      0 2015-06-23 16:08 checklist.txt
-rw-r--r-- 1 tux users  53279 2015-06-21 13:16 gnome_quick.xml
-rw-rw---- 1 tux users      0 2015-06-23 16:08 index.htm
-rw-r--r-- 1 tux users  70733 2015-06-21 09:35 kde-start.xml
-rw-r--r-- 1 tux users  47896 2015-06-21 09:46 kde_quick.xml
drwxr-xr-x 2 tux users     48 2015-06-23 16:09 local
-r-xr-xr-x 1 tux users 624398 2015-06-23 15:43 tux.jpg

In the example above, user tux owns the file kde-start.xml and has read and write access to the file but cannot execute it. The users group can read the file but cannot write or execute it. The same holds true for the other users as shown by the third block of characters.

Procedure 14.4: Changing Access Permissions

Suppose you are tux and want to modify the access permissions to your files:

  1. If you want to grant the users group also write access to kde-start.xml, enter

    tux > chmod g+w kde-start.xml
  2. To grant the users group and other users write access to kde-start.xml, enter

    tux > chmod go+w kde-start.xml
  3. To remove write access for all users, enter

    tux > chmod -w kde-start.xml

    If you do not specify any kind of users, the changes apply to all users— the owner of the file, the owning group and the others. Now even the owner tux does not have write access to the file without first reestablishing write permissions.

  4. To prohibit the users group and others to change into the directory local, enter

    tux > chmod go-x local
  5. To grant others write permissions for two files, for kde_quick.xml and gnome_quick.xml, enter

    tux > chmod o+w  kde_quick.xml gnome_quick.xml
Procedure 14.5: Changing Ownership

Suppose you are tux and want to transfer the ownership of the file kde_quick.xml to an other user, named wilber. In this case, proceed as follows:

  1. Enter the username and password for root.

  2. Enter

    root # chown wilber kde_quick.xml
  3. Check what happened with

    tux > ls -l kde_quick.xml

    You should get the following output:

    -rw-r--r-- 1 wilber users  47896 2006-06-21 09:46 kde_quick.xml
  4. If the ownership is set according to your wishes, switch back to your normal user account.

14.7 Time-Saving Features of Bash

Entering commands in Bash can involve a lot of typing. This section introduces some features that can save you both time and typing.

History

By default, Bash remembers commands you have entered. This feature is called history. You can browse through commands that have been entered before, select one you want to repeat and then execute it again. To do so, press repeatedly until the desired command appears at the prompt. To move forward through the list of previously entered commands, press . For easier repetition of a certain command from Bash history, just type the first letter of the command you want to repeat and press Page ↑.

You can now edit the selected command (for example, change the name of a file or a path), before you execute the command by pressing Enter. To edit the command line, move the cursor to the desired position using the arrow keys and start typing.

You can also search for a certain command in the history. Press CtrlR to start an incremental search function. showing the following prompt:

tux > (reverse-i-search)`':

Just type one or several letters from the command you are searching for. Each character you enter narrows down the search. The corresponding search result is shown on the right side of the colon whereas your input appears on the left of the colon. To accept a search result, press Esc. The prompt now changes to its normal appearance and shows the command you chose. You can now edit the command or directly execute it by pressing Enter.

Completion

Completing a filename or directory name to its full length after typing its first letters is another helpful feature of Bash. To do so, type the first letters then press →| (Tabulator). If the filename or path can be uniquely identified, it is completed at once and the cursor moves to the end of the filename. You can then enter the next option of the command, if necessary. If the filename or path cannot be uniquely identified (because there are several filenames starting with the same letters), the filename or path is only completed up to the point where it becomes ambiguous again. You can then obtain a list of them by pressing →| a second time. After this, you can enter the next letters of the file or path then try completion again by pressing →|. When completing filenames and paths with →|, you can simultaneously check whether the file or path you want to enter really exists (and you can be sure of getting the spelling right).

Wild Cards

You can replace one or more characters in a filename with a wild card for pathname expansion. Wild cards are characters that can stand for other characters. There are three different types of these in Bash:

Wild Card

Function

?

Matches exactly one arbitrary character

*

Matches any number of characters

[SET]

Matches one of the characters from the group specified inside the square brackets, which is represented here by the string SET.

14.7.1 Examples For Using History, Completion and Wildcards

The following examples illustrate how to make use of these convenient features of Bash.

Procedure 14.6: Using History and Completion

If you already did the example Section 14.4.1, “Examples for Working with Files and Directories”, your shell buffer should be filled with commands which you can retrieve using the history function.

  1. Press repeatedly until cd ~ appears.

  2. Press Enter to execute the command and to switch to your home directory.

    By default, your home directory contains two subdirectories starting with the same letter, Documents and Desktop.

  3. Type cd D and press →|.

    Nothing happens since Bash cannot identify to which one of the subdirectories you want to change.

  4. Press →| again to see the list of possible choices:

    tux > cd D
    Desktop/ Documents/ Downloads/
    tux > cd D
  5. The prompt still shows your initial input. Type the next character of the subdirectory you want to go to and press →| again.

    Bash now completes the path.

  6. You can now execute the command with Enter.

Procedure 14.7: Using Wildcards

Now suppose that your home directory contains several files with various file extensions. It also holds several versions of one file which you saved under different filenames myfile1.txt, myfile2.txt etc. You want to search for certain files according to their properties.

  1. First, create some test files in your home directory:

    1. Use the touch command to create several (empty) files with different file extensions, for example .pdf, .xml and .jpg.

      You can do this consecutively (do not forget to use the Bash history function) or with only one touch command: simply add several filenames separated by a space.

    2. Create at least two files that have the same file extension, for example .html.

    3. To create several versions of one file, enter

      tux > touch myfile{1..5}.txt

      This command creates five consecutively numbered files: myfile1.txt, …, myfile5.txt.

    4. List the contents of the directory. It should look similar to this:

      tux > ls -l
      -rw-r--r-- 1 tux users   0 2006-07-14 13:34 foo.xml
      -rw-r--r-- 1 tux users   0 2006-07-14 13:47 home.html
      -rw-r--r-- 1 tux users   0 2006-07-14 13:47 index.html
      -rw-r--r-- 1 tux users   0 2006-07-14 13:47 toc.html
      -rw-r--r-- 1 tux users   0 2006-07-14 13:34 manual.pdf
      -rw-r--r-- 1 tux users   0 2006-07-14 13:49 myfile1.txt
      -rw-r--r-- 1 tux users   0 2006-07-14 13:49 myfile2.txt
      -rw-r--r-- 1 tux users   0 2006-07-14 13:49 myfile3.txt
      -rw-r--r-- 1 tux users   0 2006-07-14 13:49 myfile4.txt
      -rw-r--r-- 1 tux users   0 2006-07-14 13:49 myfile5.txt
      -rw-r--r-- 1 tux users   0 2006-07-14 13:32 tux.png
  2. With wild cards, select certain subsets of the files according to various criteria:

    1. To list all files with the .html extension, enter

      tux > ls -l *.html
    2. To list all versions of myfile.txt, enter

      tux > ls -l myfile?.txt

      Note that you can only use the ? wild card here because the numbering of the files is single-digit. As soon as you have a file named myfile10.txt you must to use the * wild card to view all versions of myfile.txt (or add another question mark, so your string looks like myfile??.txt).

    3. To remove, for example, version 1-3 and version 5 of myfile.txt, enter

      tux > rm myfile[1-3,5].txt
    4. Check the result with

      tux > ls -l

      Of all myfile.txt versions only myfile4.txt should be left.

You can also combine several wild cards in one command. In the example above, rm myfile[1-3,5].* would lead to the same result as rm myfile[1-3,5].txt because there are only files with the extension .txt available.

Note
Note: Using Wildcards in rm Commands

Wildcards in a rm command can be very useful but also dangerous: you might delete more files from your directory than intended. To see which files would be affected by the rm, run your wildcard string with ls instead of rm first.

14.8 Editing Texts

In order to edit files from the command line, you will need to know the vi editor. vi is a default editor which can be found on nearly every UNIX/Linux system. It can run several operating modes in which the keys you press have different functions. This does not make it very easy for beginners, but you should know at least the most basic operations with vi. There may be situations where no other editor than vi is available.

Basically, vi makes use of three operating modes:

command mode

In this mode, vi accepts certain key combinations as commands. Simple tasks such as searching words or deleting a line can be executed.

insert mode

In this mode, you can write normal text.

extended mode

In this mode, also known as colon mode (as you have to enter a colon to switch to this mode), vi can execute also more complex tasks such as searching and replacing text.

In the following (very simple) example, you will learn how to open and edit a file with vi, how to save your changes and quit vi.

14.8.1 Example: Editing with vi

Note
Note: Display of Keys

In the following, find several commands that you can enter in vi by just pressing keys. These appear in uppercase as on a keyboard. If you need to enter a key in uppercase, this is stated explicitly by showing a key combination including the Shift key.

  1. To create and open a new file with vi, enter

    tux > vi textfile.txt

    By default, vi opens in command mode in which you cannot enter text.

  2. Press I to switch to insert mode. The bottom line changes and indicates that you now can insert text.

  3. Write some sentences. If you want to insert a new line, first press Esc to switch back to command mode. Press O to insert a new line and to switch to insert mode again.

  4. In the insert mode, you can edit the text with the arrow keys and with Del.

  5. To leave vi, press Esc to switch to command mode again. Then press : which takes you to the extended mode. The bottom line now shows a colon.

  6. To leave vi and save your changes, type wq (w for write; q for quit) and press Enter. If you want to save the file under a different name, type w FILENAME and press Enter.

    To leave vi without saving, type q! instead and press Enter.

14.9 Searching for Files or Contents

Bash offers you several commands to search for files and to search for the contents of files:

find

With find, search for a file in a given directory. The first argument specifies the directory in which to start the search. The option -name must be followed by a search string, which may also include wild cards. Unlike locate, which uses a database, find scans the actual directory.

grep

The grep command finds a specific search string in the specified text files. If the search string is found, the command displays the line in which searchstring was found, along with the filename. If desired, use wild cards to specify filenames.

14.9.1 Examples for Searching

  • To search your home directory for all occurrences of filenames that contain the file extension .txt, use:

    tux > find ~ -name '*.txt' -print
  • To search a directory (in this case, your home directory) for all occurrences of files which contain, for example, the word music, use:

    tux > grep music ~/*

    grep is case-sensitive by default. Hence, with the command above you will not find any files containing Music.To ignore case, use the -i option.

  • To use a search string which consists of more than one word, enclose the string in double quotation marks, for example:

    tux > grep "music is great" ~/*

14.10 Viewing Text Files

When searching for the contents of a file with grep, the output gives you the line in which the searchstring was found along with the filename. Often this contextual information is still not enough information to decide whether you want to open and edit this file. Bash offers you several commands to have a quick look at the contents of a text file directly in the shell, without opening an editor.

head

With head you can view the first lines of a text file. If you do not specify the command any further, head shows the first 10 lines of a text file.

tail

The tail command is the counterpart of head. If you use tail without any further options it displays the last 10 lines of a text file. This can be very useful to view log files of your system, where the most recent messages or log entries are usually found at the end of the file.

less

With less, display the whole contents of a text file. To move up and down half a page use Page ↑ and Page ↓. Use Space to scroll down one page. Home takes you to the beginning, and End to the end of the document. To end the viewing mode, press Q.

more

Instead of less, you can also use the older program more. It has basically the same function—however, it is less convenient because it does not allow you to scroll backward. Use Space to move forward. When you reach the end of the document, the viewer closes automatically.

cat

The cat command displays the contents of a file, printing the entire contents to the screen without interruption. As cat does not allow you to scroll it is not very useful as viewer but it is rather often used in combination with other commands.

14.11 Redirection and Pipes

Sometimes it would be useful if you could write the output of a command to a file for further editing or if you could combine several commands, using the output of one command as the input for the next one. The shell offers this function by means of redirection or pipes.

Normally, the standard output in the shell is your screen (or an open shell window) and the standard input is the keyboard. With certain symbols you can redirect the input or the output to another object, such as a file or another command.

Redirection

With > you can forward the output of a command to a file (output redirection), with < you can use a file as input for a command (input redirection).

Pipe

By means of a pipe symbol | you can also redirect the output: with a pipe, you can combine several commands, using the output of one command as input for the next command. In contrast to the other redirection symbols > and <, the use of the pipe is not constrained to files.

14.11.1 Examples for Redirection and Pipe

  1. To write the output of a command like ls to a file, enter

    tux > ls -l > filelist.txt

    This creates a file named filelist.txt that contains the list of contents of your current directory as generated by the ls command.

    However, if a file named filelist.txt already exists, this command overwrites the existing file. To prevent this, use >> instead of >. Entering

    tux > ls -l >> filelist.txt

    simply appends the output of the ls command to an already existing file named filelist.txt. If the file does not exist, it is created.

  2. Redirections also works the other way round. Instead of using the standard input from the keyboard for a command, you can use a file as input:

    tux > sort < filelist.txt

    This will force the sort command to get its input from the contents of filelist.txt. The result is shown on the screen. Of course, you can also write the result into another file, using a combination of redirections:

    tux > sort < filelist.txt > sorted_filelist.txt
  3. If a command generates a lengthy output, like ls -l may do, it may be useful to pipe the output to a viewer like less to be able to scroll through the pages. To do so, enter

    tux > ls -l | less

    The list of contents of the current directory is shown in less.

    The pipe is also often used in combination with the grep command in order to search for a certain string in the output of another command. For example, if you want to view a list of files in a directory which are owned by the user tux, enter

    tux > ls -l | grep tux

14.12 Starting Programs and Handling Processes

As you have seen in Section 14.8, “Editing Texts”, programs can be started from the shell. Applications with a graphical user interface need the X Window System and can only be started from a terminal window within a graphical user interface. For example, if you want to open a file named vacation.pdf in your home directory from a terminal window in KDE or GNOME, simply run okular ~/vacation.pdf (or evince ~/vacation.pdf) to start a PDF viewer displaying your file.

When looking at the terminal window again you will realize that the command line is blocked as long as the PDF viewer is open, meaning that your prompt is not available. To change this, press CtrlZ to suspend the process and enter bg to send the process to the background.

Now you can still have a look at vacation.pdf while your prompt is available for further commands. An easier way to achieve this is by sending a process to the background directly when starting it. To do so, add an ampersand at the end of the command:

tux > okular ~/vacation.pdf &

If you have started several background processes (also named jobs) from the same shell, the jobs command gives you an overview of the jobs. It also shows the job number in brackets and their status:

tux > jobs
[1]   Running        okular book.opensuse.startup-xep.pdf &
[2]-  Running        okular book.opensuse.reference-xep.pdf &
[3]+  Stopped        man jobs

To bring a job to the foreground again, enter fg JOB_NUMBER.

Whereas job only shows the background processes started from a specific shell, the ps command (run without options) shows a list of all your processes—those you started. Find an example output below:

tux > ps
PID TTY          TIME CMD
15500 pts/1    00:00:00 bash
28214 pts/1    00:00:00 okular
30187 pts/1    00:00:00 kwrite
30280 pts/1    00:00:00 ps

In case a program cannot be terminated in the normal way, use the kill command to stop the process (or processes) belonging to that program. To do so, specify the process ID (PID) shown by the output of ps. For example, to shut down the KWrite editor in the example above, enter

tux > kill 30187

This sends a TERM signal that instructs the program to shut itself down.

Alternatively, if the program or process you want to terminate is a background job and is shown by the jobs command, you can also use the kill command in combination with the job number to terminate this process. When identifying the job with the job number, you must prefix the number with a percent character (%):

tux > kill %JOB_NUMBER

If kill does not help—as is sometimes the case for runaway programs—try

tux > kill -9 PID

This sends a KILL signal instead of a TERM signal, usually bringing the specified process to an end.

This section is intended to introduce the most basic set of commands for handling jobs and processes. Find an overview for system administrators in Book “System Analysis and Tuning Guide”, Chapter 2 “System Monitoring Utilities”, Section 2.3 “Processes”.

14.13 Archives and Data Compression

On Linux, there are two types of commands that make data easier to transfer:

  • Archivers, which create a big file out of several smaller ones. The most commonly used archiver is tar, another example is cpio.

  • Compressors, which losslessly make a file smaller. The most commonly used compressors are gzip and bzip2.

When combining these two types of commands, their effect is comparable to the compressed archive files that are prevalent on other operating systems, for example, ZIP or RAR.

To pack the test directory with all its files and subdirectories into an archive named testarchive.tar, do the following:

Procedure 14.8: Archiving Files
  1. Open a shell.

  2. Use cd to change to your home directory where the test directory is located.

  3. Commpress the file with:

    tux > tar -cvf testarchive.tar test

    The -c option creates the archive, making it a file as directed by -f. The -v option lists the files as they are processed.

    The test directory with all its files and directories has remained unchanged on your hard disk.

  4. View the contents of the archive file with:

    tux > tar -tf testarchive.tar
  5. To unpack the archive, use:

    tux > tar -xvf testarchive.tar

    If files in your current directory are named the same as the files in the archive, they will be overwritten without warning.

To compress files, use gzip or, for better compression, bzip2.

Procedure 14.9: Compressing a File
  1. For this example, reuse the archive testarchive.tar from Procedure 14.8, “Archiving Files”.

    To compress the archive, use:

    tux > gzip testarchive.tar

    With ls, now see that the file testarchive.tar is no longer there and that the file testarchive.tar.gz has been created instead.

    As an alternative, use bzip2 testarchive.tar which works analogously but provides somewhat better compression.

  2. Now decompress and unarchive the file again:

    • This can be done in two steps by first decompressing and then unarchiving the file:

      tux > gzip --decompress testarchive.tar.gz
      tux > tar -xvf testarchive.tar
    • You can also decompress and unarchive in one step:

      tux > tar -xvf testarchive.tar

    With ls, you can see that a new test directory has been created with the same contents as your test directory in your home directory.

14.14 Important Linux Commands

This section provides an overview of the most important Linux commands. There are many more commands than listed in this chapter. Along with the individual commands, parameters are listed and, where appropriate, a typical sample application is introduced.

Adjust the parameters to your needs. It makes no sense to write ls file if no file named file actually exists. You can usually combine several parameters, for example, by writing ls -la instead of ls -l -a.

14.14.1 File Commands

The following section lists the most important commands for file management. It covers everything from general file administration to the manipulation of file system ACLs.

14.14.1.1 File Administration

ls OPTIONS FILES

If you run ls without any additional parameters, the program lists the contents of the current directory in short form.

-l

Detailed list

-a

Displays hidden files

cp OPTIONS SOURCE TARGET

Copies source to target.

-i

Waits for confirmation, if necessary, before an existing target is overwritten

-r

Copies recursively (includes subdirectories)

mv OPTIONS SOURCE TARGET

Copies source to target then deletes the original source.

-b

Creates a backup copy of the source before moving

-i

Waits for confirmation, if necessary, before an existing targetfile is overwritten

rm OPTIONS FILES

Removes the specified files from the file system. Directories are not removed by rm unless the option -r is used.

-r

Deletes any existing subdirectories

-i

Waits for confirmation before deleting each file

ln OPTIONS SOURCE TARGET

Creates an internal link from source to target. Normally, such a link points directly to source on the same file system. However, if ln is executed with the -s option, it creates a symbolic link that only points to the directory in which source is located, enabling linking across file systems.

-s

Creates a symbolic link

cd OPTIONS DIRECTORY

Changes the current directory. cd without any parameters changes to the user's home directory.

mkdir OPTIONS DIRECTORY

Creates a new directory.

rmdir OPTIONS DIRECTORY

Deletes the specified directory if it is already empty.

chown OPTIONS USER_NAME[:GROUP] FILES

Transfers ownership of a file to the user with the specified user name.

-R

Changes files and directories in all subdirectories

chgrp OPTIONS GROUP_NAME FILES

Transfers the group ownership of a given file to the group with the specified group name. The file owner can change group ownership only if a member of both the current and the new group.

chmod OPTIONS MODE FILES

Changes the access permissions.

The mode parameter has three parts: group, access, and access type. group accepts the following characters:

u

User

g

Group

o

Others

For access, grant access with + and deny it with -.

The access type is controlled by the following options:

r

Read

w

Write

x

Execute—executing files or changing to the directory

s

Setuid bit—the application or program is started as if it were started by the owner of the file

As an alternative, a numeric code can be used. The four digits of this code are composed of the sum of the values 4, 2, and 1—the decimal result of a binary mask. The first digit sets the set user ID (SUID) (4), the set group ID (2), and the sticky (1) bits. The second digit defines the permissions of the owner of the file. The third digit defines the permissions of the group members and the last digit sets the permissions for all other users. The read permission is set with 4, the write permission with 2, and the permission for executing a file is set with 1. The owner of a file would usually receive a 6 or a 7 for executable files.

gzip PARAMETERS FILES

This program compresses the contents of files using complex mathematical algorithms. Files compressed in this way are given the extension .gz and need to be uncompressed before they can be used. To compress several files or even entire directories, use the tar command.

-d

Decompresses the packed gzip files so they return to their original size and can be processed normally (like the command gunzip)

tar OPTIONS ARCHIVE FILES

tar puts one or more files into an archive. Compression is optional. tar is a quite complex command with several options available. The most frequently used options are:

-f

Writes the output to a file and not to the screen as is usually the case

-c

Creates a new TAR archive

-r

Adds files to an existing archive

-t

Outputs the contents of an archive

-u

Adds files, but only if they are newer than the files already contained in the archive

-x

Unpacks files from an archive (extraction)

-z

Packs the resulting archive with gzip

-j

Compresses the resulting archive with bzip2

-v

Lists files processed

The archive files created by tar end with .tar. If the TAR archive was also compressed using gzip, the ending is .tgz or .tar.gz. If it was compressed using bzip2, the ending is .tar.bz2.

find OPTIONS

With find, search for a file in a given directory. The first argument specifies the directory in which to start the search. The option -name must be followed by a search string, which may also include wild cards. Unlike locate, which uses a database, find scans the actual directory.

14.14.1.2 Commands to Access File Contents

file OPTIONS FILES

In Linux, files can have a file extensions but do not need to have one. The file determines the file type of a given file. With the output of file, you can then choose an appropriate application with which to open the file.

-z

Tries to look inside compressed files

cat OPTIONS FILES

The cat command displays the contents of a file, printing the entire contents to the screen without interruption.

-n

Numbers the output on the left margin

less OPTIONS FILES

This command can be used to browse the contents of the specified file. Scroll half a screen page up or down with Page ↑ and Page ↓ or a full screen page down with Space. Jump to the beginning or end of a file using Home and End. Press Q to quit the program.

grep OPTIONS SEARCH_STRING FILES

The grep command finds a specific search string in the specified files. If the search string is found, the command displays the line in which SEARCH_STRING was found along with the file name.

-i

Ignores case

-H

Only displays the names of the relevant files, but not the text lines

-n

Additionally displays the numbers of the lines in which it found a hit

-l

Only lists the files in which searchstring does not occur

diff OPTIONS FILE_1 FILE_2

The diff command compares the contents of any two files. The output produced by the program lists all lines that do not match. This is frequently used by programmers who need only to send their program alterations and not the entire source code.

-q

Only reports whether the two files differ

-u

Produces a unified diff, which makes the output more readable

14.14.1.3 File Systems

mount OPTIONS DEVICE MOUNT_POINT

This command can be used to mount any data media, such as hard disks, CD-ROM drives, and other drives, to a directory of the Linux file system.

-r

Mount read-only

-t FILE_SYSTEM

Specify the file system: For Linux hard disks, this is commonly ext4, xfs, or btrfs.

For hard disks not defined in the file /etc/fstab, the device type must also be specified. In this case, only root can mount it. If the file system needs to also be mounted by other users, enter the option user in the appropriate line in the /etc/fstab file (separated by commas) and save this change. Further information is available in the mount(1) man page.

umount OPTIONS MOUNT_POINT

This command unmounts a mounted drive from the file system. To prevent data loss, run this command before taking a removable data medium from its drive. Normally, only root is allowed to run the commands mount and umount. To enable other users to run these commands, edit the /etc/fstab file to specify the option user for the relevant drive.

14.14.2 System Commands

The following section lists a few of the most important commands needed for retrieving system information and controlling processes and the network.

14.14.2.1 System Information

df OPTIONS DIRECTORY

The df (disk free) command, when used without any options, displays information about the total disk space, the disk space currently in use, and the free space on all the mounted drives. If a directory is specified, the information is limited to the drive on which that directory is located.

-h

Shows the number of occupied blocks in gigabytes, megabytes, or kilobytes—in human-readable format

-T

Type of file system (ext2, nfs, etc.)

du OPTIONS PATH

This command, when executed without any parameters, shows the total disk space occupied by files and subdirectories in the current directory.

-a

Displays the size of each individual file

-h

Output in human-readable form

-s

Displays only the calculated total size

free OPTIONS

The command free displays information about RAM and swap space usage, showing the total and the used amount in both categories. See Book “Reference”, Chapter 15 “Special System Features”, Section 15.1.7 “The free Command” for more information.

-b

Output in bytes

-k

Output in kilobytes

-m

Output in megabytes

date OPTIONS

This simple program displays the current system time. If run as root, it can also be used to change the system time. Details about the program are available in the date(1) man page.

14.14.2.2 Processes

top OPTIONS

top provides a quick overview of the currently running processes. Press H to access a page that briefly explains the main options for customizing the program.

ps OPTIONS PROCESS_ID

If run without any options, this command displays a table of all your own programs or processes—those you started. The options for this command are not preceded by hyphen.

aux

Displays a detailed list of all processes, independent of the owner

kill OPTIONS PROCESS_ID

Unfortunately, sometimes a program cannot be terminated in the normal way. In most cases, you should still be able to stop such a runaway program by executing the kill command, specifying the respective process ID (see top and ps). kill sends a TERM signal that instructs the program to shut itself down. If this does not help, the following parameter can be used:

-9

Sends a KILL signal instead of a TERM signal, bringing the specified process to an end in almost all cases

killall OPTIONS PROCESS_NAME

This command is similar to kill, but uses the process name (instead of the process ID) as an argument, ending all processes with that name.

14.14.2.3 Network

ping OPTIONS HOSTNAME_OR_IP_ADDRESS

The ping command is the standard tool for testing the basic functionality of TCP/IP networks. It sends a small data packet to the destination host, requesting an immediate reply. If this works, ping displays a message to that effect, which indicates that the network link is functioning.

-cNUMBER

Determines the total number of packages to send and ends after they have been dispatched (by default, there is no limitation set)

-f

flood ping: sends as many data packages as possible; a popular means, reserved for root, to test networks

-iVALUE

Specifies the interval between two data packages in seconds (default: one second)

host OPTIONS HOSTNAME SERVER

The domain name system resolves domain names to IP addresses. With this tool, send queries to name servers (DNS servers).

ssh OPTIONS [USER@]HOSTNAME COMMAND

SSH is actually an Internet protocol that enables you to work on remote hosts across a network. SSH is also the name of a Linux program that uses this protocol to enable operations on remote computers.

14.14.2.4 Miscellaneous

passwd OPTIONS USER_NAME

Users may change their own passwords at any time using this command. The administrator root can use the command to change the password of any user on the system.

su OPTIONS USER_NAME

The su command makes it possible to log in under a different user name from a running session. Specify a user name and the corresponding password. The password is not required from root, because root is authorized to assume the identity of any user. When using the command without specifying a user name, you are prompted for the root password and change to the superuser (root). Use su - to start a login shell for a different user.

halt OPTIONS

To avoid loss of data, you should always use this program to shut down your system.

reboot OPTIONS

Does the same as halt except the system performs an immediate reboot.

clear

This command cleans up the visible area of the console. It has no options.

14.14.3 For More Information

There are many more commands than listed in this chapter. For information about other commands or more detailed information, also see the publication Linux in a Nutshell by O'Reilly.

Print this page