Linux penguin on a yellow backdrop

My Linux Cheat Sheet

I frequently use Ubuntu or Debian to run my applications. Here is my cheat sheet of how to perform commonly required tasks on these Linux distributions.

Managing Packages

Get ‘add-apt-repository’ command back on Debian 8

sudo apt-get install -y software-properties-common

Search for installed packages

sudo dpkg -l | grep 

# Example of finding packages containing mysql in the name
sudo dpkg -l | grep mysql

Completely remove a package (with all configuration files)

sudo apt-get --purge remove 

# Example removing a package called mysql-core
sudo apt-get --purge remove mysql-score

Update packages

sudo apt-get update
sudo apt-get upgrade

 

Files and directories

Create directory including all sub directories in path

[bash]



[/bash]
mkdir -p <paths to create directories for>

# Example:
mkdir -p /usr/some/directory/with/subdirectories

Configure vim editor to use 4 character tab spaces for Python development

Open /etc/vim/vimrc and add the following lines:

set tabstop=4
set shiftwidth=4
set expandtab

Locate files that are taking up disk space

# Install ncdu (an awesome tool)
sudo apt-get install -y ncdu

# Find all large files/directories
sudo ncdu /
Screenshot of NCDU

Create a symbolic link (especially useful for adding nginx enabled-sites)

ln -s
# Example adding a symbolid link in sites-enabled to a file to sites-available
ln -s /etc/nginx/sites-available/www-mysite /etc/nginx/sites/enabled/www-mysite

Delete all files with a specific extension

# First run the below to see what will be deleted
find . -name "*." -type f
# Then run this to delete
find . -name "*." -type f -delete

# Example for finding and deleting all files with .pyc extension
find . -name "*.pyc" -type f
find . -name "*.pyc" -type f -delete

 

Users, groups and security

Create a new user

sudo adduser
# Example of creating a new user called 'mark'
sudo adduser mark

Create a new group

sudo addgroup
# Example of adding a new group called 'kaizen'
sudo addgroup kaizen

Add a user to an existing group

usermod -a -G
# Example adding a user 'mark' to group 'sudo'
usermod -a -G sudo mark

Generate SSH ed25519 private key

 ssh-keygen -t ed25519 -C "email@example.com"

General Admin/Control

Change the hostname

echo new-hostname > /etc/hostname
/etc/init.d/hostname restart

Check distribution and version installed

lsb_release -a

Shutdown the system

sudo shutdown -h now

Reboot the system

sudo shutdown -r now

Find a kill a process

Note, you find the pid using the first command, then kill it using the second.

# Find the process
ps axf | grep
kill -9 ID

Find open ports

sudo netstat -plnt