The Unix Shell
The file system manages and organizes our files and directories using a common structure.
Parent-child relationships
A “family tree” (more like a root system) of “parent” and “child” relationships (Figure 1).
Directionality
Parent items are at the top/up; child items are at the bottom/down (Figure 2).
Different ways to access
Accessible via command-line (Figure 3a) and GUI (Figure 3b).
The directories, files, and subdirectories of a file system are connected by paths. Paths also describe the locations within the file system.
There are two types of paths:
/
), to the specified file or directory. The absolute path always starts with /
.Target | Absolute Path | Relative Path (from the /bin directory) |
---|---|---|
plot.R |
/bin/plot.R |
plot.R |
conda |
/bin/conda |
conda |
.
, ..
, and ~
aliasesThe characters .
, ..
and ~
have special meaning in the unix shells.
.
alias – current directoryFor example the following code means to do_the_thing
in the current directory.
..
alias – parent directoryThe code below means to do_the_thing
two directories above our current directory.
~
alias – user home directoryFinally, the code below means to do_the_thing
in the user’s home directory.
Q&A: If we are in the /tmp
directory, what are the absolute and relative paths of the genome.fa
file?
Q&A: If we are in the /tmp
directory, what are the absolute and relative paths of the genome.fa
file?
Let’s learn a few useful commands for moving around the file system.
pwd
– print working directoryPrints out our current location, called our “working directory”.
Command | Options/Flags | Arguments |
---|---|---|
pwd |
Run the pwd
command in your terminal.
pwd
– print working directoryPrints out our current location, called our “working directory”.
Command | Options/Flags | Arguments |
---|---|---|
pwd |
Run the pwd
command in your terminal.
pwd
Q&A: Is the path returned an absolute or relative path?
pwd
Q&A: Is the path returned an absolute or relative path?
ls
– listLists the items in a directory.
Without a target, the command defaults to the current directory (./
)
Command | Options/Flags | Arguments |
---|---|---|
ls |
flags |
path/to/directory |
In terminal, type ls
and press enter/return.
ls
– listLists the items in a directory.
Without a target, the command defaults to the current directory (./
)
Command | Options/Flags | Arguments |
---|---|---|
ls |
flags |
path/to/directory |
In terminal, type ls
and press enter/return.
Items in your home directory is listed, alphabetically.
ls
– listFlags/options can make the output more useful, a few are shown below.
Flag | Description |
---|---|
-l |
Returns the results in a long format, which provides information about
|
-h |
Returns the results with a human-readible size value |
-a |
Includes entries beginning with a . , which are not shown by default |
ls
– listLet’s use these 3 flags together. Type the ls -lha
into your terminal.
ls
– listLet’s use these 3 flags together. Type the ls -lha
into your terminal.
total 16
drwxrwxr-x@ 5 csifuentes staff 160B Mar 7 08:33 .
drwx------@ 7 csifuentes staff 224B Mar 3 10:26 ..
-rw-r--r--@ 1 csifuentes staff 6.0K Mar 3 12:59 .DS_Store
drwxrwxr-x@ 7 csifuentes staff 224B Sep 16 2021 exercise-data
drwxrwxr-x@ 21 csifuentes staff 672B Sep 16 2021 north-pacific-gyre
cd
– change directoryChanges our location in the file system.
Note: Without a target directory, cd
will default to the user home directory.
Command | Options/Flags | Arguments |
---|---|---|
cd |
path/to/directory |
cd
– change directoryChanges our location in the file system.
Note: Without a target directory, cd
will default to the user home directory.
Command | Options/Flags | Arguments |
---|---|---|
cd |
path/to/directory |
Q&A: Change your current working directory to be one directory above the current directory, then check the new working directory location and list it’s contents.
cd
– change directoryChanges our location in the file system.
Note: Without a target directory, cd
will default to the user home directory.
Command | Options/Flags | Arguments |
---|---|---|
cd |
path/to/directory |
Q&A: Change your current working directory to be one directory above the current directory, then check the new working directory location and list it’s contents.
cd
– change directoryNow let’s move back into the shell-lesson-data
directory.
mkdir
– make directoryCreates new directories.
Note: The -p
flag will create the directory and any required intermediate directories.
Command | Options/Flags | Arguments |
---|---|---|
mkdir |
flags |
path/to/directory path/to/additional/directory |
mkdir
– make directoryLet’s pretend we want to create a directory structure for our thesis work. We need the following:
We’ll do this using only the commands we’ve learned thus far (except for my use of tree
to easily view directory structures). Later, we’ll learn quicker ways to do this.
mkdir
– make directoryMake a top-level directory.
mkdir
– make directoryCreate a directory for each chapter.
mkdir
– make directoryCreate a directory for each subsection of each chapter.
# create sub-directories in each chapter
mkdir -p thesis/chapter_1/images thesis/chapter_1/data thesis/chapter_1/text
mkdir -p thesis/chapter_2/images thesis/chapter_2/data thesis/chapter_2/text
mkdir -p thesis/chapter_3/images thesis/chapter_3/data thesis/chapter_3/text
mkdir -p thesis/chapter_4/images thesis/chapter_4/data thesis/chapter_4/text
mkdir -p thesis/chapter_5/images thesis/chapter_5/data thesis/chapter_5/text
# look at the structure of thesis
tree thesis
Now let’s learn some useful ways to work in the file system.
Allow one to create and edit text files
In-Terminal Examples | GUI Examples |
---|---|
pico , nano |
notepad , notepad++ |
emacs , Vim |
Atom , Visual Studio Code |
nano
Visual Studio Code
nano
– in-line text editorOpens the editor into a file (or new file if it doesn’t exist).
Note: Creates the target file if it does not already exist. Flags and arguments are optional here.
Command | Options/Flags | Arguments |
---|---|---|
nano |
flags |
path/to/file |
Continuing with our thesis work, let’s create a README.txt
file to keep track of each chapter directory.
nano
– in-line text editorLet’s first create the README.txt
file in our thesis
directory.
A file will open in the editor.
nano
– in-line text editorFollow the directions in the image below.
Edit and save the file as README.txt
using the nano
editor.
nano
– in-line text editorLooking in thesis
, we see our new file.
cp
– copyCopies and pastes items with a single command.
Command | Options/Flags | Arguments |
---|---|---|
cp |
flags |
path/to/source path/to/destination |
It might be nice to have a README
in each chapter directory. Let’s use the cp
command to do this.
cp
– copyCopy from the thesis/README.txt
to each chapter directory. This was tedious. Don’t worry, we’ll learn more efficient ways to do this.
# copy to each chapter directory
cp thesis/README.txt thesis/chapter_1/
cp thesis/README.txt thesis/chapter_2/
cp thesis/README.txt thesis/chapter_3/
cp thesis/README.txt thesis/chapter_4/
cp thesis/README.txt thesis/chapter_5/
# view the structure of thesis
tree thesis
thesis
├── README.txt
├── chapter_1
│ ├── README.txt
│ ├── data
│ ├── images
│ └── text
├── chapter_2
│ ├── README.txt
│ ├── data
│ ├── images
│ └── text
├── chapter_3
│ ├── README.txt
│ ├── data
│ ├── images
│ └── text
├── chapter_4
│ ├── README.txt
│ ├── data
│ ├── images
│ └── text
└── chapter_5
├── README.txt
├── data
├── images
└── text
20 directories, 6 files
mv
– move and renameMoves and renames items, including files and directories. Note that the last argument is the destination.
Command | Options/Flags | Arguments |
---|---|---|
mv |
flags |
path/to/source path/to/other/source path/to/destination |
Let’s rename the README.txt
file in the chapter 1 directory so that it contains the chapter number.
mv
– move and renameRename by typing in the following.
rm
– removeDeletes the specified target.
Command | Options/Flags | Arguments |
---|---|---|
rm |
flags |
path/to/target |
rm
– removeFor “fun”, let’s remove the thesis directory.
Wildcards represent 0 or more characters and are used for pattern matching. Two useful wilcards are shown here.
*
– 0 or more characters?
– exactly 1 characterLet’s see some examples with of each. From our shell-lesson-data/exercise-data/proteins
.
Listing all files.
Listing files ending in ethane.pdb
, using *
.
Note that we use the *
at the end becuase all files have the same .pdb
ending, so this is faster.
Listing files ending in ethane.pdb
with a preceeding character, using ?
.
Wildcards can be used together and combined in different ways to form complex patterns.
For example, we can use ???ane.pdb
together to indicate any 3 characters followed by ane.pdb
.
Starting from /Users/amanda/data
, which command(s) whould take Amanda to her home directory (/Users/amanda
)?
cd .
cd /
cd /home/amanda
cd ../..
cd ~
cd home
cd
cd ..
Starting from /Users/amanda/data
, which command(s) whould take Amanda to her home directory (/Users/amanda
)?
cd .
– No, will be in the same placecd /
– No, will be in rootcd /home/amanda
– No, not where we want to becd ../..
– No, will be in /Userscd ~
– YES, ~
is an alias for the user’s home directorycd home
– No, not a thingcd
– YES, without input cd
will take you to the home directorycd ..
– YESWith the file system shown, if pwd
displays Users/thing
, what will ls -F ../backup
display?
Note: -F
adds a /
to the end of directories.
../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/
With the file system shown, if pwd
displays Users/thing
, what will ls -F ../backup
display?
Note: -F
adds a /
to the end of directories.
../backup/
refers to /Users/backup
With the file system below, if pwd
displays /Users/backup
and ls -r
displays items in reverse order, what command(s) will result in the following output?
pnas_sub/ pnas_final/ original/
ls pwd
ls -r -F
ls -r -F /Users/backup
With the file system below, if pwd
displays /Users/backup
and ls -r
displays items in reverse order, what command(s) will result in the following output?
pnas_sub/ pnas_final/ original/
ls pwd
– No, will look for pwd
as the target.ls -r -F
– Yesls -r -F /Users/backup
– YesChris runs the following commands and realizes that sucrose.dat
and maltose.dat
should be in the raw/
directory.
Complete the command below to move these files into the raw/
directory.
Answer:
Chris gave you a file named file.txt
, which contains a list of his favorite animals. You want to rename it to why_do_i_need_this.txt
. Which of the following commands would do the trick?
cp file.txt why_do_i_need_this.txt
mv file.txt why_do_i_need_this.txt
mv file.txt .
cp file.txt .
Chris gave you a file named file.txt
, which contains a list of his favorite animals. You want to rename it to why_do_i_need_this.txt
. Which of the following commands would do the trick?
cp file.txt why_do_i_need_this.txt
– No. This creates a new file instead of renaming the old file.mv file.txt why_do_i_need_this.txt
– YES. This renames the file.mv file.txt .
– No. This moves the file to the current directory with no new file name – would throw an error.cp file.txt .
– No. This copies the file to the current directory with no new file name – would throw an error.What is the output of the final ls
command in the sequence shown below?
proteins-saved.dat recombined
recombined
proteins.dat recombined
proteins-saved.dat
Answer:
recombined
Chris accidentally removed a file named important_file.txt
. How can the file be retrieved?
rm --undo
Answer:
When run the in proteins/
directory, which command(s) will produce the output below?
ethane.pdb methane.pdb
ls *t*ane.pdb
ls *t?ne.*
ls *t??ne.pdb
ls ethane.*
When run the in proteins/
directory, which command(s) will produce the output below?
ethane.pdb methane.pdb
ls *t*ane.pdb
– No, would give ethane.pdb methane.pdb octane.pdb pentane.pdb
ls *t?ne.*
– No, would give octane.pdb pentaçne.pdb
ls *t??ne.pdb
– Yes.ls ethane.*
– No, would give ethane.pdb
Slide and outputs are scrollable
Sam has the following diretory structure.
.
├── 2015-10-23-calibration.txt
├── 2015-10-23-dataset1.txt
├── 2015-10-23-dataset2.txt
├── 2015-10-23-dataset_overview.txt
├── 2015-10-26-calibration.txt
├── 2015-10-26-dataset1.txt
├── 2015-10-26-dataset2.txt
├── 2015-10-26-dataset_overview.txt
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
├── 2015-11-23-dataset_overview.txt
├── backup
│ ├── calibration
│ └── datasets
└── send_to_bob
├── all_datasets_created_on_a_23rd
└── all_november_files
Sam uses the following commands to create a backup directory and another directory to send to her collaborator, Bob.
Help Sam by filling in the blanks so that the resulting structure looks like this.
.
├── 2015-10-23-calibration.txt
├── 2015-10-23-dataset1.txt
├── 2015-10-23-dataset2.txt
├── 2015-10-23-dataset_overview.txt
├── 2015-10-26-calibration.txt
├── 2015-10-26-dataset1.txt
├── 2015-10-26-dataset2.txt
├── 2015-10-26-dataset_overview.txt
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
├── 2015-11-23-dataset_overview.txt
├── backup
│ ├── calibration
│ │ ├── 2015-10-23-calibration.txt
│ │ ├── 2015-10-26-calibration.txt
│ │ └── 2015-11-23-calibration.txt
│ └── datasets
│ ├── 2015-10-23-dataset1.txt
│ ├── 2015-10-23-dataset2.txt
│ ├── 2015-10-23-dataset_overview.txt
│ ├── 2015-10-26-dataset1.txt
│ ├── 2015-10-26-dataset2.txt
│ ├── 2015-10-26-dataset_overview.txt
│ ├── 2015-11-23-dataset1.txt
│ ├── 2015-11-23-dataset2.txt
│ └── 2015-11-23-dataset_overview.txt
└── send_to_bob
├── all_datasets_created_on_a_23rd
│ ├── 2015-10-23-dataset1.txt
│ ├── 2015-10-23-dataset2.txt
│ ├── 2015-10-23-dataset_overview.txt
│ ├── 2015-11-23-dataset1.txt
│ ├── 2015-11-23-dataset2.txt
│ └── 2015-11-23-dataset_overview.txt
└── all_november_files
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
└── 2015-11-23-dataset_overview.txt
Answer: