SciencesPo Intro To Programming 2024
11 October, 2024
Questions
Objectives
pwd
command - present working directory.with output:
/Users/nelle
Home Directory Variations
/home/nelle
C:\Documents and Settings\nelle
if pwd
does not return your home directory, may need to navigate there first with cd
.
Nelle’s file system looks like this:
/
is the root of the system/
to directory Users
, which contains folder Nelle
/
is a separator. (It’s \
on Windows!)ls
ls -F
. This adds option F
(for full) to the command./
indicates that this is a directory@
indicates a link*
indicates an executableApplications/ Documents/ Library/ Music/ Public/
Desktop/ Downloads/ Movies/ Pictures/
question
What kind of objects does Nelle’s home directory contain?
Clear Terminal
clear
command to clear terminal.↑
and ↓
keys to see previous commands, or just scroll up.ls
FlagsChallenge
You can also use two options at the same time. What does the command ls
do when used with the -l
option? What about if you use both the -l
and the -h
option?
Solution
The -l
option makes ls
use 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. If you use both the -h
option and the -l
option, this makes the file size ‘human readable’, i.e. displaying something like 5.3K
instead of 5369
.
ls
ChallengesListing in Reverse Chronological Order
By default, ls
lists the contents of a directory in alphabetical order by name. The command ls -t
lists items by time of last change instead of alphabetically. The command ls -r
lists the contents of a directory in reverse order. Which file is displayed last when you combine the -t
and -r
options? Hint: You may need to use the -l
option to see the last changed dates.
Solution
The most recently changed file is listed last when using -rt
. This can be very useful for finding your most recent edits or checking to see if a new output file was written.
home
directory. (~
, not Desktop
!)ls
can search other than only the current directories.home
:shows for Nelle only the data we just downloaded:
shell-lesson-data/
exercise-data/ north-pacific-gyre/
cd
is for change directory. Moves the shell to a different location in the file system.cd
command does not print any output by default.ls -F
again to see what’s in this directory!pwd
to see where we are!cd shell-lesson-data
cd
can only go into its own subdirectories...
is its parent directory, so goes one up.puts Nelle back into
/Users/nelle/shell-lesson-data
..
is listed if you flag -a
on the ls
command.Hidden Files
cd
without any arguments puts you back into your Home directory. Do it.ls -F -a
or ls -Fa
to list all files. Also hidden ones!Relative and Absolute Paths
cd
and ls
operated from our current position in the file sytem./
. This allows to go anywhere from anywhere.Tilde (~
) and dash (-
)
~
in first position means current user’s homecd -
means go into the directory I was previously in.cd ..
brings you up one levelcd -
takes you back to wherever you’ve come from.Challenge
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/amanda
cd ../..
cd ~
cd home
cd ~/data/..
cd
cd ..
Solution
.
stands for the current directory./
stands for the root directory./Users/amanda
./Users
.~
stands for the user’s home directory, in this case /Users/amanda
.home
in the current directory if it exists.Challenge
Using the filesystem diagram , if pwd
displays /Users/thing
, what will ls -F ../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
backup
in /Users
.Users/thing/backup
, but with ..
, we asked for one level further up.../backup/
refers to /Users/backup/
.Challenge
Using the filesystem diagram below, if pwd
displays /Users/backup
,and -r
tells ls
to display things in reverse order, what command(s) will result in the following output:
pnas_sub/ pnas_final/ original/
is it:
ls pwd
?ls -r -F
?ls -r -F /Users/backup
?Solution
pwd
is not the name of a directory.ls
without directory argument lists files and directories in the current directory.Let’s take as example this command:
ls
and whatever options you put is important.ls -s
is not the same as ls -S
:north-pacific-gyre/
. let’s go there.north-pacific-gyre
is a mouthful to write. try instead to type cd n
and hit the TAB key.pwd
.Key Points
pwd
prints the user’s current working directory.ls [path]
prints a listing of a specific file or directory; ls
on its own lists the current working directory.cd [path]
changes the current working directory.-
./
on Unix, but \\
on Windows./
on its own is the root directory of the whole file system..
on its own means ‘the current directory’; ..
means ‘the directory above the current one’.