Navigating Files and Directories
Overview
Teaching: 30 min
Exercises: 20 minQuestions
How can I perform operations on files outside of my working directory?
What are some navigational shortcuts I can use to make my work more efficient?
Objectives
Use a single command to navigate multiple steps in your directory structure, including moving backwards (one level up).
Perform operations on files in directories outside your working directory.
Work with hidden directories and hidden files.
Interconvert between absolute and relative paths.
Employ navigational shortcuts to move around your file system.
Moving around the file system
We’ve learned how to use pwd
to find our current location within our file system.
We’ve also learned how to use cd
to change locations and ls
to list the contents
of a directory. Now we’re going to learn some additional commands for moving around
within our file system.
Use the commands we’ve learned so far to navigate to the shell_data/untrimmed_fastq
directory, if
you’re not already there.
$ cd
$ cd shell_data
$ cd untrimmed_fastq
Notice that your prompt changes each time you “cd” into another directory. This is very helpfu for keeping track of where you are.
What if we want to move back up and out of this directory and to our top level
directory? Can we type cd shell_data
? Try it and see what happens.
$ cd shell_data
-bash: cd: shell_data: No such file or directory
Your computer looked for a directory or file called shell_data
within the
directory you were already in. It didn’t know you wanted to look at a directory level
above the one you were located in.
We have a special command to tell the computer to move us back or up one directory level.
$ cd ..
Now we can use pwd
to make sure that we are in the directory we intended to navigate
to, and ls
to check that the contents of the directory are correct.
$ pwd
/home/user/shell_data
$ ls
sra_metadata untrimmed_fastq
From this output, we can see that ..
did indeed take us back one level in our file system.
You can chain these together like so:
$ ls ../../
prints the contents of /home
.
Finding hidden directories
First navigate to the
shell_data
directory. There is a hidden directory within this directory. Explore the options forls
to find out how to see hidden directories. List the contents of the directory and identify the name of the text file in that directory.Hint: hidden files and folders in Unix start with
.
, for example.my_hidden_directory
Solution
First use the
man
command to look at the options forls
.$ man ls
The
--all
option causesls
to “not ignore entries starting with .” This is the option we want. The short form is-a
.$ ls --all
. .. .hidden sra_metadata untrimmed_fastq
The name of the hidden directory is
.hidden
. We can navigate to that directory usingcd
.$ cd .hidden
And then list the contents of the directory using
ls
.$ ls
youfoundit.txt
The name of the text file is
youfoundit.txt
.The
ls
command comes with many other useful options, such as sorting by size or modification time. Take a moment to peruse some of these options. Don’t worry about remembering the flags, just make a mental note of what is possible, in case you need it in the future.
Examining the contents of other directories
By default, the ls
commands lists the contents of the working
directory (i.e. the directory you are in). You can always find the
directory you are in using the pwd
command. However, you can also
give ls
the names of other directories to view. Navigate to your
home directory if you are not already there.
$ cd
Then enter the command:
$ ls shell_data
sra_metadata untrimmed_fastq
This will list the contents of the shell_data
directory without
you needing to navigate there.
The cd
command works in a similar way.
Try entering:
$ cd
$ cd shell_data/untrimmed_fastq
This will take you to the untrimmed_fastq
directory without having to go through
the intermediate directory.
Navigating practice
Navigate to your home directory. From there, list the contents of the
untrimmed_fastq
directory.Solution
$ cd $ ls shell_data/untrimmed_fastq/
SRR097977.fastq SRR098026.fastq
Full vs. Relative Paths
The cd
command takes an argument which is a directory
name. Directories can be specified using either a relative path or a
full absolute path. The directories on the computer are arranged into a
hierarchy. The full path tells you where a directory is in that
hierarchy. Navigate to the home directory, then enter the pwd
command.
$ cd
$ pwd
You will see:
/home/user
This is the full name of your home directory. This tells you that you
are in a directory called user
(where user
is your Garvan ID), which sits inside a directory called
home
which sits inside the very top directory in the hierarchy. The
very top of the hierarchy is a directory called /
which is usually
referred to as the root directory. So, to summarize: user
is a
directory in home
which is a directory in /
. More on root
and
home
in the next section.
Now enter the following command (again, substitute user
for your Garvan ID):
$ cd /home/user/shell_data/.hidden
This jumps forward multiple levels to the .hidden
directory.
Now go back to the home directory.
$ cd
You can also navigate to the .hidden
directory using:
$ cd shell_data/.hidden
These two commands have the same effect, they both take us to the .hidden
directory.
The first uses the absolute path, giving the full address from the home directory. The
second uses a relative path, giving only the address from the working directory. A full
path always starts with a /
. A relative path does not.
Take a moment to let that sink in. Two of the most common problems with specifying file paths
are 1) forgetting to start an absolute path with /
and 2) starting a path with /
when you
actually wanted to specify a relative path. The best way to avoid these errors is to rely
on tab completion every time you enter a path. It’s quicker, you’ll avoid typos AND you will
avoid confusion between absolute and relative paths (because tab completion won’t work if
you use /
incorrectly, reminding you to double-check).
A relative path is like getting directions from someone on the street. They tell you to “go right at the stop sign, and then turn left on Main Street”. That works great if you’re standing there together, but not so well if you’re trying to tell someone how to get there from another country. A full path is like GPS coordinates. It tells you exactly where something is no matter where you are right now.
You can usually use either a full path or a relative path depending on what is most convenient. If we are in the home directory, it is more convenient to enter the full path. If we are in the working directory, it is more convenient to enter the relative path since it involves less typing.
Over time, it will become easier for you to keep a mental note of the structure of the directories that you are using and how to quickly navigate amongst them.
Relative path resolution
Using the filesystem diagram below, if
pwd
displays/Users/thing
, what willls ../backup
display?
../backup: No such file or directory
2012-12-01 2013-01-08 2013-01-27
2012-12-01/ 2013-01-08/ 2013-01-27/
original pnas_final pnas_sub
Solution
- No: there is a directory
backup
in/Users
.- No: this is the content of
Users/thing/backup
, but with..
we asked for one level further up.- No: see previous explanation. Also, we did not specify
-F
to display/
at the end of the directory names.- Yes:
../backup
refers to/Users/backup
.
Navigational Shortcuts
The root directory is the highest level directory in your file
system and contains files that are important for your computer
to perform its daily work. While you will be using the root (/
)
at the beginning of your absolute paths, it is important that you
avoid working with data in these higher-level directories, as
your commands can permanently alter files that the operating
system needs to function. In many cases, trying to run commands
in root
directories will require special permissions which are
not discussed here, so it’s best to avoid them and work within your
home directory. Dealing with the home
directory is very common.
The tilde character, ~
, is a shortcut for your home directory.
In our case, the root
directory is two levels above our
home
directory, so cd
or cd ~
will take you to
/home/user
and cd /
will take you to /
. Navigate to the
shell_data
directory:
$ cd
$ cd shell_data
Then enter the command:
$ ls ~
bio course data r_data shell_data
This prints the contents of your home directory, without you needing to type the full path.
The commands cd
, and cd ~
are very useful for quickly navigating back to your home directory. We will be using the ~
character in later lessons to specify our home directory.
Tree view
There is a very useful utility available on Wolfpack called tree
that uses ASCII art to display the structure of the current directory
as well as its subdirectory. Try the following:
$ cd ~
$ tree -L 3
Caution: By default, tree
displays all of the directories below the current directory, as well as all the subdirectories of those directories and
all their subdirectories, and so on all the way down to the lowest level. Depending on where you are in the filesystem, that could produce more output
than is really helpful. It is usually a good idea to use the -L
flag to only display a few “levels”.
Note: By default, tree
doesn’t follow symbolic links.
We’ve made quite liberal use of symbolic links for this workshop by placing the real data files on /share/ScratchGeneral/ and linking to these files from your home directory.
You can instruct tree
to follow symbolic links using the -l
flag (that’s lowercase “l”).
$ cd ~/course
$ tree -l -L 3
sshfs
All the command line has many advantages, sometimes it is easier to browse the filesystem using familiar graphical interfaces. sshfs
(Mac) and sshfs-win
(Windows) are tools that allow you to “mount” the volumes on the cluster with the filesystem on your laptop so that you browse files and directories in
much the same way as you would “mount” a USB stick or external drive. Using Finder
or File Explorer
to visually check the results of your operations
can be helpful as you work through this course. See this Confluence page for details.
Key Points
The
/
,~
, and..
characters represent important navigational shortcuts.Hidden files and directories start with
.
and can be viewed usingls --all
.Relative paths specify a location starting from the current location, while absolute paths specify a location from the root of the file system.