Show Menu
Cheatography

Linux CLI commands Cheat Sheet (DRAFT) by

Linux command reference detailing commands I require most often.

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

General

lsb_re­lease -a
Linux version
alias
Show aliases attached to shell

Locations

/var/www
Server www sites / pointers to
/var/log
System / auth / php etc. logs
/var/l­og/­nginx/
NGINX access / error logs
~/.ssh
SSH config
/etc/php
PHP version installed

Files

echo "­xyz­" > /somew­her­e/f­ile.log
Write xyz to given file
touch myfile.txt
Create a file
nano myfile.txt
Edit myfile.txt with Nano
rm -rf /node_­modules
Remove node_m­odules recurs­ively
grep -r 'string to search' direct­ory­/to­/search
Search for string in files

File system

ls -al -S
Directory list in file size order
ls -alt
directory list in time order
lsblk
Display volumes / partitions
df -i
inode usage
df -hT
File system memory usage
find / -size +20M
Find large files
find / -type d -name 'httpdocs'
Find folder
find / -name ‘filen­ame.html'
Find file
file -s /dev/xvd*
File system / partition types
growpart /dev/xvda 1
Grow partition to newly added space
resize2fs /dev/xvda1
Resize partition to new allocated size

Permis­sions

chown -R root:www /var/www
Change ownership of /var/www to root user:www group

Networking

ping -c 4 8.8.8.8
Ping (Google) 4 times
ip addr
Network interfaces enabled?
sudo nano /etc/n­etw­ork­/in­ter­faces
Network config
route -n
Show kernal IP routing table
sudo ifdown --force eth0
Network interface off (force optional)
sudo ifup --force eth0
Network interface on (force optional)

PHP

php -m
List installed modules / extensions
php --version
PHP version on CLI

Weblish

CTRL A, Esc
Enter scroll mode (Esc to exit)

User

whoami
Output current user context to CLI
groups
Show which groups user belongs to
adduser ubuntu www-data
Add user ubuntu to group www-data

GIT

git remote --verbose
Show remotes for currently checked out repo
git branch
List local branches
 

Services

sudo service /etc/i­nit.d/­mysqld restart
Restart service (full path)
sudo service nginx restart
Restart service (short)
sudo service --stat­us-all
Service status + running / - stopped / ? services without status command
sudo netstat -tulpn
Display port attach­ments
sudo fuser -k 80/tcp
Kill everything on given port (i.e. release 80 for NGINX restart)

Process and jobs

jobs
List of background jobs
jobs -l
List with PID
fg
Bring most recent to front
fg %2
Switch to specific job in list
If you ctrl-z out of a command line program, such as man, it may leave a running process in the background


[3]+ Stopped man


3 = Number of stopped jobs in background
man = program that was stopped


Find and kill it

Package management

sudo apt-get autoremove
Delete unused packages
sudo apt-get purge php7.*
Remove package
apt list —installed | grep your-p­ack­age­-name
List installed packages
apt-get install your-p­ack­age­-name
Install package
apt-get update
Update packages info about package from repo
apt-get upgrade
Update all packages
apt-get install --only­-up­grade <pa­cka­gen­ame> <an­oth­erp­ack­age­nam­e>
Update specific package
add apt-re­pos­itory name-o­f-y­our­-re­po-here
Add apt repo
apt-cache search packag­e-n­ame­-here
Search for packages
apt-get = older version of apt

MYSQL

apt-get install mysql-­server
Install mysql
mysql_­sec­ure­_in­sta­llation
Secure instal­lation
mysql -u your-u­sername -p
Connect on CLI
CREATE DATABASE databa­se_­nam­e_c­ant­_us­e_h­yph­ens­_us­e_u­nde­rscores CHARACTER SET utf8mb4 COLLATE utf8mb­4_u­nic­ode_ci;
Create DB
SHOW TABLE STATUS;
Show collation of tables
ALTER TABLE <so­me_­tab­le> convert to CHARACTER SET utf8mb4 COLLATE utf8mb­4_u­nic­ode_ci;
Alter table collation
CREATE USER 'newus­er'­@'l­oca­lhost' IDENTIFIED BY 'a_pas­swo­rd_­here';
Create user
GRANT ALL PRIVILEGES ON db_nam­e_here . * TO 'newus­er'­@'l­oca­lhost';
Grant privil­edges
FLUSH PRIVIL­EGES;
Reload privil­edges
Follow https:­//s­tac­kov­erf­low.co­m/a­/42­742610 Option 1 during mysql_­sec­ure­_in­sta­llation if required