LINUX Tutorial Four

4.1 Wildcards and globbing

The characters * and ?

Before beginning this section, cd to your linuxstuff directory (if not already there) and copy the file list1 to alist and list2tolist2.txt. You should now have the files alist,list1, list2,list2.txt.

LINUX shells will carry out filename expansion, based on wildcard characters, in a process known as globbing.

The character * is called a wildcard, and will match against none or more character(s) of any type in a file (or directory) name. For example, in your linuxstuff directory, type

# ls list*

This will list all files in the current directory starting with list.

Try typing

# ls *list

This will list all files in the current directory ending with list. However, the filename expansion will not include hidden files beginning with a dot, such as .bash_profile. In order to list all hidden files, you can use

# ls -d .*

In the above, the -d option specifies that we do not want to recursively search directories for matching files.

The character ? will match exactly one character.
So ls ?ouse will match files like house and mouse, but not grouse.
Try typing

# ls ?list

When globbing is used, the filename is expanded and the command executed using this expansion, which may contain multiple files. To see the expansion itself, you can type

# echo list*

Suppose that this returns list1 list2. Then the command ls list* executes the command ls list1 list2. Similarly, the command grep can be used to search multiple files. For example, to search all (non-hidden) files in a directory for the word Linux, type

# grep Linux *

Matching a range of characters

Square brackets are used to indicate that a match to one of the specified characters is desired. For example,try typing

# ls -d [al]*

This will list all files in the current directory begining with either an 'a' or an 'l'.

Within brackets, a hyphen can be used to denote a range. For example, to list all files beginning with any letter between (and including) a to k, you can type

ls -d [a-k]*

Exercise 4a

Write commands for the following:

  1. list all files that end in .txt
  2. list all files that end in a number (i.e., any digit between 0 and 9)
  3. list all files that contain a number

Globbing vs. Regular Expressions

Be aware that there is a related 'pattern matching' grammar known as regular expressions (regex), which are used by utilities such as grep and in scripting languages such as python. There are important differences between globbing and regular expressions. For example, the character * in a regular expression means "zero or more of the preceeding character". For example, the command

# ls h*t

will list all files that start with h and end in t, such as hat, hot, halt, and hoist. However, the command

# grep h*t file.txt

will match any lines containing one or more 'h's followed by a t, such as ht, ahht, ZZZhhhhht123, etc. For more on reglar expressions, click here for a grep cheat sheet.

4.2 Filename conventions

We should note here that a directory is merely a special type of file. So the rules and conventions for naming files apply also to directories.

In naming files, characters with special meanings such as / & & # , should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with _ (underscore) and . (dot).

File names conventionally start with a lower-case letter, and may end with a dot followed by a group of letters indicating the contents of the file. For example, all files consisting of python code may be named with the ending .py, for example, prog1.py . Then in order to list all files containing python code in your home directory, you need only type ls *.py in that directory.

Exercise 4b

What command can you use to search all python code files (ending in .py) and printing the line and line number for each line containing the word sum?

4.3 Getting Help

On-line Manuals

There are on-line manuals which gives information about most commands. These built-in manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command.

For example, to find out more about the wc (word count) command, type

# man wc

You will get an error! In the docker container we are using, the man command and manual pages are not installed. In order to use the man command we need to do the following:

  1. Modify the yum configuration file /etc/yum.conf by adding a number sign (#) to the beginning of the line tsflags=nodocs. This change comments out the line which tells the program installer yum not to install manual pages.
  2. Run the command yum install man-pages man-db. Type y when prompted to continue the installation.
  3. Once installed, retype man wc to see the man page for the wc command. Note that you will need to type q to exit the manual.

The correct usage of a command is described under Synopsis, where options or arguments in brackets are optional.

For example, the usage for wc is wc [-clmw] [file ...], with optional flags for outputting the number of bytes in each file, number of lines, characters, and words. The ellipses (...) indicate that one or more files can be specified. If no files are specified, then standard input is used. For example, to display the number of words and lines in the files list1 and list2, type

# wc -wl list1 list2

An alternate way of getting help is given by the following example,

# whatis wc

which gives a one-line description of the command, but omits any information about options etc. Note that in our docker container, we need to run the command mandb before using this command for the first time

Exercise 4c

Look at the online manual for ls, and note that there are many options. However, we can search for keywords in the same way that vi allows searches, e.g., type /string to search for a string

  1. Sort the files list1 and list2 by "time modified".

Apropos

When you are not sure of the exact name of a command,

# apropos keyword

will give you the commands with keyword in their manual page header. For example, try typing

# apropos copy

History

The shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered. The following command prints a numbered list of recent commands:

# history

In most shells, incuding bash (the shell we are using), you can use the exclamation character (!) to recall commands easily.

# !! (recall last command)

# !-3 (recall third most recent command)

# !5 (recall the 5th command in list)

# !grep (recall last command starting with grep)

You can increase the size of the history buffer by typing

# set history=100

Summary

* match any number of characters
? match one character
[] match a range of characters
man command read the online manual page for a command
whatis command brief description of a command
apropos keyword match commands with keyword in their man pages
history lists the recent history of the shell

 

Last Modified: January 2019