Kernel

uname -r // kernel
lsb_release -a // print linux version
cat /etc/*-release // print linux version
cat /proc/version // print linux version

Package Managers

apt-get (Debian)

nano /etc/apt/sources.list // Add packages to list / check where updates coming from
apt-get install [package]
apt-get [—purge] remove [package]
apt-get autoremove
apt-get clean
apt-cache show [package] // show package details

sudo apt-get update
sudo apt-get dist-upgrade // upgrade os version

find / -name [search query] // starts from /, -name means filename rather than any other attribute

Build tools

sudo apt-get install build-essential
sudo apt-get install python-software-properties python g++ make

Homebrew (OSX)

brew tap // list tapped upstream repos

Filesystem

df -h // view available disc space

Files

ls  -la  // list contents: -a includes all, -l includes files attributes (long-format), -i provides inode number for each file
cp  -r [source] [destination] // copy: -r recursively
mv [source] [destination] // move
rm -rf [source] [source2] // remove: -r recursively

Make a new file

cat > foo.txt // Enter content of the File, CTRL+D to save
OR
echo "[contents]" > [destination]

cat [source] // will print contents of a file

ln -s [source] [destination] // link: -s symlinks (can’t create a hardlink to a directory, but can create a symbolic link)

Find & Delete

OSX

find . -name ".DS_Store" \- delete // OSX: delete all .DS_Store files

Linux

find ./ -name ".DS_Store" -rf // delete all .DS_Store files

Directories

pwd // print working directory
lpwd // local print working directory
mkdir  // make directory
rmdir foo // removes directory
cd [.][..][../..] // change directory. . current directory, .. parent directory, ../.. grandparent etc
cd - // change to previous working directory
pushd <dir> // mark current directory and change to entered directory
pushd -<Number> // go to directory in stack (0 index - presented left to right starting 0)
popd // go to last directory off stack

Paths

directory/file.txt // Relative file path to current folder
/directory/file.txt // Absolute file path

Can include wildcard * in the file path to match all against the * portion

In Unix, a \ is used to signify a literal.  To work with a file with a space, use file\ name.txt and the space will be recognised literally as a space.  Otherwise can use quotation: "file name.txt"

Permissions

whoami // will tell us who currently logged in as
group // will tell us which groups we’re a member of

drwx-rwx-rwx // Directory-Owner-Group-Other
chmod -R [permissionsetttings] [directory] // change mode manages permissions for files/directories
chmod ugo+rwx /path/to/folder/file // user, group, other, read, write, executable (+ add, - minus)
chmod 777 /path/to/folder/file // each number reflects owner, group, other

chown -R [username]:[group] [directory] // change owner changes the owner of files/directories

Users & Groups

cat /etc/passwd //lists all users on the system
cat /etc/group // lists all groups on the system
sudo usermod -a -G groupname username // user mode -a add, -g group (add user to a group)
sudo usermod -m -d /path/to/new/home/dir username // change home directory for a user
sudo groupdel group // delete group (only after all primary users removed from group)

passwd username // change password of username
sudo deluser [--remove-home] username // delete user
sudo useradd --system --gid group --shell /bin/bash --home /path/home username // add new user
sudo gpasswd -d username groupname // remove user from group
sudo groupadd --system groupname // add new group, —system adds system group
su // switch user
sudo -s // navigate to root
sudo visudo // edit sudo configuration, must run as root

Path

echo $PATH //see contents of path
/etc/paths // OSX paths
edit dot-files:
nano ~/.bash_profile
nano ~/.profile
nano ~/.bashrc

Processes

service [service] restart // Ubuntu
sudo /etc/init.d/supervisor restart // Debian

Man pages & manpath

echo $MANPATH
man -w // list where manuals are located
/etc/manpaths // OSX manpaths
/private/etc/man.conf // man pages conf

MANPATH /usr/share/man
MANPATH /usr/local/share/man
MANPATH /usr/X11/man

man [command] // bring up manual for command
SPACE to page down, CONTROL+B to page up, q to quit

man ascii // display ASCII character encodings

Memory

free -m // view available RAM

Create a swapfile:
sudo dd if=/dev/zero of=/path/to/swapfile bs=1024 count=1048576
sudo chmod 600 /path/to/swapfile
sudo mkswap /path/to/swapfile
sudo swapon /path/to/swapfile
Add to /etc/fstab to enable at reboot:
/path/to/swap none swap sw 0 0

Utils

Globally search a regular expression and print (grep)

lsof | grep /var/folders // list open files, into search in /var/folders

List Open Files (lsof)

sudo lsof -n -i4TCP:$PORT | grep LISTEN  // see process listening on port

Process Status (ps)

ps -ax | grep [keyword] // find a running process
ps -faux // see parent process

grep -i error server.log // grep logs for error

Supervisor

Run supervisor as non-root

/etc/supervisor/supervisord.conf // Main supervisor config
/etc/supervisor/conf.d // Additional config
sudo supervisord -c /etc/supervisor/supervisord.conf // launch supervisor
supervisorctl // states running processes
supervisorctl reload // reloads processes
supervisorctl reread // rereads config files , will launch from config
supervisorctl status/stop/start/restart [process name]

Archives

unzip

sudo apt-get install unzip
unzip file.zip -d destination_folder

ar - used to extract .deb packages

ar x archive.deb /out/folder

tar

tar zxvf file.tgz folder // create archive
tar -zcvf archive-name.tar.gz directory-name // untar archive
-z : Compress archive using gzip program
-c: Create archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name