Содержание
When working with Linux these days, 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 (the X Window System or X11) is initially configured during installation. Both KDE and GNOME desktop (and other window managers you can install) use it for interaction with the user.
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® 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 Глава 18, Bash and Bash Scripts.
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 Ctrl+Alt+F2 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:


tux@linux:~>
| Your login. | |
| The hostname of your computer. | |
| 
     Path to the current directory. Directly after login, the current
     directory usually is your home directory, indicated by the
      | 
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 Alt+F7 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 Alt+F2 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 Alt+F4.
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), Delete, and Пробел. You can correct typing errors or add options. The command is not executed until you press Enter.
| ![[Important]](admon/important.png) | 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. | |
In Раздел 17.5.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@knox:~> ls bin Desktop Documents public_html tux.txt tux@knox:~>
    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.
   
     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@knox:~> ls -l drwxr-xr-x 1 tux users 48 2006-06-23 16:08 bin drwx---r-- 1 tux users 53279 2006-06-21 13:16 Desktop drwx------ 1 tux users 280 2006-06-23 16:08 Documents drwxr-xr-x 1 tux users 70733 2006-06-21 09:35 public_html -rw-r--r-- 1 tux users 47896 2006-06-21 09:46 tux.txt tux@knox:~>
This output shows the following information about each object:
drwxr-xr-x1
tux
users
48
2006-06-23 16:08
bin

| Type of object and access permissions. For further information, refer to Раздел 17.5.1, «Permissions for User, Group and Others». | |
| Number of hard links to this file. | |
| Owner of the file or directory. For further information, refer to Раздел 17.5.1, «Permissions for User, Group and Others». | |
| Group assigned to the file or directory. For further information, refer to Раздел 17.5.1, «Permissions for User, Group and Others». | |
| File size in bytes. | |
| Date and time of the last change. | |
| 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.
   
Nobody is expected to know all options of all commands by heart. If you remember the command name but are not sure about the options or the syntax of the command, choose one of the following possibilities:
--help 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. 
      
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.
The man pages are displayed directly in the shell. To navigate them, move up and down with Page Up and Page Down. Move between the beginning and the end of a document with Home and End. End this viewing mode by pressing Q. Learn more about the man command itself with man man.
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). You can browse an info page with a viewer directly in the shell and display the different sections, called «nodes.» Use Пробел to move forward and <— to move backwards. Within a node, you can also browse with Page Up and Page Down but only Пробел and <— will take you also to the previous or subsequent node. Like for the man pages, press Q to end the viewing mode.
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.
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 | 
|---|---|
| Ctrl+L | Clears the screen and moves the current line to the top of the page. | 
| Ctrl+C | Aborts the command which is currently being executed. | 
| Shift+Page Up | Scrolls upwards. | 
| Shift+Page Down | Scrolls downwards. | 
| Ctrl+U | Deletes from cursor position to start of line. | 
| Ctrl+K | Deletes from cursor position to the end of line. | 
| Ctrl+D | Closes the shell session. | 
| ↑, ↓ | Browses in the history of executed commands. | 
To address a certain file or directory, you must specify the path leading to that directory or file. As you may know from MS DOS or Mac OS already, there are two ways to specify a path:
Enter the entire path from the root directory to the relevant file or directory.
Enter a path to the relevant file or directory by using the current directory as a starting point. This implies to give the levels you have to move up or down in the file system tree to reach the target directory of file, starting from the current directory.
Paths contain filenames, 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]](admon/note.png) | 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 ( | |
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
     ~ (or
     course, this will only work if you have permission to view the
     contents, see Раздел 17.5, «File Access Permissions»). For example,
     entering ls ~tux would list the contents of the home
     directory of a user named
     username 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.
    Suppose you want to copy a file located somewhere in your home directory
    to a subdirectory of /tmp that you need to create
    first.
   
Процедура 17.1. Creating and Changing Directories
     From your home directory create a subdirectory in
     /tmp:
    
Enter
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.
     
To check what happened, now enter
ls -l /tmp
      The new directory test should appear in the list
      of contents of the /tmp directory.
     
Switch to the newly created directory with
cd /tmp/test
Процедура 17.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]](admon/important.png) | 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  | |
To list the contents of your home directory, enter
ls -l ~
      It should contain a subdirectory called Documents
      by default. If not, create this subdirectory with the
      mkdir command you already know:
     
