Working with Unix#
Self-Help#
Exercise 5
Use
manto look up the flag for human-readable output fromls.Get help with
manby typingman maninto the console.Wouldn’t it be nice if there was a calendar command? Use
aproposto look for such a command, then usemanto read about how that command works.
Solution to Exercise 5
-h. Human readable output output outputs the file size using MB, GB instead of bytesuseful:
cal -3
Get Wild#
Exercise 6
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 Exercise 6
ls *.pngrm *-hiking*.jpgmkdir data-science-course mkdir lab-pictures mv figure-*.svg data-science-course mv lab*.jpg lab-pictures
Search#
Exercise 7
Search
states.txtandcanada.txtfor 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
.htmlfiles it contains.
Solution to Exercise 7
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#
Exercise 8
Use pipes to figure out how many US states contain the word “New.”
Examine your
~/.bash_historyto try to figure out how many unique commands you’ve ever used. (You may need to look up how to use theuniqandsortcommands).
Solution to Exercise 8
grep New states.txt canada.txt | wc -l
Note that
uniqonly removes subsequent repeated lines. If the line is separated with a different line, thenuniqdoes not remove the duplicate.The same behavior can also be achieved using
sort -uwithout usinguniqsort ~/.histfile | uniq