Show Menu
Cheatography

Linux Cheat Sheet (DRAFT) by

I'm starting a new job and need to remeber the basics :) Good sources: https://ss64.com/bash/ https://ubuntu.com/server/docs/package-management

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Need help?

man comman­dname
Open the help manual
apropos keyword
Searches the keyword inside the help manual
apropos -a keyword
Searches for multiple keywords inside the help manual
comman­dname --help
Displays help for the command
whatis comman­dname
One line descri­ption of a command
linuxconf
Linux config­uration
aspell
Spell checker
alias
To create simple commands
* Alias example: alias armas ='grep -inT armas Um.txt' --> This defines the command armas to execute the grep command.

* Typing only alias will display the already created commands

Wildcards: (set of characters of arbitrary lenghts); ? (any charac­ter); [numbers] (range of charac­ters)

Basic commands

whoami
To learn your username
clear
Clears the screen
exit
Exits the terminal
echo "­­ra­n­d­om­­wor­­ds­"
Returns what you wrote
expr number + number
To perform basic math operations
date
Dipslays the date and time of the system
nano
Opens nano, the linux text editor
sqlite3
Example of a vesion of sql to use from linux
soofice
See files on Libreo­ffice
gedit
Opens text/code editor Gedit
gedit filename
Opens the file on Gedit
cal
Displays the calender and easter date
seq
Prints a numeral sequence

Permis­sions

ls -l
Displays permis­sions to filesand direct­ories
ls -la
Displays permis­sions to files and direct­ories, including hidden files
chmod
Change permis­sions to files and direct­ories
sudo
Tempor­arily grants elevated permis­sions
sudo useradd
Adds a user to the system
sudo useradd -g groupname username
Sets the user's default group
sudo userass -G group(­s)name username
Adds the user to additional groups
sudo usermod
Modifies existing user accounts
sudo usermod -g
Changes thhe primary group
sudo usermo -G
Changes the secondary group
sudo usermode -d path username
Changes the user's home directory
sudo usermode -l
Changes the user login name
sudo usermod -L
Locks the account
sudo userdel
Deletes user
sudo chown user/g­rou­pname file
Commands changes to the ownership of a file or directory
Permis­sions in Linux look like drwxrw­xrwx. 1st char is a d (for directory) or - (for regular files). The 3 sets refer to user-g­rou­p-o­the­r.The options for characters are: u (indicats changes will be made to user permis­sions); g (indicates changes will be made to the group permis­sions), o (indicates changes will be made to other permis­sions); + (adds permis­sions to them); - (removes permis­sions from them); = (assigns permis­sions to them)
 

Direct­ories & Files

tree
Lists the directory conten on a tree format
pwd
Prints the current working directory path
ls
Displays the names of the direct­ories and files in the current directory
ls -a
Displays hidden files
cd direct­oryname
Changes directory
cd /
Back to the root directory
cd ~
Changes to the home directory
cd ..
Changes to the preceding directory
mkdir newdir­ect­oryname
Create new direct­ories
touch
Creates a new empty file
rmdir direco­tyname
Delete direct­ories
rmdir -r
Deletes the directory and files
rm filename
Deletes a file
cp [options] origin destiny
Copie files and direct­ories
mv file/d­ire­cto­ryname /path
Moves direct­ories and files to a new location. Can also be used to rename files.
tar -xf filename
Extracts files
stat filename
Displays info about an file
stat -f
Displays info about the system file (not the file)
find filename
To find a file
- Home directory: the one in which the session is started after login.
- Working directory: the one being used at the moment.
-Root directory: mother directory, first of the tree.

* " " are needed to have spaces on the directory name

*rmdir only works in empty direct­ories

*hidden files will have an . (dot) at the beggining

* stat can be used with multiple files (stat filename filename)

Processes

top
Shows the executing processes on Linux
ps
Shows the active exeuting processes on the machine
ps -e
Lists all the processes in order
ps -f
Shows the same as ps but in a table
ps -e-f
Shows the processes in order, on a table
ps tree
Shows the processes in tree
kill
Ends the process
kill -9
Is used when kill doesn't work
kill -1
Restarts the process
* kill -9 and kill -1 can be combined (kill -9-1)

Networks

ping
Verify if an server is available
traceroute
Shows the network route
nslookup
Lets you find info about the computer in the network through DNS
telnet
Log in securely on a remote computer
ssh -X
Log in securely on a remote computer
Xfce4-­session
Start an graphical session on a remote computer
exit
Leave the remote session
quit
Leave the remote session
who
Shows who has an active session on this machine
who -w
Know some more info
finger
Shows info about an user
write username
Send messages to other users
talk
Also used to commun­ciate with other users. In this case, thy msut accept the connection with the same command
mesg n
Indicates that I don't want to receive more messages
mesg y
Indicates that I'm available
at
Schedules the execution of commands on the computer
scp
Securely copie files between different machines
- ping -c 4 apps .fe.up.pt
- nslookup moodle.up.pt or nslookup 193.13­7.3­5.211
- telnet towel.c­li­nke­nli­ght­s.nl.
-ssh -X gnomo.f­e.u­p-pt or ssh -X mccart­hy.f­e.u­p.pt
- at 11:15 comman­dst­obe­exe­cuted ctrl+d
-scp user@m­cca­rth­y.f­e.up.pt:/usr/­SCC­OM/­ola.txt copiad­o.txt
*when using write, use ctrl+c to send the message or use echo mensagem | write username
 

Reading files content

cat filename
Concat­enate and display the content of files
tac
Like cat but in reverse
grep filename
Returns all lines of a file that contain a specified string
head filename
Dispalys the first 10 lines of the file
head -number
Displays the n first lines
tail filename
Displays the last 10 lines
tail -number filename
Displays the last n number of lines
less
Displays the content of a file one page at a time
more filename
Displays the file
wc
Counts charac­ters, words and lines
wc -c
Counts bytes
wc -m
Counts characters
wc -w
Counts words
nl
counts the number of lines
nl -a
numbers
sort
reorga­nizes the file by lines, so that they are ordered by number and alphab­eticaly
sort -r
like sort but in reverse
grep -n
Prints all lines according to the condition and numerates them
grep -t
All the lines will start on the same place
grep -i
Looks for the condition, ignoring the letter capita­liz­ation
grep -w
searches only for the complete word and not for words that contain part of the chosen word

Joining commands

There are several ways to combine commands on a single line.

Running commands one after the other: ; or && or ||.

&& If the command that preeceds && completes sccess­fully, the following is run.

|| If the command that precedes || fails, the following runs.


Run more than one command concur­rently (the output from the first command is piped to the next as the first command is runnign): | or a filter with a |
------­---­---­---­---­---­---­---­---­---­---­---­-------
Using ; : cd mydir ; ls

Using && : command && command

Using || : command || command

Using | : history | grep "­cp" (displays all the cp commands that are recorded among the 16 most recently recorded command in history file). ls -l | grep "­Jan­" (displays the files that were last changed in January). ps -e | grep cc | wc -l (lists all o the processes active in the system and pipes the output to grep, which searches for every instace of the string cc. The output of the grep is then piped to wc, which counts every line in which the string cc occurs and sends the number of lines to standard output).

------­---­---­---­---­---­---­---­---­---­---­---­------

> : redirects the output of a command to an file (the result of the command will appear on the file and not on the shell. When there's content on the file, it will repace it. When there's no file it will create a new one).

>>> : adds to the file

Admini­str­ation

apt
Search for and install software packages
apt-get
Search for and install software packages
apt remove
Removes a package
apt upgrade
updats the whole system
apt update
Upgrades the repository
- It might be necessary to use sudo to use apt