Terminal Crash Course and Reference Sheet

January 13th, 2013

I have been using linux for five or six years and I’m wrapping up my last year of college. I noticed that a lot of first year students and people starting to use linux have trouble getting around the terminal. Most of the time I’m only using a handful of commands and operations. I thought it might be helpful to share some basic usage of these commands and other tricks, so I wrote a short guide and reference sheet that I wish someone had given me when I started using linux. I’m using Ubuntu and Bash 4.2.8, YMMV

Directories:

. Current directory

.. Parent directory

- Directory that you were at last ex:

$ cd ~/Downloads
$ cd ~/Documents
$ cd -
[now you are back in Downloads]

~ Home directory, contains personal files, equivalent to /home/[your username]/

/ Root directory, the parent directory for all files

Navigation

cd Choose Directory - move to a given directory
ex: $ cd ~/Desktop

ls List files in the current directory
Options:
-a list all files (some files are hidden otherwise)
-l list files with metadata like permissions, timestamp and size

pwd Prints the absolute path of the directory thta you are in

mkdir [new dir name] Creates a new directory with a relative or absolute path

System Management

ps Lists processes, ps aux prints all processes that the current user can see and should be used in most cases

top Shows system(CPU, mem, processes) status dynamically. Hit q key to exit.

kill Sends a signal to a process, usually to terminate it. Takes in a pid(can be obtained from ps) as an argument.

free Lists memory usage stats -m displays numbers in Mb and can be easier to read

df Lists hard disk usage stats (also takes -m option)

Files

cat Prints out a file in the terminal

rm Removes a file
Options:
-r Recursivly remove files (use for directories)
-f Force removing files, ie the user isn’t prompted and a warning isnt given if the file does not exist
BE CAREFUL WITH RM, especially -with -r and -f!

mv [file] [destination] Moves a file

cp [file] [destination] Copies a file

ln Make a link(alias) to a file - you can open/read/edit the file using the link

chmod [permission] [file] Changes file permissions/mode. More info

tail Prints the last 10 or -# # of lines of a file

head Prints the first 10 or -# # of lines of a file

diff Shows differences between two files

emacs/vim/nano Text editors. I personally like VIM, but everyone has their preference and VIM has a decent learning curve.

Misc

man [command name] IMPORTANT Shows the manual page for a given command

grep [pattern] [filename] Searches a file for a regex or text pattern

time [command] Runs a command and displays the time it takes to execute

history Prints previously run commands

wget [URL] Downloads the file at the given URL

Complicating Things…

Shortcuts

up/down arrow keys Toggles through previously run terminal commands

ctrl+c Attempts to shutdown the process that is running in the terminal (sends a SIGINT)

ctrl+z Brings the process that is running in the terminal to the background(sends a SIGSTOP), can be brought back to foreground with fg

tab Autocompletes a file or command name in the terminal. Will only fully autocomplete if there is one option or will complete partially if all possibilities share part of their name.

tab *twice* Shows options to finish command or filename

Combining terminal commands

[command one] && [command two] Runs command one, then runs command two
ex: mkdir newDir && cd newDir to make a directory and navigate into it

[command one] | [command two] Pipe: Uses the output from command one as input for command two
ex: history | grep git to search history for when git commands were run

Redirection

[command] > [file] Redirect out: Instead of printing to the terminal, prints output in a file

[command] < [file] Redirect in: Uses input from a file instead of the terminal

Bang!

!! Run the last command that was executed

!# Runs the command with a given line # that can be found from history

![pattern] Runs the last command you executed that started with ‘pattern’

I hope this helps!