Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

OpenSuSE 11.1 Quick Start Guide
Previous Page Home Next Page

8.6 Useful Features of the Shell

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.

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 PgUp.

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.

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 Tab (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 is getting ambiguous again. You can then obtain a list of them by pressing Tab a second time. After this, you can enter the next letters of the file or path then try completion again by pressing Tab. When completing filenames and paths with the help of Tab, 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.

8.6.1 Examples For Using History, Completion and Wildcards

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

If you already did the example Section 8.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.

  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. Enter cd D and press Tab.

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

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

    tux@knox:~> cd D Desktop/   Documents/ tux@knox:~> cd D
  5. The prompt still shows your initial input. Type the next character of the subdirectory you want to go to and press Tab again.

    Bash now completes the path.

  6. You can now execute the command with Enter.

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.

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

    1. Use the touch command you already know 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 blank.

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

    3. To create several versions of one file, enter

      touch myfile{1..5}.txt

      This command creates five consecutively numbered files:

      myfile1.txt,…,myfile5.txt
    4. 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
             
  2. With the help of wild cards, select certain subsets of the files according to various criteria:

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

       ls -l *.html
    2. 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 also had a file named myfile10.txt you would have 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

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

      ls -l

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

Of course, 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: 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.

OpenSuSE 11.1 Quick Start Guide
Previous Page Home Next Page

 
 
  Published under the terms fo the GNU General Public License Design by Interspire