Terminal commands cheat sheet
Basics
- Commands are case sensitive.
- Pressing the Tab key helps with autocompletion.
- / (slash): directory separator.
- ~ (tilde): home directory.
- | (pipe): to chain two commands together (if the first command has an error it doesn't go for the second.
- Command options can be combined. (ls -l -a = ls -la)
- Use quotes ("my notes.txt" or 'my notes.txt') or a backslash (my\ notes.txt) to escape spaces.
- The clear command, or pressing Ctrl + L, clears the terminal screen of all previous commands and outputs, giving you a fresh view.
Commands
!! | Repeat the previous command |
↑↓ ([Ctrl] + p, [Ctrl] + n) | Search previous commands |
[Ctrl] + g | Cancel the search |
history | Display the shell history |
Controll the number of command to retain in history (see the default: echo $HISTSIZE) | |
command -h | Ask the command for help |
which command | Location (full path) of the command |
man command | Manual of the command |
[space] | Next page |
q | Quit |
Navigating
. | Current directory |
.. | Parent directory |
pwd | Desplay present working directory |
cd | Change directory to default directory (home) |
cd- | Change to the previous directory (to see the previous directory: echo $OLDPWD) |
cd directory | Change directory to dir |
Creating and Removing
mkdir name | Create a directory |
mkdir -p parent_name/child_name | Create a parent with a child |
rmdir name | remove a directory (which has no contents) |
rm -rf name | Recursively remove a directory (which has contents) |
touch name | Create a file (if not exist) |
rm name | Remove a file |
Listing and Sorting
ls | List current directory contents (in alphabetic order) |
ls directory | List dir contents |
ls -a | List all contents (including hidden files) |
ls -l | List contents in a long format |
ls -t | List by time |
ls -r | Reverse list order |
ls -R | List with childs (tree: display visualized) |
ls -F | Reveal file types (/: directory, @: link, *: executable) |
ls --color | Colorize output (done by default in some distros) |
sort file_name | Sort the contents of a text file, line by line (by alphabet) |
sort -r file_name | Sort in reverse order |
sort -u file_name | Sort unique (doesn't show repeated text) |
Moving and copying
mv source destination | Move a file/directory (if destination doesn't exist, source will be rename) |
cp -r source destination | Copy a file/directory |
Reading a file
cat file_name | Display the contents of the file |
head file_name | Display 10 line of the top (-<number> for more or less lines) |
tail file_name | Display 10 line of the bottom (-<number> for more or less lines) |
tail -f file_name | Real time viewing (for log files) |
Writing a file
nano file_name | Edit the contents of the file |
[^]+[x] | Exit |
[^]+[o] | Save |
[^]+[g] | Get help |
Search in a file
grep pattern file_name | Display the lines matching a pattern |
-i | Ignore upper case and lower case |
-c | Precede output with line numbers |
-v | Invert match (prints lines that don't match) |
Compairing files
diff file1 file2 | Compare two files (or directories contents) |
sdiff file1 file2 | Compare two files side by side |
vimdiff file1 file2 | Highlight differences using vim |
[ctr]+[ww] | Move between windows |
[:]+[qa] | Close all |
Compressing files
du -h | Display file size (human readable) |
tar cf name.tar file1 file2 | Archive the files |
tar czf name.tar file1 file2 | Archive the files with compression |
tar tf name.tar | Show the contents |
tar tvf name.tar | Show the contents in long format |
tar xf name.tar | Extract the archive |
gzip name.gz | Zip the files |
gunzip name.gz | Unzip the files |
I/O
> | Redirect outputs to a file - overwrites the contents if the file exist (ls -l > file.txt) |
>> | Redirect outputs to a file - appends the contents if the file exist |
< | Redirect inputs from a file to a command (sort < file.txt) |
Whildcards
* | Match zero or more characters (*.txt - a* - a*txt) |
? | Match exactly one character (?.txt - a? - a?txt) |
[] | Match any of the characters included between the brackets (ca[nt]* -> can, cat, catch) |
[!] | Match any of the characters NOT included between the brackets |
[a-g] | Get range (it can include numbers -> [2-8]) |
[[:alpha:]] | Match alphabetic letters (others: digit, alnum, lower, upper, space) |
َUsers and Groups
whoami | Display current user |
~ + [tab] | Display all users |
sudo su | Switch to the superuser |
sudo command | Run a command as superuser |
sudo -s username | Change user |
sudo -u username command | Run a command as another user |
groups | displays all groups |
groups username | Display groups of a user |
chgrp group_name file_name | Change groups of a file |
chmod [ugoa|+-=|rwx] file_name | Change groups of a file |
Connecting and Transfering
ssh user@host | Connect to host as user |
ssh -p port user@host | Connect using special port |
sftp user@host | Connect to host for transfering a file |
pdw | Server pwd |
lpdw | Local pwd |
put file_name | Transfer the file |
quit | Disconnect the session |
scp filename user@host:path | Transfer a file without a session |
Environment Variable
Environment Variables are key-value pairs configured outside your source code so that each value can change depending on the Environment. Your source code can read these values to change behavior during the Build Step or during Function execution.
printenv | List environment variables |
export [key]="[value]" | Create/Update environment variables (TZ="US/Pacific") |
echo $[key] | View an environment variable |
unset [key] | Remove an environment variable |
echo 'export [key]="[value]"' >> ~/.bash_profile | Persist env changes (they will not be deleted when exiting) |
Aliases
- Shortcuts: alias ll='ls -l'
- Fix typos: alias grpe='grep'
- Change behave like another os: alias cls='clear'
alias | List aliases |
alias [key]="[value]" | Create an alias (TZ="US/Pacific") |
unalias [key] | Remove an alias |
echo 'alias [name]="[value]"' >> ~/.bash_profile | Persist aliases (they will not be deleted when exiting) |
Cron Jobs
crontab -l | List cron jobs |
crontab file_name | Install crontabs from a file |
crontab -r | Remove all cron jobs |
- Daily: 0 0 * * *
- Weekly: 0 0 * * 0
- Monthly: 0 0 1 * *
- Annually: 0 0 1 1 *
- run every Monday at 7 pm and send output to a log file: 0 7 * * 1 /root/backupdb > /tmp/db.log 2>&1
- run every 15 minutes: 0,15,30,45 * * * * or */15 * * * *