Bash Commands Cheat Sheet

Quick reference Bash commands you should know.

Shortcuts And History
CTRL+A
move to beginning of line
CTRL+B
moves backward one character
CTRL+C
halts the current command
CTRL+D
deletes one character backward or logs out of current session, similar to exit
CTRL+E
moves to end of line
CTRL+F
moves forward one character
CTRL+G
aborts the current editing command and ring the terminal bell
CTRL+H
deletes one character under cursor (same as DELETE)
CTRL+J
same as RETURN
CTRL+K
deletes (kill) forward to end of line
CTRL+L
clears screen and redisplay the line
CTRL+M
same as RETURN
CTRL+N
next line in command history
CTRL+O
same as RETURN, then displays next line in history file
CTRL+P
previous line in command history
CTRL+Q
resumes suspended shell output
CTRL+R
searches backward
CTRL+S
searches forward or suspends shell output
CTRL+T
transposes two characters
CTRL+U
kills backward from point to the beginning of line
CTRL+V
makes the next character typed verbatim
CTRL+W
kills the word behind the cursor
CTRL+X
lists the possible filename completions of the current word
CTRL+Y
retrieves (yank) last item killed
CTRL+Z
stops the current command, resume with fg in the foreground or bg in the background
ALT+B
moves backward one word
ALT+D
deletes next word
ALT+F
moves forward one word
ALT+H
deletes one character backward
ALT+T
transposes two words
ALT+.
pastes last word from the last command. Pressing it repeatedly traverses through command history.
ALT+U
capitalizes every character from the current cursor position to the end of the word
ALT+L
uncapitalizes every character from the current cursor position to the end of the word
ALT+C
capitalizes the letter under the cursor. The cursor then moves to the end of the word.
ALT+R
reverts any changes to a command you’ve pulled from your history if you’ve edited it.
ALT+?
list possible completions to what is typed
ALT+^
expand line to most recent match from history
CTRL+X then (
start recording a keyboard macro
CTRL+X then )
finish recording keyboard macro
CTRL+X then E
recall last recorded keyboard macro
CTRL+X then CTRL+E
invoke text editor (specified by $EDITOR) on current command line then execute resultes as shell commands
CTRL+A then D
logout from screen but don't kill it, if any command exist, it will continue
BACKSPACE
deletes one character backward
DELETE
deletes one character under cursor
history
shows command line history
!!
repeats the last command
!<n>
refers to command line 'n'
!<string>
refers to command starting with 'string'
esc :wq
exits and saves script
exit
logs out of current session
Bash Basics
env
displays all environment variables
echo $SHELL
displays the shell you're using
echo $BASH_VERSION
displays bash version
bash
if you want to use bash (type exit to go back to your previously opened shell)
whereis bash
locates the binary, source and manual-page for a command
which bash
finds out which program is executed as 'bash' (default: /bin/bash, can change across environments)
clear
clears content on window (hide displayed lines)
Variables
varname=value
defines a variable
varname=value command
defines a variable to be in the environment of a particular subprocess
echo $varname
checks a variable's value
echo $$
prints process ID of the current shell
echo $!
prints process ID of the most recently invoked background job
echo $?
displays the exit status of the last command
read <varname>
reads a string from the input and assigns it to a variable
read -p "prompt" <varname>
same as above but outputs a prompt to ask user for value
column -t <filename>
display info in pretty columns (often used with pipe)
let <varname> = <equation>
performs mathematical calculation using operators like +, -, *, /, %
export VARNAME=value
defines an environment variable (will be available in subprocesses)
export -f<funcname>
Exports function 'funcname'
export var1="var1 value"
Export and assign in the same statement
export <varname>
Copy Bash variable
declare -x <varname>
Copy Bash variable
array[0]=valA
how to define an array
array[1]=valB
array[2]=valC
array=([2]=valC [0]=valA [1]=valB)
another way
array=(valA valB valC)
and another
${array[i]}
displays array's value for this index. If no index is supplied, array element 0 is assumed
${ #array[i]}
to find out the length of any element in the array
File Commands
ls
lists your files in current directory,
ls <dir> to print files in a specific directory
ls -l
lists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified
ls -a
lists all files in 'long format', including hidden files (name beginning with '.')
ln -s <filename>
creates symbolic link to file
readlink <filename>
shows where a symbolic links points to
tree
show directories and subdirectories in easilly readable file tree
mc
terminal file explorer (alternative to ncdu)
touch <filename>
creates or updates (edit) your file
mktemp -t <filename>
make a temp file in /tmp/ which is deleted at next boot (-d to make directory)
cat <filename>
displays file raw content (will not be interpreted)
cat -n <filename>
shows number of lines
nl <file.sh>
shows number of lines in file
cat filename1 > filename2
Copy filename1 to filename2
cat filename1 >> filename2
merge two files texts together
any_command > <filename>
'>' is used to perform redirections, it will set any_command's stdout to file instead of "real stdout" (generally /dev/stdout)
more <filename>
shows the first part of a file (move with space and type q to quit)
head <filename>
outputs the first lines of file (default: 10 lines)
tail <filename>
outputs the last lines of file (useful with -f option) (default: 10 lines)
vim <filename>
opens a file in VIM (VI iMproved) text editor, will create it if it doesn't exist
mv <filename1> <dest>
moves a file to destination, behavior will change based on 'dest' type (dir: file is placed into dir; file: file will replace dest (tip: useful for renaming))
cp <filename1> <dest>
copies a file
rm <filename>
removes a file
find . -name <name> <type>
searches for a file or a directory in the current directory and all its sub-directories by its name
diff <filename1> <filename2>
compares files, and shows where they differ
wc <filename>
tells you how many lines, words and characters there are in a file. Use -lwc (lines, word, character) to ouput only 1 of those informations
sort <filename>
sorts the contents of a text file line by line in alphabetical order, use -n for numeric sort and -r for reversing order.
sort -t -k <filename>
sorts the contents on specific sort key field starting from 1, using the field separator t.
rev
reverse string characters (hello becomes olleh)
chmod -options <filename>
lets you change the read, write, and execute permissions on your files (more infos: SUID, GUID)
gzip <filename>
compresses files using gzip algorithm
gunzip <filename>
uncompresses files compressed by gzip
gzcat <filename>
lets you look at gzipped file without actually having to gunzip it
lpr <filename>
prints the file
lpq
checks out the printer queue
lprm <jobnumber>
removes something from the printer queue
genscript
converts plain text files into postscript for printing and gives you some options for formatting
dvips <filename>
prints .dvi files (i.e. files produced by LaTeX)
grep <pattern> <filenames>
looks for the string in the files
grep -r <pattern> <dir>
search recursively for pattern in directory
head -n file_name | tail +n
Print nth line from file.
head -y lines.txt | tail +x
want to display all the lines from x to y. This includes the xth and yth lines.
Directory Commands
mkdir <dirname>
makes a new directory
rmdir <dirname>
remove an empty directory
rmdir -rf <dirname>
remove a non-empty directory
mv <dir1> <dir2>
rename a directory from <dir1> to <dir2>
cd
changes to home
cd ..
changes to the parent directory
cd <dirname>
changes directory
cp -r <dir1> <dir2>
copy <dir1> into <dir2> including sub-directories
pwd
tells you where you currently are
cd ~
changes to home.
cd -
changes to previous working directory
SSH, System Info & Network Commands
ssh user@host
connects to host as user
ssh -p <port> user@host
connects to host on specified port as user
ssh-copy-id user@host
adds your ssh key to host for user to enable a keyed or passwordless login
whoami
returns your username
su <user>
switch to a different user
su -
switch to root, likely needs to be sudo su -
sudo <command>
execute command as the root user
passwd
lets you change your password
quota -v
shows what your disk quota is
date
shows the current date and time
cal
shows the month's calendar
uptime
shows current uptime
w
displays whois online
finger <user>
displays information about user
uname -a
shows kernel information
man <command>
shows the manual for specified command
info <command>
shows another documentation system for the specific command
help
shows documentation about built-in commands and functions
df
shows disk usage
du <filename>
shows the disk usage of the files and directories in filename (du -s give only a total)
resize2fs
ext2/ext3/ext4 file system resizer
last <yourUsername>
lists your last logins
ps -u yourusername
lists your processes
kill <PID>
kills the processes with the ID you gave
killall <processname>
kill all processes with the name
top
displays your currently active processes
lsof
lists open files
bg
lists stopped or background jobs ; resume a stopped job in the background
fg
brings the most recent job in the foreground
fg <job>
brings job to the foreground
ping <host>
pings host and outputs results
whois <domain>
gets whois information for domain
dig <domain>
gets DNS information for domain
dig -x <host>
reverses lookup host
wget <file>
downloads file
netstat
Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
time <command>
report time consumed by command execution
Try These Related Tools



Web Services are like teenage sex. Everyone is talking about doing it, and those who are actually doing it are doing it badly.

Michelle Bustamante