Shell Reference

Basics

. a single period refers to the current directory
.. a double period refers to the directory immediately above the current directory
~ refers to your home directory. Note: this command does NOT work on Windows machines (Mac and Linux are okay)
cd ./dirname changes the current directory to the directory dirname
ls -F tells you what files and directories are in the current directory
pwd tells you what directory you are in (pwd stands for print working directory)
history lists previous commands you have entered. history | less lets you page through the list.
man cmd displays the manual page for a command.

Creating and Deleting

mkdir ./dirname makes a new directory called dirname below the current directory. Note: Windows users will need to use \ instead of / for the path separator
rm ./filename deletes a file called filename from the current directory
rmdir ./dirname deletes the directory dirname from the current directory. Note: dirname must be empty for rmdir to run.
mv tmp/filename . moves the file filename from the directory tmp to the current directory. Note: (i) the original filename in tmp is deleted. (ii) mv can also be used to rename files (e.g., mv filename newname
cp tmp/filename . copies the file filename from the directory tmp to the current directory. Note: (i) the original file is still there

Wildcards

* zero or more characters
? exactly one character
[abcde] exactly one of the characters listed
[a-e] exactly one character in the given range
{software,carpentry} exactly one entire word from the options given

Pipes and Redirection

> write stdout to a new file; overwrites any file with that name (e.g., ls *.md > mardkownfiles.txt)
>> append stdout to a previously existing file; if the file does not exist, it is created (e.g., ls *.md >> markdownfiles.txt)
< assigns the information in a file to a variable, loop, etc (e.g., n < markdownfiles.md)
| Output from one command line program can be used as input to another one (e.g. ls *.md | head gives you the first 5 *.md files in your directory)

Matching Lines

grep [options] day haiku.txt finds every instance of the string day in the file haiku.txt and pipes it to standard output
-E tells grep you will be using a regular expression. Enclose the regular expression in quotes.
-i makes matching case-insensitive
-n limits the number of lines that match to the first n matches
-v shows lines that do not match the pattern (inverts the match)
-w outputs instances where the pattern is a whole word

Finding Files

find start [options] [commands] look for files or directories with specified properties and take specified actions
-type d match directories
-type f match files
-name 'pattern' match things with specific names
-maxdepth n maximum search depth
-mindepth n minimum search depth
-print print matches

Repeat Operations

for filename in *.txt
do
    head -5 $(filename) | sort | uniq -c
done

Save Recent Commands

history | tail -20 > recent-commands.sh