Skip to content

Instantly share code, notes, and snippets.

@adaltavo
Last active February 17, 2018 18:15
Show Gist options
  • Save adaltavo/1ea78fbe78168c987a0205e260f0e140 to your computer and use it in GitHub Desktop.
Save adaltavo/1ea78fbe78168c987a0205e260f0e140 to your computer and use it in GitHub Desktop.
This Linux cheat sheets are intended to give a list of most useful bash commands and utilities. They will be divided as four main sheets: a general list, a list for sys admin, a list for networking, and a list for bash scripting utilities
==============================================Basic Comands===============================================
cat FILE Displays FILE's content (moves FILE content to standard output,
hence, if multiple files are specified, it concatenates them all)
__________________________________________________________________________________________________________
cat > FILE Creates/overrides FILE's content with standard input
__________________________________________________________________________________________________________
cd DIR Moves to DIR directory
__________________________________________________________________________________________________________
cp FILE DEST Copies file FILE to DEST
__________________________________________________________________________________________________________
cp DIR/{FILE1,FILE2,...,FILEN} DEST Copies files from FILE1 to FILEN to DEST
__________________________________________________________________________________________________________
cp -R DIR DEST Copies DIR directory and it's content to DEST
__________________________________________________________________________________________________________
echo TEXT Prints TEXT
__________________________________________________________________________________________________________
find DIR -name FILE -type TYPE Find all the files named like FILE of the type TYPE
(d,c,f,b,p,l,s) from DIR directory and ahead (sublevels, never
goes to upper levels from DIR)
__________________________________________________________________________________________________________
grep PATT FILE Search PATT pattern in FILE and displays all the lines which
contain it. Usually used with a pipe, example: CMD | grep PATT
__________________________________________________________________________________________________________
gzip FILE Compress FILE into FILE.gz; original file IS NOT kept (use
-k flag if you wish to keep it)
__________________________________________________________________________________________________________
gzip -d FILE Decompress FILE (must be a valid gz file). You can use
'gunzip' instead, it will reproduce same result
__________________________________________________________________________________________________________
head FILE Shows first 10 lines of FILE
__________________________________________________________________________________________________________
history Shows your history of used commands
__________________________________________________________________________________________________________
jobs -l Displays current jobs
__________________________________________________________________________________________________________
less FILE Displays FILE's content in a paginted way (is more than more)
__________________________________________________________________________________________________________
ls -alFh DIR Displays contents of DIR directory, including hidden files (a),
With details as a list (l), differentiating regular files from
directories (F), and showing human readable byte amounts (h)
__________________________________________________________________________________________________________
more FILE Displays File's content in a paginated way
__________________________________________________________________________________________________________
mkdir -p DIR/INNERDIR Creates DIR directory and INNERDIR directory (optional)
__________________________________________________________________________________________________________
mv FILE DEST Moves file FILE to DEST
__________________________________________________________________________________________________________
mv DIR/{FILE1,FILE2,...,FILEN} DEST Moves files from FILE1 to FILEN to DEST
__________________________________________________________________________________________________________
mv -R DIR DEST Moves DIR directory and it's content to DEST
__________________________________________________________________________________________________________
pwd Displays current working directory
__________________________________________________________________________________________________________
pushd DIR Pushs DIR directory to a directory stack
__________________________________________________________________________________________________________
popd Pops out an element from the directory stack
__________________________________________________________________________________________________________
rm -R DIR Deletes DIR directory and it's content
__________________________________________________________________________________________________________
rmdir DIR Deletes DIR directory. (MUST BE EMPTY)
__________________________________________________________________________________________________________
rm FILE Deletes FILE file
__________________________________________________________________________________________________________
tar -cvzf TARNAME FILE Compress(-c) and zips(-z) the specified (with -f) FILE file
or directory into the TARNAME file (must be '*.tar.gz' or
'*.tgz'). For just compressing FILE, use -cvf options and use
.tar extension in TARNAME.
NOTE: 'f' option must be in last place
__________________________________________________________________________________________________________
tar -tvf TARNAME Displays(-t) TARNAME's content (must be a valid tar file)
NOTE: 'f' option must be in last place
__________________________________________________________________________________________________________
tar -xvf TARNAME Extracts(-x) TARNAME's content in current directory. If you
wants to extract to a different directory, add
"-C DIR" option, where DIR is a valid directory
NOTE: 'f' option must be in last place
__________________________________________________________________________________________________________
tail FILE Shows last 10 lines of FILE
__________________________________________________________________________________________________________
touch FILE Creates an empty file with FILE name
__________________________________________________________________________________________________________
vim FILE Creates/opens FILE in VIM editor to edit it's content
__________________________________________________________________________________________________________
./FILE Executes FILE
===============================================Admin tools================================================
cut -d "DEL" -f "1,2,...,N" FILE Splits each line of FILE into columns defined by DEL separator
(delimiter), and displaying only the columns specified by -f
parameter
__________________________________________________________________________________________________________
chgrp GRP FILE Changes default group of FILE to GRP
__________________________________________________________________________________________________________
chmod PERM FILE Changes system security permissions of FILE to PERM (bits or
letters)
__________________________________________________________________________________________________________
chown OWN FILE Changes the default owner of FILE to OWN
__________________________________________________________________________________________________________
df -h Lists filesystems information with human readable amounts (-H).
This includes hardrives and external drives (such as USB)
__________________________________________________________________________________________________________
eject /dev/DEVICE Unmounts and eject the specified DEVICE device
__________________________________________________________________________________________________________
env Displays all environment variables
__________________________________________________________________________________________________________
export VAR=HUE Define VAR variable with HUE value and make it part of
environment (just for your current shell and subsequents
subshells)
__________________________________________________________________________________________________________
free -h Shows memory stats with human readable amounts (-h)
__________________________________________________________________________________________________________
groups USER Displays the groups to which USER belongs to (default $USER)
__________________________________________________________________________________________________________
gpasswd -a USER GRP Adds USER user to GRP group
__________________________________________________________________________________________________________
gpasswd -d USER GRP Deletes USER user from GRP group
__________________________________________________________________________________________________________
id USERID Displays user and group information of specified USERID id
(default $UID)
__________________________________________________________________________________________________________
kill -9 PID Sends a SIGKILL signal to the process with the PID id. ( SIGKILL
instantly terminates the process)
__________________________________________________________________________________________________________
killall -9 PNAME Sends a SIGKILL signal to the process with the PNAME name.
(SIGKILL instantly terminates the process)
__________________________________________________________________________________________________________
lsusb Displays information about USB buses and connected USB devices
__________________________________________________________________________________________________________
man CMD Displays manual pages for CMD
__________________________________________________________________________________________________________
passwd USER Changes/sets a new password for USER user
__________________________________________________________________________________________________________
ps -fea Displays current status from all running processes
__________________________________________________________________________________________________________
sed 's/PATT/REP/g' FILE Replaces every instance in FILE that match PATT pattern
(can be a regex) with REP text
__________________________________________________________________________________________________________
shutdown -h TIME Shutdown the computer at TIME (use 'now' for instant shutdown)
__________________________________________________________________________________________________________
shutdown -r TIME Reboot the computer at TIME (use 'now' for instant reboot)
__________________________________________________________________________________________________________
su USER Login as USER (default is 'root' user)
__________________________________________________________________________________________________________
sudo USER CMD Executes CMD as USER (default is 'root' user)
__________________________________________________________________________________________________________
top Displays a real-time stats of the system running information
__________________________________________________________________________________________________________
type CMD Displays where CMD executable file is or if it's a built-in
shell command
__________________________________________________________________________________________________________
useradd -m -U USER Creates a new user named USER, and a group with the same
name (-U). It also creates the user's home directory (-m).
__________________________________________________________________________________________________________
userdel -f USER Deletes USER user, it's mailing pool and it's home directory
__________________________________________________________________________________________________________
usermod -aG GRP USER Adds USER user to GRP group
__________________________________________________________________________________________________________
which CMD Displays where CMD executable file is located
__________________________________________________________________________________________________________
who -H Displays users logged in, with heading information (H)
__________________________________________________________________________________________________________
whoami Displays your username (same as echo $USER)
=============================================Networking tools=============================================
curl -X METH -d "DATA" -H"Head" URL Sends a request to URL using the METH (POST, GET, etc.)
method and the HEAD header, providing DATA as part of the body
__________________________________________________________________________________________________________
iftop -i INTERFACE Shows network stats in specified INTERFACE
__________________________________________________________________________________________________________
lsof -i :PORT Shows real-time process and network information about
the resources using the specified PORT
__________________________________________________________________________________________________________
nethogs INTERFACE Shows real-time network stats about processes using the
specified network interface INTERFACE (default is all of them)
__________________________________________________________________________________________________________
netstat --inet -apn Lists DARPA (ipv4) network connections (--inet), showing all
sockets (-a) used, also displaying it's belonging processes (-p)
without resolving ip directions nor port names (-n)
__________________________________________________________________________________________________________
nmap -sA NETWORK Scans the given NETWORK (example, 192.168.1.0/24) looking for
connected devices
__________________________________________________________________________________________________________
nmcli con add con-name NAME Add a new TYPE (ethernet,wifi,etc.) connection for the specified
ifname INTERFACE type TYPE INTERFACE interface (use * for all), using the given IP/NETMASK
ip4 IP/NETMASK gw4 GATEWAY and the GATEWAY gateway
__________________________________________________________________________________________________________
nmcli dev status Shows network devices status
(nmcli works on Debian and RHEL based)
__________________________________________________________________________________________________________
nmcli con down CONNAME Disconects computer from CONNAME connection
__________________________________________________________________________________________________________
nmcli con mod CONAME PROP NEW Modifies CONAME connection, setting PROP property to NEW value
(to see full list of properties avialable and it's current
values use 'nmcli con show id CONAME' )
__________________________________________________________________________________________________________
nmcli con show Shows avialable network connections (may be down or up)
__________________________________________________________________________________________________________
nmcli con show id CONAME Shows profile information about CONAME connection
__________________________________________________________________________________________________________
nmcli con up CONNAME ifname INTERFACE Connects computer to CONNAME connection using given INTERFACE
(default is predefined interface, hence, can be omitted)
__________________________________________________________________________________________________________
nmcli radio wifi STATE Changes wifi radio status to STATE ('on' or 'off')
__________________________________________________________________________________________________________
nslookup URL Resolves the given URL name into the actual ip address
__________________________________________________________________________________________________________
ping URL Sends an ICMP ECHO_REQUEST to the specified URL to elicit an
ICMP ECHO_RESPONSE from same URL
__________________________________________________________________________________________________________
route -ve Shows ip table with verbose(-v) and extended(-e) information
__________________________________________________________________________________________________________
ssh USER@URL -i PRIVATEKEY Starts a ssh session with the USER user at the given URL server,
using the specified (-i) PRIVATEKEY file
__________________________________________________________________________________________________________
tracepath URL Traces the route between your computer and the URL server
============================================File descriptors==============================================
0 Standard input (stdin). Regularly keyboard
__________________________________________________________________________________________________________
1 Standard output (stdout). Regularly display
__________________________________________________________________________________________________________
2 Standard error (stderr). Regularly display
======================================System security permissions=========================================
a All
__________________________________________________________________________________________________________
g Group
__________________________________________________________________________________________________________
o Others
__________________________________________________________________________________________________________
u User (owner)
__________________________________________________________________________________________________________
r Read ('4' as decimal or '100' as binary)
__________________________________________________________________________________________________________
w Write ('2' as decimal or '010' as binary)
__________________________________________________________________________________________________________
x Execute ('1' as decimal or '001' as binary)
__________________________________________________________________________________________________________
LETTER+PERM Add PERM permission(s) to LETTER group(s) (example, a+rw)
_________________________________________________________________________________________________________
LETTER-PERM Removes PERM permission(s) to LETTER group(s) (example, og-r)
__________________________________________________________________________________________________________
LETTER=PERM Sets PERM permission(s) to LETTER group(s) (example, u=rwx)
===========================================Linux file types===============================================
f ('-') Regular file
__________________________________________________________________________________________________________
d Directory file
__________________________________________________________________________________________________________
s Socket file
__________________________________________________________________________________________________________
p Named pipe file
__________________________________________________________________________________________________________
c Character device file
__________________________________________________________________________________________________________
l Symbolic link file
__________________________________________________________________________________________________________
b block device file
============================================Special operators=============================================
CMD > FILE Redirects standard output stream to FILE
__________________________________________________________________________________________________________
CMD >> FILE Redirects and appends standard output stream to FILE
__________________________________________________________________________________________________________
CMD &> FILE Redirects standard error and output streams to FILE
__________________________________________________________________________________________________________
CMD 2> FILE Redirects standard error stream to FILE
__________________________________________________________________________________________________________
CMD 2>> FILE Redirects and appends standard error to FILE
__________________________________________________________________________________________________________
N>&Y Redirects N stream to Y stream
__________________________________________________________________________________________________________
CMD < FILE Takes FILE as standard input for CMD
__________________________________________________________________________________________________________
CMD << CONTENT Takes CONTENT from standard input for CMD
__________________________________________________________________________________________________________
CMD & Executes CMD as a background process (job)
__________________________________________________________________________________________________________
CMD1 | CMD2 Executes CMD1, then executes CMD2 taking CMD1 output as input
__________________________________________________________________________________________________________
(CMD) Executes CMD in a subshell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment