Navigating the filesystem
OverviewTeaching: 20 min
Exercises: 10 minQuestions
How do you move around the filesystem in the shell?Objectives
Use shell commands to work with directories and files
Use shell commands to find and manipulate data
Navigating the shell
We will begin with the basics of navigating the Unix shell.
Let’s start by opening the shell. This likely results in seeing a black window with a cursor flashing next to a dollar sign.
This is our command line, and the
$ is the command prompt to show that the system is ready for our input.
The appearance of the prompt will vary from system to system, depending on how the set up has been configured,
but it usually ends with a
When working in the shell, you are always somewhere in the computer’s
file system, in some folder (directory). We will therefore start by finding out
where we are by using the
pwd command, which you can use whenever you are unsure
about where you are. It stands for “print working directory” and the result of the
command is printed to your standard output, which is the screen.
pwd and hit enter to execute the command
(Note that the
$ sign is used to indicate a command to be typed on the command prompt,
but we never type the
$ sign itself, just what follows after it.):
The output will be a path to your home directory. Let’s check if we recognise it
by listing the contents of the directory. To do that, we use the
Applications Documents Library Music Public Desktop Downloads Movies Pictures
We may want more information than just a list of files and directories.
We can get this by specifying various flags (also known as
parameters, or, most frequently,
arguments) to go with our basic commands.
Arguments modify the workings of the command by telling the computer what sort of output or manipulation we want.
If we type
ls -l and hit enter, the computer returns a list of files that contains
information similar to what we would find in our Finder (Mac) or Explorer (Windows):
the size of the files in bytes, the date it was created or last modified, and the file name.
$ ls -l
total 0 drwx------+ 6 user user 204 Jul 16 11:50 Desktop drwx------+ 3 user user 102 Jul 16 11:30 Documents drwx------+ 3 user user 102 Jul 16 11:30 Downloads drwx------@ 46 user user 1564 Jul 16 11:38 Library drwx------+ 3 user user 102 Jul 16 11:30 Movies drwx------+ 3 user user 102 Jul 16 11:30 Music drwx------+ 3 user user 102 Jul 16 11:30 Pictures drwxr-xr-x+ 5 user user 170 Jul 16 11:30 Public
In everyday usage we are more used to units of measurement like kilobytes, megabytes, and gigabytes.
Luckily, there’s another flag
-h that when used with the -l option, use unit suffixes:
Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the
number of digits to three or less using base 2 for sizes.
ls -h won’t work on its own. When we want to combine two flags,
we can just run them together. So, by typing
ls -lh and hitting
enter we receive an output in a human-readable format (note: the order here doesn’t matter).
$ ls -lh
total 0 drwx------+ 6 user user 204B Jul 16 11:50 Desktop drwx------+ 3 user user 102B Jul 16 11:30 Documents drwx------+ 3 user user 102B Jul 16 11:30 Downloads drwx------@ 46 user user 1.5K Jul 16 11:38 Library drwx------+ 3 user user 102B Jul 16 11:30 Movies drwx------+ 3 user user 102B Jul 16 11:30 Music drwx------+ 3 user user 102B Jul 16 11:30 Pictures drwxr-xr-x+ 5 user user 170B Jul 16 11:30 Public
We’ve now spent a great deal of time in our home directory.
Let’s go somewhere else. We can do that through the
cd or Change Directory command:
(Note: On Windows and Mac, by default, the case of the file/directory doesn’t matter
On Linux it does.)
$ cd Desktop
Notice that the command didn’t output anything. This means that it was carried
out successfully. Let’s check by using
If something had gone wrong, however, the command would have told you. Let’s test that by trying to move into a non-existent directory:
$ cd "Evil plan to destroy the world"
bash: cd: Evil plan to destroy the world: No such file or directory
Notice that we surrounded the name by quotation marks. The arguments given to any shell command are separated by spaces, so a way to let them know that we mean ‘one single thing called “Evil plan to destroy the world”’, not ‘six different things’, is to use (single or double) quotation marks.
We’ve now seen how we can go ‘down’ through our directory structure
(as in into more nested directories). If we want to go back, we can type
This moves us ‘up’ one directory, putting us back where we started.
If we ever get completely lost, the command
cd without any arguments will bring
us right back to the home directory, the place where we started.
To switch back and forth between two directories use
Move around the computer, get used to moving in and out of directories, see how different file types appear in the Unix shell. Be sure to use the
cdcommands, and the different flags for the
lscommand you learned so far.
Move into the ‘LibraryCarpData’ directory that you downloaded to prepare for this workshop. How many files are there? How large is each one? When were they created?
If you run Windows, also try typing
explorer .to open Explorer for the current directory (the single dot means “current directory”). If you’re on a Mac, try
open .and for Linux try
xdg-open .to open their graphical file manager.
Being able to navigate the file system is very important for using the Unix shell effectively. As we become more comfortable, we can get very quickly to the directory that we want.
mancommand to invoke the manual page (documentation) for a shell command. For example,
man lsdisplays all the arguments available to you - which saves you remembering them all! Try this for each command you’ve learned so far. Use the spacebar to navigate the manual pages. Use q at any time to quit.
Note: this command is for Mac and Linux users only. It does not work directly for Windows users. If you use windows, you can search for the Shell command on http://man.he.net/, and view the associated manual page. In some systems the command name followed by
--helpwill work, e.g.
Also, the manual lists commands individually, e.g., although
-hcan only be used together with the
you’ll find it listed as
-hin the manual, not as
$ man ls
LS(1) BSD General Commands Manual LS(1) NAME ls -- list directory contents SYNOPSIS ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...] DESCRIPTION For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, asso- ciated information. If no operands are given, the contents of the current directory are dis- played. If more than one operand is given, non-directory operands are displayed first; directory and non-directory operands are sorted sepa- rately and in lexicographical order. The following options are available: -@ Display extended attribute keys and sizes in long (-l) output. -1 (The numeric digit ``one''.) Force output to be one entry per line. This is the default when output is not to a terminal. -A List all entries except for . and ... Always set for the super- user. ...several more pages... BUGS To maintain backward compatibility, the relationships between the many options are quite complex. BSD May 19, 2002 BSD
Find out about advanced
Find out, using the manual page, how to list the files in a directory ordered by their filesize. Try it out in different directories. Can you combine it with the
-largument you learned before?
Afterwards, find out how you can order a list of files based on their last modification date. Try ordering files in different directories.
To order files in a directory by their filesize, in combination with the
Note that the
To order files in a directory by their last modification date, in combination with the
Knowing where you are in your directory structure is key to working with the shell