mkdir ~/Documents
      To create a new, empty file named myfile.txt in
      the Documents directory, enter
     
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.
Enter
ls -l ~/Documents
The new file should appear in the list of contents.
To copy the newly created file, enter
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.
     
Check the result by entering
ls -l
      The file myfile.txt  should appear in the list of
      contents for /tmp/test.
     
Процедура 17.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.
    
To rename the file, enter
mv myfile.txt tuxfile.txt
To check what happened, enter
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.
Coming to the conclusion that you do not need the file any longer, you can delete it by entering
rm tuxfile.txt
Bash deletes the file without any confirmation.
Move up one level with cd .. and check with
ls -l test
      if the test directory is empty now.
     
      If yes, you can remove the test directory by
      entering
     
rmdir test
   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.
  
    In order to temporarily become root in a shell, proceed as
    follows:
   
      Enter su. You are prompted for the root
      password.
     
      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.
     
      Execute your task. For example, transfer ownership of a file to a new
      user which only root is allowed to do:
     
chown wilber kde_quick.xml
      After having completed your tasks as root, switch back to your
      normal user account. To do so, enter
     
exit
The hash symbol disappears and you are acting as «normal» user again.
    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:
   
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.
   
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 Глава 9, Списки управления доступом в 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.
 
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.
  
Пример 17.1. Access Permissions For Files and Folders¶
-rw-r----- 1 tux users 0 2006-06-23 16:08 checklist.txt -rw-r--r-- 1 tux users 53279 2006-06-21 13:16 gnome_quick.xml -rw-rw---- 1 tux users 0 2006-06-23 16:08 index.htm -rw-r--r-- 1 tux users 70733 2006-06-21 09:35 kde-start.xml -rw-r--r-- 1 tux users 47896 2006-06-21 09:46 kde_quick.xml drwxr-xr-x 2 tux users 48 2006-06-23 16:09 local -rwxr--r-- 1 tux users 624398 2006-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 | 
| 
         | 
         | 
         | 
         | 
   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
   Пример 17.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.
  
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:
Таблица 17.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  | 
| 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.
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 Раздел 17.5, «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.
  
    The following example shows the output of an ls
    -l command in a shell.
   
Пример 17.2. Access Permissions For Files and Folders¶
-rw-r----- 1 tux users 0 2006-06-23 16:08 checklist.txt -rw-r--r-- 1 tux users 53279 2006-06-21 13:16 gnome_quick.xml -rw-rw---- 1 tux users 0 2006-06-23 16:08 index.htm -rw-r--r-- 1 tux users 70733 2006-06-21 09:35 kde-start.xml -rw-r--r-- 1 tux users 47896 2006-06-21 09:46 kde_quick.xml drwxr-xr-x 2 tux users 48 2006-06-23 16:09 local -r-xr-xr-x 1 tux users 624398 2006-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.
   
Процедура 17.4. Changing Access Permissions
     Suppose you are tux and want to
     modify the access permissions to your files:
    
      If you want to grant the users group also
      write access to kde-start.xml, enter
     
chmod g+w kde-start.xml
      To grant the users group and other users
      write access to kde-start.xml, enter
     
chmod go+w kde-start.xml
To remove write access for all users, enter
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.
     
      To prohibit the usersgroup and others to
      change into the directory local, enter
     
chmod go-x local
      To grant others write permissions for two files, for
      kde_quick.xml and
      gnome_quick.xml, enter
     
chmod o+w kde_quick.xml gnome_quick.xml
Процедура 17.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:
    
      Enter the username and password for root.
     
Enter
chown wilber kde_quick.xml
Check what happened with
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
If the ownership is set according to your wishes, switch back to your normal user account.
As you probably noticed in the examples above, entering commands in Bash can include a lot of typing. In the following, get to know some features of the Bash that can make your work a lot easier and save a lot of typing.
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 Up.
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, just 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 Ctrl+R to start an incremental search function. showing the following prompt:
(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.
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 the help of →|, 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).
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 | 
| 
            | 
           Matches one of the characters from the group specified inside the
           square brackets, which is represented here by the string
            | 
The following examples illustrate how to make use of these convenient features of Bash.
Процедура 17.6. Using History and Completion
If you already did the example Раздел 17.3.1, «Examples for Working with Files and Directories» your shell buffer should be filled with commands which you can retrieve using the history function.
Press ↑ repeatedly until cd ~ appears.
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.
     
