No matter how long you try to avoid it, eventually you’ll find things that can only be solved (or that can be much more quickly solved) using the Command Line Interface (CLI).
Things like:
git
is actually easier on the Command Line.docker
, GDAL
, proj4/6
).A lot of this ties back to data and servers.
This command does four things in one ‘line’ on the CLI:
curl
downloads the file and passes the contents to…head
which takes the first three rows and passes those to…awk
which splits the rows on ","
and takes the 2nd, 4th, and 6th fields and directs them into…results.txt
Command | Does | Example |
---|---|---|
ls |
List | ls . |
cd |
Change Directory | cd $HOME or cd ~ |
pwd |
Print Working Directory | pwd |
mv |
Rename/Move file a to b |
mv a.txt b.txt |
find |
Find files matching some criteria | find . -name "*.md" |
Shortcut | Means | Example |
---|---|---|
. |
The current working directory | ls . |
.. |
The directory above the current working one | cd .. |
~ 1 |
The current user’s home directory. | cd ~ |
/ |
The ‘root’ directory for the entire computer | ls / |
"*" |
A ‘wildcard’ meaning any number of characters in a filename | find . -name "*.md" |
"?" |
A ‘wildcard’ meaning one character in a filename | find . -name "2.?-*.md" |
Command | Does | Example |
---|---|---|
less |
Peek at contents of a text file | less file.txt |
grep |
Find lines matching a ‘pattern’ in a file | grep 'pattern' file.txt |
head |
Peek at first x rows of a text file |
head -n 10 file.txt |
tail |
Peek at last x rows of a text file |
tail -n 10 file.txt |
wc |
Count things (rows, words, etc.) | wc -l file.txt |
sed /awk |
Complicated, but powerful, things | awk -F"," '{ print $1, $3; }' file.csv |
Some characters are ‘special’ and need to be escaped. You’ll encounter these both in the shell (a.k.a. command line) and in Python:
Escape | Does | Example |
---|---|---|
\ |
Allows spaces in file names | less My\ File\ with\ Spaces.txt |
\t |
Creates/matches a tab character | \tThe start of a paragraph... |
\n |
Creates/matches a newline character | The end of a row/para...\n |
\r |
Creates/matches a carriage return | The end of a row/para...\r\n |
\$ |
Literal dollar sign (since $ often marks a variable) |
It costs \$1,000,000 |
\! |
Literal exclamation mark (since ! can mean a number of things) |
Don't forget me\! |
This also becomes relevant when you’re dealing with quotes:
vs.
Command | Does | Example |
---|---|---|
gzip |
Compress/Decompress files | gzip file.txt |
gunzip |
Decompress files | gunzip file.txt.gz 1 |
The CLI becomes much useful with command chaining:
The ‘pipe’ (|
) takes output from command and ‘pipes’ (aka. passes) it to another.
We can redirect outputs in to new files with >
, and inputs out of existing files using <
:
So the output from the previous commands goes into matches.txt
as plain-text. The reverse <
is only used in very special circumstances so you probably won’t encounter it very often.
Most developers will use one or more of these on a daily basis:
I do not expect you to understand this, but I do want you to understand why this is important:
The Software Carpentry people have a whole set of lessons around working with ‘the shell’ (a.k.a. Command Line) that might help you.
Indeed all of MIT’s Missing Semester content could be useful!
The Shell/Terminal in general:
And lots more here on using the file system and shell commands
The Command Line • Jon Reades