Learn Bash - Pathnames, Wildcards, Variables

Question Click to View Answer

What directory does the following code switch to?

$ cd /

cd / switches to the root directory. The root directory is the topmost directory of the Unix file system.

Switch to the /bin directory.

cd /bin

/bin is an example of an absolute pathname. Absolute pathnames are specified relative to the root directory, which is represented with the backslash character /.

Switch to the Desktop directory and make the folder hey_hey.

cd ~/Desktop
mkdir hey_hey

~ is shorthand for the home directory. The Desktop directory is a subdirectory of the home directory.

Change from the ~/Desktop/hey_hey directory back to the ~/Desktop directory.

cd ../

../ represents the parent directory of the present working directory (pwd). Relative paths are specified relative to the present working directory. A root relative path (cd ~/Desktop) would also work, but relative paths are often more convenient.

Identify the program and arguments of the following command line:

$ ls /bin
ls is a program

/bin is an absolute path that is used as an argument to the ls program in this example

Run the following commands. Then list all the files in the i_care directory with the .txt extension.

cd ~/Desktop
mkdir i_care
touch i_care/care_bear.txt i_care/woo_hoo i_care/foofie.txt
ls ~/Desktop/i_care/*.txt

The ls command lists care_bear.txt and foofie.txt based on wildcard matching. The * matches any characters and the .txt checks for files with the text extension.

Delete the ~/Desktop/hey_hey and the ~/Desktop/i_care directories.

rm -rf ~/Desktop/hey_hey ~/Desktop/i_care

The rm program is supplied options (-rf) that are necessary to delete folders. Two folders are passed to the rm command as arguments. Most Unix commands are built to take one or multiple arguments.

Explain what the following command prints to the shell.

$ echo $HOME

$HOME is an environment variable that is assigned to the absolute pathname of the home directory. Previously, we've used ~ to navigate to the $HOME directory, but both ~ and $HOME reference the absolute pathname of the home directory. cd $HOME/Desktop and cd ~/Desktop can both be used to navigate to the Desktop directory.

Explain what the following command prints to the shell. What is the significance of this output?

$ echo $PATH

The $PATH environment variable stores a list of directories, separated by colons. When a shell program is run, the shell looks for the program in these directories. All the programs we've been using thus far (cd, cat, touch, ls) are all defined in one of the directories listed in the $PATH environment variable.

What file is the ls program defined in?

which ls

The which command returns the location of executable files. On my machine, the ls file is located in /bin/ls. /bin/ is the directory and ls is the executable file.

What file is the top program defined in?

which top

The top executable file is located in /usr/bin/top on my computer. /usr/bin/ is the directory and top is the executable file. /usr/bin is one of the directories that is listed in my $PATH environment variable, so the top program can be located by the shell when it's called.

Create the file $HOME/Desktop/moo. Then delete it.

touch $HOME/Desktop/moo
rm $HOME/Desktop/moo

moo is a file without a file extension (it's not moo.txt or moo.pdf). A lot of Unix files don't have file extensions.

The command $ ls -l provides a long listing of the files in a directory. Alias longls so it can be used in place of $ ls -l.

alias longls='ls -l'
# now the following command line works
longls # same as ls -l

Show the last 500 commands you've entered in the shell.

history

Bash keeps track of your commands so they can be tracked and re-executed.

Is $HOME/Desktop/ an absolute or relative path? Explain.

$HOME/Desktop/ is an absolute path because it is specified relative to the root directory.

Is /bin/ an absolute or relative path? Explain.

/bin/ is an absolute path because it's specified relative to the / (root) directory.

Is ../../Desktop/blah/ an absolute or relative path. Explain.

../../Desktop/blah/ is a relative path because it's specified relative to the current working directory.

What's the difference between absolute and relative paths?

Absolute paths are specified relative to the root directory. Relative paths are specified relative to the current working directory.