Working with Unix#
Self-Help#
Use
man
to look up the flag for human-readable output fromls
.Get help with
man
by typingman man
into the console.Wouldn’t it be nice if there was a calendar command? Use
apropos
to look for such a command, then useman
to read about how that command works.
Solution to
-h
. Human readable output output outputs the file size using MB, GB instead of bytesuseful:
cal -3
Get Wild#
Before I organized the photos by year, what command would have listed all of the photos of type
.png
?Before I organized the photos by year, what command would have deleted all of my hiking photos?
What series of commands would you use in order to put my figures for a data science course and the pictures I took in the lab into their own folders?
Solution to
ls *.png
rm *-hiking*.jpg
mkdir data-science-course mkdir lab-pictures mv figure-*.svg data-science-course mv lab*.jpg lab-pictures
Search#
Search
states.txt
andcanada.txt
for lines that contain the word “New”.Make five text files containing the names of states that don’t contain one of each of the five vowels.
Download the GitHub repository for this book and find out how many
.html
files it contains.
Solution to
Download required files:
curl https://seankross.com/notes/states.txt -O
curl https://seankross.com/notes/canada.txt -O
grep New states.txt canada.txt
grep -iv 'a' states.txt > states-without-a.txt grep -iv 'e' states.txt > states-without-e.txt grep -iv 'i' states.txt > states-without-i.txt grep -iv 'o' states.txt > states-without-o.txt grep -iv 'u' states.txt > states-without-u.txt
compacter:
for vowel in a e i o u; do grep -iv $vowel states.txt > states-without-$vowel.txt; done
git clone https://github.com/seankross/the-unix-workbench find the-unix-workbench -iname '*.html' | wc -l
Pipes#
Use pipes to figure out how many US states contain the word “New.”
Examine your
~/.bash_history
to try to figure out how many unique commands you’ve ever used. (You may need to look up how to use theuniq
andsort
commands).
Solution to
grep New states.txt canada.txt | wc -l
Note that
uniq
only removes subsequent repeated lines. If the line is separated with a different line, thenuniq
does not remove the duplicate.The same behavior can also be achieved using
sort -u
without usinguniq
sort ~/.histfile | uniq