Understand what is a command shell and why would use one.
Explain how the shell relates to the keyboard, the screen, the operating system, and users’ programs.
Explain when and why command-line interfaces should be used instead of graphical interfaces.
Create, copy, move, rename, and delete files and folders.
Print and sort file contents.
Search for regular expressions in files.
Execute R commands and scripts in the command line.
Tip
You can practice your command-line skills with the Command Challenge
Introduction
We we use interact with computers, we often do so with a keyboard and mouse, touch screen interfaces, or using speech recognition systems.
The most widely used way to interact with personal computers is called a graphical user interface (GUI). With a GUI, we give instructions by clicking a mouse and using menu-driven interactions.
The problem with only working with GUIs is that while the visual aid of a GUI makes it intuitive to learn, this way of delivering instructions to a computer scales very poorly.
Example
Imagine the following task: for a literature search, you have to
Copy the third line of one thousand text files in one thousand different directories
Paste the lines into a separate single file.
Using a GUI, you would not only be clicking at your desk for several hours, but you could potentially also commit an error in the process of completing this repetitive task.
This is where we take advantage of the Unix shell.
The Unix shell is both
A command-line interface (CLI)
A scripting language
This allows such repetitive tasks to be done automatically and fast. Using the shell, the task in the literature example can be accomplished in seconds.
The Shell
The shell is a program (or environment) where users can type commands and the commands can be executed.
Another way of thinking about it is, a shell provides an interface between the user and the UNIX system.
Example types of shells
Bash (Bourne Again SHell). The most popular Unix shell is Bash (the Bourne Again SHell — so-called because it’s derived from a shell written by Stephen Bourne). Bash is the default shell on most modern implementations of Unix and in most packages that provide Unix-like tools for Windows.
Zsh (Z SHell). Zsh is built on top of bash with some additional features including providing the user with more flexibility by providing various features such as plug-in support, better customization, theme support, spelling correction, etc. Zsh is the default shell for macOS and Kali Linux.
The grammar of a shell allows you to combine existing tools into powerful pipelines and handle large volumes of data automatically.
Benefits:
Sequences of commands can be written into a script, improving the reproducibility of workflows.
The command line is often the easiest way to interact with remote machines and supercomputers.
Familiarity with the shell is near essential to run a variety of specialized tools and resources including high-performance computing systems.
Let’s get started.
Where to find the shell
If you are using Windows, by default, Windows does not use bash, but instead you will need to install one of several Windows-specific tools (like Git for Windows or PowerShell) to allow this kind of text-based interaction with your operating system.
If you are using macOS, Apple calls the shell ‘Terminal’. There is an application you can open called ‘Terminal’ and it also appears in a tab next to the R console in the RStudio IDE.
If the shell can’t find a program whose name is the command you typed, it will print an error message such as:
ks
Error in running command bash
This might happen if the command was mis-typed or if the program corresponding to that command is not installed.
Next, lets learn to display
basic information about the user
the current date & time
the calendar
and clear the screen
Command
Description
R command
whoami
Who is the user?
Sys.info() / whoami::whoami()
date
Get date, time and timezone
Sys.time()
cal
Display calendar
clear
Clear the screen
Ctrl + L
whoamiprints the user id (i.e. the name of the user who runs the command). Use it to verify the user as which you are logged into the system.
whoami
stephaniehicks
date will display or change the value of the system’s time and date information.
date
Thu Nov 3 00:17:02 EDT 2022
cal will display a formatted calendar and clear will clear all text on the screen and display a new prompt.
cal
November 2022
Su Mo Tu We Th Fr Sa
1 2 _ _3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Pro-tip
To clear the R console and the shell, we use Ctrl + L.
Getting help
Before we proceed further, let us learn to view the documentation/manual pages of the commands.
Command
Description
nameofcommand -h
For some nameofcommand command (only for some commands)
man nameofcommand
Display manual pages (i.e. man) for the nameofcommand command
whatis
Single line description of a command
man is used to view the system’s reference manual.
man date
DATE(1) General Commands Manual DATE(1)NAME date – display or set date and timeSYNOPSIS date [-jnRu] [-r seconds | filename][-v [+|-]val[ymwdHMS]] ... [+output_fmt] date [-ju] [[[mm]dd]HH]MM[[cc]yy][.ss] date [-jRu] -f input_fmt new_date [+output_fmt] date [-jnu] [-I[FMT]] [-f input_fmt] [-r ...][-v ...] [new_date]DESCRIPTION When invoked without arguments, the date utility displays the current date and time.
Try it out
Let’s explore the manual pages of date in the command line to show you what that looks like.
We will figure out what is the argument to print out the number of seconds since the Unix epoch or 00:00:00 UTC on 1 January 1970.
We will figure out what is the argument to display the date in UTC.
## try it out
Pro-tip
For most commands (but not all!), NAMEOFCOMMAND -h or NAMEOFCOMMAND --help will bring up a small guide to command options.
For example, python -h or python --help bring up:
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...Options and arguments (and corresponding environment variables):-b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors)-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x-c cmd : program passed in as string (terminates option list)-d : turn on parser debugging output (for experts only, only works on debug builds); also PYTHONDEBUG=x-E : ignore PYTHON* environment variables (such as PYTHONPATH)-h : print this help message and exit (also --help)-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x
Move around files
Navigate the file system
Next, we will introduce commands that help us:
navigate between different folders/directories
return current working directory
list all the files & folders in a directory
create and delete directories
Command
Description
R commands
pwd
Print working directory
here::here()
ls
List directory contents
dir() / list.files() / list.dirs()
cd
Change current working directory
setwd()
mkdir
Create directory
dir.create()
rmdir
Remove/delete directory
pwddisplays the name and path of the present (or current) working directory (pwd).
Pressing tab at any time within the shell will prompt it to attempt to either
auto-complete the name of the command you are searching for
auto-complete the line based on the files or sub-directories in the current directory.
Where two or more files have the same characters, the auto-complete will only fill up to the first point of difference, after which we can add more characters, and try using tab again.
rmdir will remove empty directories from the file system. It can be used to remove multiple empty directories as well.
If the directory is not empty, rmdir will not remove it and instead display a warning that the directory is not empty.
These are all the files in my home directory on my computer.
cd ~ ls
Applications
Creative Cloud Files
Desktop
Documents
Downloads
Dropbox
Library
Movies
Music
Pictures
Public
miniforge3
List directory contents
ls will list the contents of a directory. Using different arguments, we can
list hidden files
view file permissions, ownership, size & modification date
sort by size & modification date
Command
Description
ls
List directory contents
ls -l
List files one per line
ls -a
List all files including hidden files
ls -la
Display file permissions, ownership, size & modification date
ls -lh
Long format list with size displayed in human readable format
ls -lS
Long format list sorted by size
ls -ltr
Long format list sorted by modification date
List files one per line
cd ../..ls-l
total 232
drwxr-xr-x 7 stephaniehicks staff 224 Oct 21 22:40 _freeze
-rw-r--r--@ 1 stephaniehicks staff 976 Oct 17 11:43 _post_template.qmd
-rw-r--r-- 1 stephaniehicks staff 827 Oct 23 11:13 _quarto.yml
drwxr-xr-x 16 stephaniehicks staff 512 Nov 3 00:16 _site
drwxr-xr-x 6 stephaniehicks staff 192 Oct 26 23:55 data
drwxr-xr-x 13 stephaniehicks staff 416 Oct 29 00:49 images
-rw-r--r-- 1 stephaniehicks staff 2200 Oct 24 21:26 index.qmd
-rw-r--r-- 1 stephaniehicks staff 205 Nov 1 21:04 jhustatprogramming2022.Rproj
-rw-r--r-- 1 stephaniehicks staff 189 Aug 11 21:03 lectures.qmd
drwxr-xr-x 23 stephaniehicks staff 736 Oct 20 21:01 posts
-rw-r--r-- 1 stephaniehicks staff 60521 Aug 11 21:03 profile.jpg
drwxr-xr-x 6 stephaniehicks staff 192 Oct 21 21:17 projects
-rw-r--r-- 1 stephaniehicks staff 191 Oct 21 21:08 projects.qmd
-rw-r--r-- 1 stephaniehicks staff 501 Aug 11 21:03 resources.qmd
-rw-r--r-- 1 stephaniehicks staff 3288 Oct 27 18:19 schedule.qmd
-rw-r--r-- 1 stephaniehicks staff 17 Aug 11 21:03 styles.css
-rw-r--r-- 1 stephaniehicks staff 18755 Oct 24 10:54 syllabus.qmd
Hidden files
Next, let’s talk about hidden (or invisible) files. These are everywhere on modern operating systems.
When a programmer needs to have a file or folder, but does not want to show it to the user, they prefixes the file name with a single period (.). The operating system then hides this files from the user.
But now you can see these invisible files using the command line. Just use the -a flag (short for “all”) for the ls command to have it show you all the files that are there:
If we want to redirect that output from printing to the terminal and write to a file, we use the > operator like so (command > [file]) where on the left side is output gets piped into a file on the right side.
The PATH variable
An important feature of the command line is the PATH variable.
I won’t get into all the details about the PATH variable, but having a basic understanding will likely prove useful if you ever have to troubleshoot problems in the future.
Have you ever wondered how the command-line knows what to do when you type a command like python or ls?
How does it know what program to run, especially on a computer that might have multiple installations of a program like Python?
The answer is that your system has a list of folders stored in an “environment variable” called PATH.
When you run a command (like python or ls), it goes through those folders in order until it finds an executable file with the name of the command you typed.
Then, when it finds that file, it executes that program and stops looking.
You can see the value of the PATH variable on your computer by typing
That means that when I type python, my computer will first look in the folder /opt/homebrew/Caskroom/miniforge/base/bin to see if there is a file named python it can run. If it can’t find one there, it moves on to to the next one.
Why is this useful
In a perfect world, you will never have to worry about your PATH variable, but there are a couple situations where knowing about your PATH variable can be helpful. In particular:
If you downloaded a program, but you cannot run it from the command line, that probably means that its location is not in the PATH variable.
If you find that when you type a command like python, the command line is not running the version of python you want it to run, that’s probably because a different version of python appears earlier in the PATH variable (since the command line will stop looking through these folders as soon as it finds a match).
Note
You can diagnose this problem by typing which COMMANDNAME, which will tell you the folder from which COMMANDNAME is being run.
which python
/opt/homebrew/Caskroom/miniforge/base/bin/python
which ls
/bin/ls
Print file contents
The cat (stands for catenate) command reads data from files, and outputs (or prints) their contents to the screen.
Example
Let’s consider the release_names.txt file, which contains release names of different R versions.
If we wanted to print the file contents to the screen:
cat release_names.txt
Unsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
Good Sport
Frisbee Sailing
Warm Puppy
Spring Dance
Sock it to Me
Pumpkin Helmet
Smooth Sidewalk
Full of Ingredients
World-Famous Astronaut
Fire Safety
Wooden Christmas Tree
Very Secure Dishes
Very, Very Secure Dishes
Supposedly Educational
Bug in Your Hair
Sincere Pumpkin Patch
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree
If we wanted to number all the output, use the -n option:
cat-n release_names.txt
1 Unsuffered Consequences
2 Great Pumpkin
3 December Snowflakes
4 Gift-Getting Season
5 Easter Beagle
6 Roasted Marshmallows
7 Trick or Treat
8 Security Blanket
9 Masked Marvel
10 Good Sport
11 Frisbee Sailing
12 Warm Puppy
13 Spring Dance
14 Sock it to Me
15 Pumpkin Helmet
16 Smooth Sidewalk
17 Full of Ingredients
18 World-Famous Astronaut
19 Fire Safety
20 Wooden Christmas Tree
21 Very Secure Dishes
22 Very, Very Secure Dishes
23 Supposedly Educational
24 Bug in Your Hair
25 Sincere Pumpkin Patch
26 Another Canoe
27 You Stupid Darkness
28 Single Candle
29 Short Summer
30 Kite Eating Tree
To concatenate several source (or input) files into one final target (or output) file, we can also use the redirect operator (>):
Funny-looking Kid
Unsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
Good Sport
Frisbee Sailing
Warm Puppy
Spring Dance
Sock it to Me
Pumpkin Helmet
Smooth Sidewalk
Full of Ingredients
World-Famous Astronaut
Fire Safety
Wooden Christmas Tree
Very Secure Dishes
Very, Very Secure Dishes
Supposedly Educational
Bug in Your Hair
Sincere Pumpkin Patch
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree
The head command will display the first 10 lines of a file(s) by default.
The tail command displays the last 10 lines of a file(s) by default.
It can be used to display the first (or last) few lines or bytes of a file as well.
Command
Description
head
Output the first parts of a file
head -n num
Output the first num lines of a file
head -c num
Output the first num bytes of a file
tail
Display the last part of a file
tail -n num
Show the last num lines of a file
tail -n +num
Show all contents of the file starting from num line
tail -c num
Show last num bytes of a file
To show the head of the first 8 lines of the combined_names.txt file:
head-c 8 combined_names.txt
Funny-lo
To show all the lines starting from line 8 and beyond:
tail-n +8 combined_names.txt
Trick or Treat
Security Blanket
Masked Marvel
Good Sport
Frisbee Sailing
Warm Puppy
Spring Dance
Sock it to Me
Pumpkin Helmet
Smooth Sidewalk
Full of Ingredients
World-Famous Astronaut
Fire Safety
Wooden Christmas Tree
Very Secure Dishes
Very, Very Secure Dishes
Supposedly Educational
Bug in Your Hair
Sincere Pumpkin Patch
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree
The more command displays text, one screen at a time. It opens a file for
interactive reading
scrolling
searching
Tip
Press space to scroll down the page, the forward slash (/) for searching strings, n to go to the next match, and q to quit.
Command
Description
more
Open a file for interactive reading, scrolling and searching
space
Page down
/
Search for a string; press n to go the next match
q
Quit
The less command is similar to more, but offers more features.
It allows the user to scroll up and down the file, go to the beginning and end of the file, forward and backward search and the ability to go the next and previous match while searching the file.
Command
Description
less
Open a file for interactive reading, scrolling and searching
space
Page down
b
Page up
G
Go to the end of file
g
Go to the start of file
/
Forward search
?
Backward search
n
Go to next match
N
Go to previous match
q
Quit
Sort files
The sort command will sort the contents of text file, line by line. Using additional options, we can
sort a file in ascending/descending order
ignore case while sorting
use numeric order for sorting
preserve only unique lines while sorting
Tip
Using the sort command, the contents can be sorted numerically and alphabetically. By default, the rules for sorting are:
lines starting with a number will appear before lines starting with a letter.
lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.
Using additional options, the rules for sorting can be changed. We list the options in the below table.
Command
Description
sort
Sort lines of text files
sort -r
Sort a file in descending order
sort --ignore-case
Ignore case while sorting
sort -n
Use numeric order for sorting
sort -u
Preserve only unique lines while sorting
Here we are sorting in a descending alphabetical order of the combined_names.txt
sort-r combined_names.txt
You Stupid Darkness
World-Famous Astronaut
Wooden Christmas Tree
Warm Puppy
Very, Very Secure Dishes
Very Secure Dishes
Unsuffered Consequences
Trick or Treat
Supposedly Educational
Spring Dance
Sock it to Me
Smooth Sidewalk
Single Candle
Sincere Pumpkin Patch
Short Summer
Security Blanket
Roasted Marshmallows
Pumpkin Helmet
Masked Marvel
Kite Eating Tree
Great Pumpkin
Good Sport
Gift-Getting Season
Funny-looking Kid
Full of Ingredients
Frisbee Sailing
Fire Safety
Easter Beagle
December Snowflakes
Bug in Your Hair
Another Canoe
Count length of file
wc (word count) will print newline, word, and byte counts for file(s).
wc combined_names.txt
31 75 564 combined_names.txt
wc-l combined_names.txt
31 combined_names.txt
wc-w combined_names.txt
75 combined_names.txt
wc-c combined_names.txt
564 combined_names.txt
If more than one file is specified, it will also print total line.
If you are familiar with regular expressions, you can do cool things like search for a “r” followed by a white space with the \s character set for white spaces.
grep-i'r\s' release_names.txt
December Snowflakes
Easter Beagle
Trick or Treat
Bug in Your Hair
Another Canoe
Short Summer
If there is more than one file to search, use the -H option to print the filename for each match.
In this section, we will learn to execute R commands and scripts in the command line using:
R -e
Rscript -e
R CMD BATCH
The -e option allows us to specify R expression(s).
R -e will launch R and then execute the code specified within quotes.
Use semi-colon to execute multiple expressions as shown below.
You will be able to run the below commands only if you are able to launch R from the command line. (Demo this).
Windows users need to ensure that R is added to the path environment.
R-e"head(mtcars); tail(mtcars)"
R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin21.6.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> head(mtcars); tail(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mpg cyl disp hp drat wt qsec vs am gear carb
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
>
>
We can use Rscript to execute a R script as well. In the below example, we execute the code in analysis.R file (which just asks to print the head of mtcars).
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 -e or R -e to 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
Final Questions
Here are some post-lecture questions to help you think about the material discussed.
Questions
Explore the help files of tar and gzip commands for compressing files.
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 pwd and cd commands, and the different flags for the ls commands.
Practice using “Tab for Auto-complete” in the shell to autocomplete commands or file names.