Navigating Files and Directories
Overview
Teaching: 15 min
Exercises: 0 minQuestions
How can I move around on my computer?
How can I see what files and directories I have?
How can I specify the location of a file or directory on my computer?
Objectives
Absolute vs Relative Paths
Starting from
/Users/amanda/data/, which of the following commands could Amanda use to navigate to her home directory, which is/Users/amanda?
cd .cd /cd /home/amandacd ../..cd ~cd homecd ~/data/..cdcd ..Solution
- No:
 .stands for the current directory.- No:
 /stands for the root directory.- No: Amanda’s home directory is
 /Users/amanda.- No: this goes up two levels, i.e. ends in
 /Users.- Yes:
 ~stands for the user’s home directory, in this case/Users/amanda.- No: this would navigate into a directory
 homein the current directory if it exists.- Yes: unnecessarily complicated, but correct.
 - Yes: shortcut to go back to the user’s home directory.
 - Yes: goes up one level.
 
Relative Path Resolution
Using the filesystem diagram below, if
pwddisplays/Users/thing, what willls -F ../backupdisplay?
../backup: No such file or directory2012-12-01 2013-01-08 2013-01-272012-12-01/ 2013-01-08/ 2013-01-27/original/ pnas_final/ pnas_sub/
Solution
- No: there is a directory
 backupin/Users.- No: this is the content of
 Users/thing/backup, but with..we asked for one level further up.- No: see previous explanation.
 - Yes:
 ../backup/refers to/Users/backup/.
lsReading ComprehensionAssuming a directory structure as in the above Figure (File System for Challenge Questions), if
pwddisplays/Users/backup, and-rtellslsto display things in reverse order, what command will display:pnas_sub/ pnas_final/ original/
ls pwdls -r -Fls -r -F /Users/backup- Either #2 or #3 above, but not #1.
 Solution
- No:
 pwdis not the name of a directory.- Yes:
 lswithout directory argument lists files and directories in the current directory.- Yes: uses the absolute path explicitly.
 - Correct: see explanations above.
 
Exploring More
lsFlagsWhat does the command
lsdo when used with the-land-hflags?Some of its output is about properties that we do not cover in this lesson (such as file permissions and ownership), but the rest should be useful nevertheless.
Solution
The
-lflag makeslsuse a long listing format, showing not only the file/directory names but also additional information such as the file size and the time of its last modification. The-hflag makes the file size “human readable”, i.e. display something like5.3Kinstead of5369.
Listing Recursively and By Time
The command
ls -Rlists the contents of directories recursively, i.e., lists their sub-directories, sub-sub-directories, and so on in alphabetical order at each level. The commandls -tlists things by time of last change, with most recently changed files or directories first. In what order doesls -R -tdisplay things? Hint:ls -luses a long listing format to view timestamps.Solution
The directories are listed alphabetical at each level, the files/directories in each directory are sorted by time of last change.
Key Points