Introduction to the command-line
Pre-lecture activities
In advance of class, please follow the instructions on Software Carpentry: The Unix Shell to
- Install a Unix shell on your computer (if it is not already installed)
- How to access and open the Unix shell on your computer
- How to type commands on the Unix shell
- Familarize yourself with some of the basic commands in Chapters 1 to 7. If you have used the Unix shell before, these commands are likely familiar to you already. If you have never used the Unix shell to write commands, I encourage you to skim the material to learn some of the terminology and basics. You are not expected to spend multiple hours on this, but during class, I will give an overview of working in the Unix shell and then we will practice the commands with an in-class activity. If you have not opened a Unix shell before or you are not comfortable with executing commands, it will be challenging to participate in the activity at the end of lecture.
Lecture
Acknowledgements
Material for this lecture was borrowed and adopted from
Learning objectives
At the end of this lesson you will:
- Understand what is a command shell and why would use one.
- Create, copy, move, rename, and delete files and folders.
- Search for regular expressions in files.
- Execute R commands and scripts in the command line.
- Redirect a command’s output to a file with redirect operators (
>,>>). - Construct command pipelines with two or more stages with the pipe operator (
|). - Write a loop that applies one or more commands separately to each file in a set of files.
You can practice your command-line skills with the Command Challenge
Slides
Class activity
For our in-class activity today, we will
- Practice writing commands in the Unix shell
- Enjoy a little fun with Taylor Swift lyrics
Find a partner and work on the following tasks. It’s unlikely you will finish all of them, but try to do as many as you can! Also, try to work out the solution for yourselves first and then check the solution afterwards.
- Create Taylor Swift’s Albums Directory. Navigate to your home directory. Create a directory called
taylor_swift_discography. Insidetaylor_swift_discography, create subdirectories for each of her albums:fearless,red,1989,reputation, andlover.
Expected commands: cd, mkdir
- List all the album directories in
taylor_swift_discographyin a long format, showing permissions and modification dates.
ls -l
- Navigate to the
reddirectory and create a file calledall_too_well.txt. Add the first few lines of the song “All Too Well” to the file using a single Unix command.
A combo of touch, echo, cat
- Display the contents of
all_too_well.txtwithout opening the file in a text editor.
Expected commands: cat, more, less
- Navigate to the
1989directory. Create a file calledblank_space.txtand add some of the lyrics. Count how many words are in the song lyrics using a Unix command.
Expected commands: wc
- In the
reputationdirectory, create a file calleddelicate.txtand add some of the lyrics. Use a command to find all occurrences of the word “delicate” in the file.
Expected commands: grep
- Move the file
all_too_well.txtfrom thereddirectory to a new directory calledfavoriteswithintaylor_swift_discography. Copyblank_space.txtfrom1989to thefavoritesdirectory.
Expected commands: mv, cp
- Navigate to the
favoritesdirectory and compress bothall_too_well.txtandblank_space.txtinto a file calledfavorite_lyrics.tar.gz.
Expected commands: tar, gzip
- Uncompress the
favorite_lyrics.tar.gzfile and verify that the files have been restored correctly.
Expected commands: tar, gunzip
- In the
taylor_swift_discographydirectory, find all the lines (representing song lyrics) that contain the word “love”. Output the results to a file calledlove_songs.txtin thefavoritesdirectory.
grep -r "love" >> favorites/love_songs.txt
- For each song in
favorites, write aforloop to print to the screen the first two lines of each song in the filesall_too_well.txtandblank_space.txt.
for
- Create a shell script called
top_favorites.shand place theforloop in the shell script. Run the shell script.
Expected comments: touch
- In the
taylor_swift_discographydirectory, create a file calledsong_metadata.csvthat contains columns for album name, song title, duration (in minutes), and year with the following information.
Album,Song Title,Duration,Year
Fearless,Love Story,3.55,2008
Red,All Too Well,5.29,2012
1989,Blank Space,3.51,2014
Reputation,Delicate,3.52,2017
Lover,You Need to Calm Down,2.51,2019
Folklore,Cardigan,4.00,2020
Evermore,Willow,3.34,2020
Midnights,Anti-Hero,3.21,2022
Use awk to extract and display only the song titles and durations from this file.
awk -F, '{print $2, $3}' song_metadata.csv
Post-lecture
Summary
- Shell is a text based application for viewing, handling and manipulating files
- It is also known by the following names
- CLI (Command Line Interface)
- Terminal
- Bash (Bourne Again Shell)
- Use
Rscript -eorR -eto execute R scripts from the command line - RStudio includes a Terminal (from version 1.1.383)
- Execute commands from shell script in RStudio using Ctrl + Enter
- RMarkdown and Quarto supports bash, sh and awk
- Python terminals can be opened from the command
- Conda environments can be useful when install Python libraries
Additional practice
Here are some additional practice questions to help you think about the material discussed.
- Move around the computer, get used to moving in and out of directories, see how different file types appear in the Unix shell. Be sure to use the
pwdandcdcommands, and the different flags for thelscommands. - Practice using “Tab for Auto-complete” in the shell to autocomplete commands or file names.
- Explore the manual pages of
datein the command line to show you what that looks like. Try to figure out what is the argument to print the date since the Unix epoch or 00:00:00 UTC on 1 January 1970 as a function of the number of seconds. Then try to identify what is the argument to display the date in UTC. - Practice your command line knowledge with Command Challenge.