Unix: Wildcards

The directories . and ..

Make sure you're still in the exercises directory, and type:

$ ls -a 

As you can see, in the exercises directory (and in all other directories), there are two special directories listed as (.) and (..).

The current directory (.)

In Unix, dot (.) means the current directory, so typing the following (make sure you type a space between cd and the dot):

$ cd . 

just means stay where you are (i.e. the exercises directory).

The parent directory (..)

The parent of the current directory is known as dot dor (..). In other words, to move one directory up the hierarchy (back to your home directory if you're located in your exercises directory), type:

$ cd .. 

More about home directories and path-names

Understanding path-names

First type cd to get back to your home-directory, then type:

$ ls exercises

to list the contents of your exercises directory.

Now type:

$ ls repository

You will get an error message like this:

repository: No such file or directory

The reason for the error message is that repository is not inside your current working directory. To use a command on a file (or directory) not in the current working directory, you must either cd to the correct directory, or specify its full or relative path-name. To list the contents of your repository directory, you must type:

$ ls exercises/repository

Your home directory (~)

Home directories can also be referred to by the (~) character tilde. It can be used to specify paths starting at your home directory. So typing;

$ ls ~/exercises

will list the contents of your exercises directory, no matter where you currently are in the file system.

Exercise

Explain what the following two commands would list:

$ ls ~
…
$ ls ~/.. 

Wildcards

The * wildcard

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

$ ls dog*.txt

This will list all text files in the current directory starting with “dog”.

Also try the following:

$ ls *dogs.txt

This will list all text files in the current directory that have a name ending with “dogs.txt”.

The ? wildcard

The character ? will match exactly one character. So “m?ster” will match files like “mister” and “muster”, but not “monster”.

Try the following in your exercises directory:

$ ls dog?.txt

Summary

Wildcard Meaning
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
* match any number of characters
? match one character