Command Reference
Note Don’t type the
[]
brackets when using the command -cd [dirname]
means you’ll typecd Documents
.
Being lazy
- Press ↑ and ↓ to scroll through previous commands. It can save you a lot of typing.
- Instead of typing out a long file/directory name, type the first few letters and then press tab. It will automatically fill in the rest of the filename. Use this every time you’re typing anything on the command line.
- On a mac, you can drag a file or folder from Finder onto the Terminal and it will automatically type the full path
Navigating your directories/folders
pwd
: prints working directory — use this to check that you’re in the directory you wantcd [dirname]
: changes directory, moves you into the folder nameddirname
cd ..
: moves you up one directory, e.g. fromDesktop/foundations/class-1
toDesktop/foundations
.ls
: lists subdirectories (and files).ls -lah
is nicer, though.locate [filename]
: tries to find a file calledfilename
and tell you where it’s at. It uses a database that isn’t updated too often, so it can’t always find new files.find
: tries to find a file without waiting for the database to update - can search by name, size, date modified, etc. This is a good reference
Moving/editing/downloading files
mv [source] [destination]
moves a file from one place to anothercp [source] [destination]
copies a file from one place to another (so now you have two)rm [file]
deletes (removes) a filemkdir [directoryname]
creates a directoryrmdir [directoryname]
deletes (removes) a directorytar cvf [filename] [filename] [filename]...
compresses (zips) files up into a.tar.gz
filetar xvf [filename]
extracts (unzips) a.tar.gz
filecurl [url]
downloads a file, but streams it into your terminal windowcurl -O [url]
downloads a file, saving itwget [url]
downloads a file, saving it (yes,curl
andwget
are pretty similar)
Looking at individual files
cat [filename]
: displays the contents of a filewc [filename]
: displays the word count of a filewc -l [filename]
: displays the line count of a filehead -n 10 [filename]
: displays the first 10 lines of a filetail -n 20 [filename]
: displays the last 20 lines of a filemore [filename]
: displays the contents of a file one screen at a time (spacebar to continue)grep [text] [filename]
: show all of the lines infilename
that containtext
.sort
: sorts the lines of a fileuniq
: removes duplicate adjacent lines of a file
Amusing ones to look up
banner
cowsay
say
Others you might find useful if you love the command line
sed
awk
vi
/vim
Escaping commands
Has something gone wrong? Taking too long? Hold down Control
and hit C
, a.k.a. Ctrl+C
.
Learning more/finding more
man
pages are manuals that you can use from the command line.
man grep
would give you the entry for grep, and then you’d use the d
and u
to navigate up and down. You should probably just google grep man page or grep examples, though.