2.1. Navigating the File System¶
In the previous tutorial, you experienced the pwd
(print working directory)
command and were provided a brief introduction to absolute paths. In Unix, a
directory file or directory is the same concept as a folder in other
operating systems; that is, in most cases the words “directory” and “folder”
are interchangeable. Your current working directory is the directory in which you
are currently executing commands. There are other directories, and in Unix,
they are all organized under a top-most directory called /
, representing the
root of the file system. The macOS and Linux operating systems organize their
file systems the same way. If you are coming from a Windows background, then it
is reasonable to compare /
to the C:
drive.
For this part of the tutorial, we are going to assume that you have some specific files in your home directory. To make sure that you have these files, you should execute the command below. The command may take a few seconds to run as it involves downloading some files from the Internet. You don’t need to memorize or understand the command below; you just need to execute it to proceed with the tutorial.
curl -s -L https://git.io/fjbdg | bash
2.1.1. ls
, ls PATH
, tree PATH
¶
To list the files in your current working directory, use the ls
(list)
command. It is common for ls
to differentiate between directories
and regular files in its output using different colors.
ls
How many directories and regular files are in your home directory?
The ls
(list) command is not limited to the current working directory.
You can tell ls
to list the files in some other directory by supplying a
path to that directory as a command-line argument.
ls cs1302-unix
The paths used in the examples above are relative paths. Instead of describing the traversal path starting from
/
, they describe the traversal path starting from the current working directory. Thels
command also works with absolute paths. What is the command to list the files in your home directory using an absolute path?
While ls
is nice and probably one of the most used Unix commands of all time,
it doesn’t let us see the whole picture without us issuing repetitive
ls
commands. To see the bigger picture, use the tree
command, which
lists the contents of a directory in a tree-like format. If this command doesn’t work for you,
please go back and make sure you have completed the previous tutorial and all the required setup
steps.
tree cs1302-unix
Test Yourself
Which files are contained in the
cs1302
directory?What about the
books
directory?
Command |
Description |
---|---|
|
List contents of the current working directory. |
|
List contents of the last directory in the provided |
|
List contents of the last directory in the provided |
2.1.2. ls -l
, ls -lh
¶
By default, the ls
command displays the contents of a directory using its “simple”
output format. A “long” output option can be specified by supplying a -l
as a
command-line argument. In addition to the name of each file, additional information
is included in the “long” output format, as described below in the next few examples.
ls -l cs1302-unix/books
Mode |
User |
Group |
Size |
Timestamp |
Name |
|
---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ls -l cs1302-unix
Mode |
User |
Group |
Size |
Timestamp |
Name |
|
---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
You don’t need to understand most of the columns in a long listing
produced by ls -l
at this point in time. The takeaway is that
adding a command-line argument like -l
changes the behavior of
ls
without needing to ask the user for any additional input.
You can supply -h
as a command-line argument to ls -l
to
enable the display of human-readable units for file sizes. Many Unix
programs like ls
allow you to combine single character (short)
options into a single command-line argument when entering the command
at the shell prompt. All three examples below produce the same output.
ls -l -h cs1302-unix/books
ls -h -l cs1302-unix/books
ls -lh cs1302-unix/books
Test Yourself
What command can be used to see the sizes of the files in a directory?
What is second way to accomplish the same task using the same options?
Command |
Description |
---|---|
|
|
|
|
2.1.3. cd PATH
, cd -
, cd ~
, cd
¶
So far, you know how to list the files in your home directory, your current working directory,
and other directories; however, all of the examples so far have utilized absolute or relative
paths in a way that kept you in your home directory. It’s time for that to change, or rather
it’s time for your current working directory to change. This can be done using the cd
(change directory) command.
As you navigate to various directories in this section, try typing pwd
and ls
in each
directory. Note how the output of each command changes as you change directories. That’s because
commands are always executed in the context of the current directory.
cd cs1302-unix
cd notes/cs1302
If you to change to the directory you were last in prior to your current working directory,
then supply -
(minus sign) as a command-line argument to cd
.
cd -
On most Unix systems, ~
(tilde) is an alias for the absolute path of your home directory.
While you can use it with cd
to change directly to your home directory, it’s more commonly
used to change to directories nested under your home directory.
cd ~/cs1302-unix
If you use the cd
command without supplying any command-line arguments, then it changes
to your home directory. How convenient!
cd
Test Yourself
What two commands (or variations of the same command) can always be used to take you to your home directory?
How can you tell if a path is absolute or relative?
How can you convert a relative path to an absolute path (and vise-versa)?
Command |
Change to |
---|---|
|
home directory |
|
last previous working directory |
|
|
|
|
|
|
2.1.4. ls -a
, cd ..
¶
The default behavior of ls
is to ignore files starting with .
(hidden files).
To force ls
to not ignore hidden files, supply the -a
(all) option
as a command-line argument.
ls -a
Every directory on a Unix system has two special hidden files
named .
and ..
.
File |
Description |
---|---|
|
path alias for the directory itself |
|
path alias for parent of the directory (parent directory) |
Remember, the default behavior of ls
is to list the contents of
the current working directory. If we supply .
as a path to ls
,
then it does the same thing.
ls .
You can supply ..
as a path for ls
to list the contents
of the current working directory’s parent (i.e., its parent directory).
ls ..
You can supply ..
as a path to cd
to change to go up one
directory; that is, change to the parent directory.
cd ..
You can even go up two directories using ../..
.
cd ../..
Command |
Description |
---|---|
|
|
|
|
|
|
|
|
|
|
Test Yourself
What is the difference between
.
and..
?How can you leverage
..
to go up a directory tree by three directories (i.e., instead of up by one or up by two) using a single command?