2022-10-24
cd
)bash
(Bourne Again SHell)zsh
(Z SHell)Galaxy
try to wrap these tools)LIVE DEMONSTRATION
$ $ ls
goostats.sh
(a program): 30s per sampleLIVE DEMONSTRATION
/home/<USERNAME>
/Users/<USERNAME>
C:\Documents and Settings\<USERNAME>
or C:\Users\<USERNAME>
Return to your home directory with either command:
cd cd ~
/Users/nelle
/Users/nelle
LIVE DEMONSTRATION
ls ls -F ls --help man ls ls -j ls -ltrh ls -F Desktop cd Desktop pwd cd .. ls -Fa cd cd Desktop/shell-lesson-data/exercise-data cd ~ cd /Users/<USERNAME>/Desktop/shell-lesson-data
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 ..
Using the filesystem diagram below, 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/
Consider the command $ ls -F /
Upper and lower case matter
ls -s ls -S
north-pacific-gyre
NENE01729A.txt
LIVE DEMONSTRATION
LIVE DEMONSTRATION
pwd cd exercise-data/writing/ mkdir thesis mkdir -p ../project/data ../project/results ls -FR ../project
Good names for files and directories:
-
(dash or hyphen)LIVE DEMONSTRATION
cd thesis nano draft.txt
Text editor choices
nano
: simple, basic, but extendableemacs
: extremely powerful, steep learning curvevim
: extremely powerful, steep learning curveGedit
: GUI text editor on LinuxNotepad++
: GUI text editor on WindowsVisual Studio Code (VSCode)
: cross-platform text editorYou may see instructions to hold the Control/Ctrl key written in several ways. E.g. to hold down Control and press X you might be instructed to press:
Control-X
Control+X
Ctrl-X
Ctrl+X
^X
C-x
Files often have two parts, separated by a dot/period, e.g. myfile.txt
.
myfile
is the filestemtxt
is the extensionThe extension is an indicator of the data the file contains, but it is only a convention. The extension does not determine the content of the file.
You can’t turn your project thesis into an audiobook by renaming thesis.txt
to thesis.mp3
.
LIVE DEMONSTRATION
cd ~/Desktop/shell-lesson-data/exercise-data/writing mv thesis/draft.txt thesis/quotes.txt mv -i thesis/quotes.txt . ls thesis/quotes.txt
Be careful: mv
will overwrite the destination
LIVE DEMONSTRATION
cp quotes.txt thesis/quotations.txt ls quotes.txt thesis/quotations.txt cp -r thesis thesis_backup ls thesis thesis_backup
Suppose that you created a plain-text file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it: statstics.txt
After creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?
cp statstics.txt statistics.txt
mv statstics.txt statistics.txt
mv statstics.txt .
cp statstics.txt .
LIVE DEMONSTRATION
rm quotes.txt ls quotes.txt
Deleting is forever
rm -i thesis_backup/quotations.txt
LIVE DEMONSTRATION
mkdir backup cp creatures/minotaur.dat creatures/unicorn.dat backup/ ls *.pdb ls p*.pdb ls ?ethane.pdb ls *ethane.pdb ls ???ane.pdb
When run in the proteins directory, which ls command(s) will produce the output:
ethane.pdb methane.pdb
ls *t*ane.pdb
ls *t?ne.*
ls *t??ne.pdb
ls ethane.*
wc
commandshell-lesson-data/exercise-data/proteins
The .pdb
extension indicates that these are Protein DataBank structure files
LIVE DEMONSTRATION
ls proteins cd proteins wc cubane.pdb wc *.pdb wc -l *.pdb
wc
is written to the terminal, but we can redirect it with the >
(right angled bracket) characterLIVE DEMONSTRATION
wc -l *.pdb > lengths.txt ls lengths.txt cat lengths.txt less lengths.txt
sort
commandLIVE DEMONSTRATION
sort ../numbers.txt sort -n ../numbers.txt sort -n lengths.txt sort -n lengths.txt > sorted-lengths.txt head -n 1 sorted-lengths.txt
|
) symbol is like the redirect, except it passes the output to another command, rather than to a file.LIVE DEMONSTRATION
sort -n lengths.txt | head -n 1 wc -l *.pdb | sort -n wc -l *.pdb | sort -n | head -n 1
Tools and commands in Unix are designed to be linked together in this way, as pipelines. This power is one of the reasons that Unix has been so successful.
In our current directory, we want to find the 3 files which have the least number of lines. Which command listed below would work?
wc -l * > sort -n > head -n 3
wc -l * | sort -n | head -n 1-3
wc -l * | head -n 3 | sort -n
wc -l * | sort -n | head -n 3
north-pacific-gyre
directorycd north-pacific-gyre wc -l *.txt wc -l *.txt | sort -n | head -n 5
One of the files is too short! Do any of the files have too much data?
wc -l *.txt | sort -n | tail -n 5
There’s a convention that files with missing data end with Z
ls *Z.txt
Let’s look in exercise-data/creatures
head -n 5 basilisk.dat minotaur.dat unicorn.dat
We want the classification for each species…
for thing in list_of_things do operation_using $thing # Indentation not required, but readable done
We need to apply this structure to our creature files
$ for filename in basilisk.dat minotaur.dat unicorn.dat > do > head -n 2 $filename | tail -n 1 > done
The shell prompt changes from $
to >
to let us know that we haven’t finished typing our command, yet.
How would you write a loop that echoes all 10 numbers from 0 to 9?
We want to see some of the middle of the sequence for each creature.
$ for filename in *.dat > do > echo $filename > head -n 100 $filename | tail -n 20 > done
Note that
for filename in red dragon.txt
)We want to modify each file as we loop over it, e.g. rename the original basilisk.dat
file to original-basilisk.dat
and put modified data into basilisk.dat
.
We can’t use the following:
cp *.dat original-*.dat
because this is equivalent to
cp basilisk.dat minotaur.dat unicorn.dat original-*.dat
Instead we use a loop:
$ for filename in *.dat > do > cp $filename original-$filename > done
Nelle can now process her data files with goostats.sh
(a shell script her supervisor wrote).
This script takes two arguments: an input file (the raw data), and the output file (calculated statistics).
LIVE DEMONSTRATION
Ctrl-a
takes you to the start of a lineCtrl-e
takes you to the end of a lineWe can take commands we repeat frequently and save them in files so they can be re-run with a single command. Such files are called shell scripts (these are programs).
LIVE DEMONSTRATION
cd proteins nano middle.sh head -n 15 octane.pdb | tail -n 5 bash middle.sh
Script files must be in plain text
Word files, etc. are not plain text
LIVE DEMONSTRATION
nano middle.sh head -n 15 "$1" | tail -n 5 bash middle.sh octane.pdb bash middle.sh pentane.pdb
We put double-quotes around "$1"
to protect in case any filenames contain spaces or other special characters.
LIVE DEMONSTRATION
nano middle.sh head -n "$2" "$1" | tail -n "$3" bash middle.sh pentane.pdb 15 5 bash middle.sh pentane.pdb 20 5
LIVE DEMONSTRATION
# Select lines from the middle of a file. # Usage: bash middle.sh filename end_line num_lines head -n "$2" "$1" | tail -n "$3"
LIVE DEMONSTRATION
wc -l *.pdb | sort -n
We use the special variable $@
- “all command line arguments”
# Sort files by their length. # Usage: bash sorted.sh one_or_more_filenames wc -l "$@" | sort -n
bash sorted.sh *.pdb ../creatures/*.dat
nano do-stats.sh
# Calculate stats for data files. for datafile in "$@" do echo $datafile bash goostats.sh $datafile stats-$datafile done
bash do-stats.sh NENE*A.txt NENE*B.txt bash do-stats.sh NENE*A.txt NENE*B.txt | wc -l
grep
to select lines from text files that match simple patterns.find
to find files and directories whose names match simple patterns.grep
grep
is a very useful command-line tool that lets us find lines in files that match a pattern we want to look forgrep
is short for global/regular expression/print
LIVE DEMONSTRATION
cd cd Desktop/shell-lesson-data/exercise-data/writing cat haiku.txt grep not haiku.txt grep The haiku.txt grep -w The haiku.txt grep -w "is not" haiku.txt grep -n "it" haiku.txt grep -n -w "the" haiku.txt grep -nwi "the" haiku.txt grep -nwv "the" haiku.txt grep -r Yesterday .
find
find
command finds files in the filesystemLIVE DEMONSTRATION
cd shell-lesson-data/exercise-data find . find . -type d find . -type f find . -name *.txt find . -name "*.txt"
find
as the input to other commandsLIVE DEMONSTRATION
wc -l $(find . -name "*.txt") wc -l ./writing/LittleWomen.txt ./writing/haiku.txt ./numbers.txt grep "searching" $(find . -name "*.txt")