Terminal commands cheat sheet

Linux file hierarchy

Basics


Commands

Linux file hierarchy
!!Repeat the previous command
↑↓ ([Ctrl] + p, [Ctrl] + n)Search previous commands
[Ctrl] + gCancel the search
historyDisplay the shell history
export HISTSIZE=numberControll the number of command to retain in history (see the default: echo $HISTSIZE)
command -hAsk the command for help
which commandLocation (full path) of the command
man commandManual of the command
[space]Next page
qQuit

Navigating

.Current directory
..Parent directory
pwdDesplay present working directory
cdChange directory to default directory (home)
cd-Change to the previous directory (to see the previous directory: echo $OLDPWD)
cd directoryChange directory to dir

Creating and Removing

mkdir nameCreate a directory
mkdir -p parent_name/child_nameCreate a parent with a child
rmdir nameremove a directory (which has no contents)
rm -rf nameRecursively remove a directory (which has contents)
touch nameCreate a file (if not exist)
rm nameRemove a file

Listing and Sorting

lsList current directory contents (in alphabetic order)
ls directoryList dir contents
ls -aList all contents (including hidden files)
ls -lList contents in a long format
ls -tList by time
ls -rReverse list order
ls -RList with childs (tree: display visualized)
ls -FReveal file types (/: directory, @: link, *: executable)
ls --colorColorize output (done by default in some distros)
sort file_nameSort the contents of a text file, line by line (by alphabet)
sort -r file_nameSort in reverse order
sort -u file_nameSort unique (doesn't show repeated text)

Moving and copying

mv source destinationMove a file/directory (if destination doesn't exist, source will be rename)
cp -r source destinationCopy a file/directory

Reading a file

cat file_nameDisplay the contents of the file
head file_nameDisplay 10 line of the top (-<number> for more or less lines)
tail file_nameDisplay 10 line of the bottom (-<number> for more or less lines)
tail -f file_nameReal time viewing (for log files)

Writing a file

nano file_nameEdit the contents of the file
[^]+[x]Exit
[^]+[o]Save
[^]+[g]Get help

Search in a file

grep pattern file_nameDisplay the lines matching a pattern
-iIgnore upper case and lower case
-cPrecede output with line numbers
-vInvert match (prints lines that don't match)

Compairing files

diff file1 file2Compare two files (or directories contents)
sdiff file1 file2Compare two files side by side
vimdiff file1 file2Highlight 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 file2Archive the files
tar czf name.tar file1 file2Archive the files with compression
tar tf name.tarShow the contents
tar tvf name.tarShow the contents in long format
tar xf name.tarExtract the archive
gzip name.gzZip the files
gunzip name.gzUnzip 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

whoamiDisplay current user
~ + [tab]Display all users
sudo suSwitch to the superuser
sudo commandRun a command as superuser
sudo -s usernameChange user
sudo -u username commandRun a command as another user
groupsdisplays all groups
groups usernameDisplay groups of a user
chgrp group_name file_nameChange groups of a file
chmod [ugoa|+-=|rwx] file_nameChange groups of a file
Linux file hierarchy

Connecting and Transfering

ssh user@hostConnect to host as user
ssh -p port user@hostConnect using special port
sftp user@hostConnect to host for transfering a file
pdwServer pwd
lpdwLocal pwd
put file_nameTransfer the file
quitDisconnect the session
scp filename user@host:pathTransfer 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.

printenvList 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_profilePersist 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'
aliasList aliases
alias [key]="[value]"Create an alias (TZ="US/Pacific")
unalias [key]Remove an alias
echo 'alias [name]="[value]"' >> ~/.bash_profilePersist aliases (they will not be deleted when exiting)

Cron Jobs

Linux file hierarchy
crontab -lList cron jobs
crontab file_nameInstall 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 * * * *