Enter cd D and press →|.
Nothing happens since Bash cannot identify to which one of the subdirectories you want to change.
Press →| again to see the list of possible choices:
tux@knox:~> cd D Desktop/ Documents/ tux@knox:~> cd D
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.
You can now execute the command with Enter.
Процедура 17.7. Using Wildcards
     Now suppose that your home directory contains a number of 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.
    
First, create some test files in your home directory:
        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.
        Create at least two files that have the same file extension, for
        example .html.
       
To create several «versions» of one file, enter
touch myfile{1..5}.txtThis command creates five consecutively numbered files:
myfile1.txt,…,myfile5.txt
List the contents of your home directory. It should look similar to this:
-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
       With the help of wild cards, select certain subsets of the files according to various criteria:
        To list all files with the .html extension,
        enter
       
ls -l *.html
        To list all «versions» of
        myfile.txt, enter
       
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).
       
        To remove, for example, version 1-3 and version 5 of
        myfile.txt, enter
       
rm myfile[1-3,5].txt
Check the result with
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]](admon/note.png) | 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. | |
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:
In this mode, vi accepts certain key combinations as commands. Simple tasks such as searching words or deleting a line can be executed.
In this mode, you can write normal text.
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.
| ![[Note]](admon/note.png) | 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. | |
To create and open a new file with vi, enter
vi textfile.txt
By default, vi opens in command mode in which you cannot enter text.
Press I to switch to insert mode. The bottom line changes and indicates that you now can insert text.
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.
In the insert mode, you can edit the text with the arrow keys and with Delete.
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.
      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.
Bash offers you several commands to search for files and to search for the contents of files:
      This utility is only available if you have installed the
      findutils-locate
      package. With this command you can find out in which directory a
      specified file is located. If desired, use wild cards to specify
      filenames. The program is very quick, because it uses a database
      specifically created for the purpose (rather than searching through
      the entire file system). This very fact, however, also results in a
      major drawback: locate is unable to find any files created after the
      latest update of its database. The database can be generated by
      root running
      updatedb.
     
      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.
     
      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.
     
     The KDE and GNOME desktops store user-specific application data in
     hidden directories, for example .kde and
     .gnome.
    
To locate these directories on your computer, enter
locate .kde
if you have installed KDE desktop or
locate .gnome
if you have installed GNOME desktop.
      You will see that locate displays all file names in
      the database that contain the string .kde or
      .gnome anywhere. To learn how to modify this
      behavior refer to the man page of locate.
     
      To search your home directory for all occurrences of filenames that
      contain the file extension .txt, use
     
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, enter
     
