It may not always be obvious where you are in the Unix file structure.
We can use pwd
(print working directory) to return the full path to your current working directory.
‘pwd’ is our first example of a command.
Referring back to our analogy of a human-computer conversation, the $
prompt indicates that its your turn to talk.
In the following examples everything after the $
prompt is user input.
[user@host ~]$ pwd
/home/username
[user@host ~]$
In our output note that:
~
- ‘tilde’ is shorthand for your home directory/
- the root directoryNote: Windows uses backslashes \
in paths, everything else uses forward slashes. Also, ‘username’ will depend on your own username for the system being used.
The Unix file structure resembles an inverted tree, spreading out from the root /
.
We have some files and directories to explore. These should have been downloaded either from Moodle or during Set-Up.
If you are using a local Bash installation you will need to navigate to your Documents directory:
[user@host ~]$ cd ~/Documents/
We can use ls
to list the contents of the current directory. The output of this command will vary dependent on the files you have stored on your device or in the Unix Filestore.
[user@host ~]$ ls
Scratch shell-training
In our examples we are using the UCL Research Computing Platform Legion.
In addition to the directory shell-training, we also have the directory Scratch.
We can also find out what is in a specified directory:
[user@host ~]$ ls shell-training
animals data docs scripts
Here we have given ls
the name of a directory as an argument to list the contents of that directory.
[user@host ~]$ ls -a
. .bash_logout
.. .bashrc shell-training
.bash_history .emacs .ssh
We have changed the default behaviour of ls
with a switch, also known as a flag or option, to
show the hidden files and directories. Hidden files and directories start with “.”.
There are two hidden files of note:
.
- Present working directory (in this case ~)..
- Parent directory to the current directory (in this case /home)Always leave a space to separate commands, switches and arguments.
We can type man ls
for a list of options you can use with ls (q to quit).
To navigate our directory structure we use the command cd
.
[user@host ~]$ pwd
/home/username
[user@host ~]$ cd shell-training
[user@host shell-training]$ pwd
/home/username/shell-training
[user@host shell-training]$ cd ..
[user@host ~]$ cd /
[user@host /]$
We type ‘cd’ followed by the path to a directory to change into that directory.
Using:
/
- takes you to the “root” directoryWe can also use cd ~
to return to our ‘home’ directory.
There are many ways of writing the path to a directory.
Absolute path | Relative path |
/home/alice | ../home/alice (from "tmp") |
Thinking about our shell-training directory:
Every directory has a unique absolute path:
/home/username/shell-training/
Relative paths depend on your current location:
shell-training/
This relative path begins with the home alias and works from anywhere:
~/shell-training/
This path works too:
cd /bin/../tmp/../home/username/shell-training
Paths that start with the home alias ~
are relative paths as they do not start from the root, but it provides a useful reference point for relating files in your home directory to one another.
Note that home directories are unique to each user so relative paths using the home alias will not behave consistently for everyone.
cd ~
or cd
cd sh
and then press the tab key?Next activity: Exercises - Navigating Files and Directories.