grep music ~/*
      Note that grep is case-sensitive— unless you use it with the
      -i option. With the command above you will not find
      any files containing Music.
     
If you want to use a search string which consists of more than one word, enclose the string in double quotation marks, for example:
grep "music is great" ~/*
   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.
  
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.
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.
With less, display the whole contents of a text file. To move up and down half a page use Page Up and Page Down. Use Пробел 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.
      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 backwards. Use Пробел to move
      forward. When you reach the end of the document, the viewer closes
      automatically.
     
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.
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 the help of certain symbols you can redirect the input or the output to another object, such as a file or another command.
      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).
     
      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.
     
To write the output of a command like ls to a file, enter
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
     
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.
     
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:
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:
     
sort < filelist.txt > sorted_filelist.txt
      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
     
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
     
ls -l | grep tux
   As you have seen in Раздел 17.7, «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 Ctrl+Z 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:
  
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@linux:~> 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@linux:~> 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
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
   (%):
  
kill %job_numberIf kill does not help—as is sometimes the case for «runaway» programs—try
kill -9 PIDThis sends a KILL signal instead of a TERM signal, bringing the specified process to an end in most cases.
This section is intended to introduce the most basic set of commands for handling jobs and processes. Find an overview for system administrators in Section “Processes” (Chapter 2, System Monitoring Utilities, ↑System Analysis and Tuning Guide).
This section gives insight into the most important 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. To learn more about the various commands, use the manual pages, 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, move up and down with Page Up and Page Down. Move between the beginning and the end of a document with Home and End. End this viewing mode by pressing Q. Learn more about the man command itself with man man.
  In the following overview, the individual command elements are written in
  different typefaces. The actual command and its mandatory options are
  always printed as command option. Specifications or
  parameters that are not required are placed in [square
  brackets].
 
  Adjust the settings 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.
 
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.
[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
[options] source target
     
       Copies source to target.
      
          Waits for confirmation, if necessary, before an existing
          target is overwritten
         
Copies recursively (includes subdirectories)
[options] source target
     
       Copies source to target
       then deletes the original source.
      
          Creates a backup copy of the source before
          moving
         
          Waits for confirmation, if necessary, before an existing
          targetfile is overwritten
         
[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
[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.
      
Creates a symbolic link
[options] [directory]
     Changes the current directory. cd without any parameters changes to the user's home directory.
[options] directory
     Creates a new directory.
[options] directory
     Deletes the specified directory if it is already empty.
[options] username[:[group]] files
     Transfers ownership of a file to the user with the specified username.
-R
        Changes files and directories in all subdirectories
[options] groupname 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.
      
[options] mode files
     Changes the access permissions.
       The mode parameter has three parts:
       group, access, and
       access type. group accepts the
       following characters:
      
User
Group
Others
       For access, grant access with +
       and deny it with -.
      
       The access type is controlled by the following
       options:
      
Read
Write
Execute—executing files or changing to the directory
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.
[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.
      
Decompresses the packed gzip files so they return to their original size and can be processed normally (like the command gunzip)
options archive files
     tar puts one or more files into an archive. Compression is optional. tar is a quite complex command with a number of 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.
       
      
patterns
     
       This command is only available if you have installed the
       findutils-locate package.
       The locate command can find in which directory a
       specified file is located. If desired, use wild cards  to specify filenames. The program is very fast, because
       it uses a database specifically created for the purpose (rather than
       searching through the entire file system). This very fact, however,
       also results in a major drawback: locate is unable to find any files
       created after the latest update of its database. The database can be
       generated by root with
       updatedb.
      
[options]
     
       This command performs an update of the database used by
       locate. To include files in all existing
       directories, run the program as
       root. It also makes sense
       to place it in the background by appending an ampersand
       (&), so you can immediately continue
       working on the same command line (updatedb &).
       This command usually runs as a daily cron job (see
       cron.daily).
      
[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.
[options] files
     The cat command displays the contents of a file, printing the entire contents to the screen without interruption.
Numbers the output on the left margin
[options] files
     This command can be used to browse the contents of the specified file. Scroll half a screen page up or down with PgUp and PgDn or a full screen page down with Space. Jump to the beginning or end of a file using Home and End. Press Q to exit the program.
[options] searchstring 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 searchstring was found along with the
       filename.
      
-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
         
[options] file1 file2
     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
[options] [device] mountpoint
     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 filesystem
        
          Specify the file system, commonly ext2 for
          Linux hard disks, msdos for MS-DOS media,
          vfat for the Windows file system, and
          iso9660 for CDs
         
       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.
      
[options] mountpoint
     
       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.
      
The following section lists a few of the most important commands needed for retrieving system information and controlling processes and the network.
[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.)
[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
[options]
     The command free displays information about RAM and swap space usage, showing the total and the used amount in both categories. See Раздел “The free Command” (Глава 7, Special System Features, ↑Содержание) for more information.
-b
        Output in bytes
-k
        Output in kilobytes
-m
        Output in megabytes
[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.
      
[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.
[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.
Displays a detailed list of all processes, independent of the owner
[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:
Sends a KILL signal instead of a TERM signal, bringing the specified process to an end in almost all cases
[options] processname
     This command is similar to kill, but uses the process name (instead of the process ID) as an argument, killing all processes with that name.
[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 basically 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)
[options] hostname [server]
     The domain name system resolves domain names to IP addresses. With this tool, send queries to name servers (DNS servers).
[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.
[options] [username]
     
       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.
      
[options] [username]
     
       The su command makes it possible to log in under a
       different username from a running session. Specify a username 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 username, you are prompted for the
       root password and change to
       the superuser (root). Use
       su - to start a login shell for a different user.
      
[options]
     To avoid loss of data, you should always use this program to shut down your system.
[options]
     Does the same as halt except the system performs an immediate reboot.
This command cleans up the visible area of the console. It has no options.
There are many more commands than listed in this chapter. For information about other commands or more detailed information, the O'Reilly publication Linux in a Nutshell is recommended.