Shell cheatsheet

Working with SSH (Shell) commands can be extremely powerful and timesaving if you do it right. If you do it wrong, though, it can be disastrous. It also can be time consuming if you have to look up the right commands for each job all the time. That’s why I created this little cheatsheet, which I’ll update on the go.

Navigating via SSH
Getting around in a terminal is basically the same as using your File Explorer on Windows or Finder on OS X. Only you don’t see the folders and you don’t have a cursor.
But it’s really doable if you know how:

See the current path you’re at
pwd

List all files and folders in the current directory
ls

List all files and folder including hidden files in current directory
la

Move into a directory
cd folder/

Move up a directory
cd ../

File management via SSH
It’s possible to manage files via SSH; you can create, move, rename, delete and edit them all from the terminal. Though the interface isn’t specifically suited for extensive editing, it can be useful to do some quick edits.

Make a directory
mkdir mydirectory

Create a file
touch myfile.txt

Copy a file
cp myfile.txt myfile-copy.txt

Move a file to a directory
mv myfile.txt mydirectory/

Remove a file
rm myfile.txt

Remove a directory and it’s contents
rm -rf mydirectory

List file/folder size of all items in current path
du -sh *

Zip and unzip folder/files
zip -r archive.zip mydirectory/ and unzip archive.zip

Remove all WordPress generated images ( filename[size]x[size].ext )
find . -regextype posix-extended -regex ".*-[[:digit:]]{2,4}x[[:digit:]]{2,4}(@2x)?.(jpg|jpeg|png|eps|gif)" -type f

Filetransfers
Copy RSA/SSH key to host
ssh-copy-id user@123.45.56.78

Secure copy file from local to server
scp filename.ext user@domain.com:/home/path/to/public

Secure copy folder from local to server
scp -r folder/* user@domain.com:/home/path/to/public

Secure copy download from server to local
scp -r user@domain.com:path/to/foobar /local/path/.

Copy one folder to another (and clear sourcefolder first)
rsync -avv --delete ~/path/to/sourcefolder/ ~/path/to/destination/

Download all files from a csv file to current directory
cut -d, -f1 file.csv | xargs wget

MySQL via SSH
Login to database
mysql -u[user] -p[password]

List all databases
SHOW DATABASES;

Create new database
CREATE DATABASE [databasename];
CREATE DATABASE IF NOT EXISTS [databasename];

Select database
USE [databasename];

Show all tables in current selected database
SHOW TABLES;

Show all data in table
SELECT * FROM [tablename];

List table field formats
DESCRIBE [tablename];

Delete a table or database
DROP TABLE [tablename];
DROP DATABASE [databasename];

Show the columns and column information pertaining to the designated table
SHOW COLUMNS FROM [tablename];

Select and show unique records
SELECT DISTINCT [columnname] FROM [tablename];

To export a MySQL Dump to a .sql file, use this command with the correct username and database name. You’ll be asked for the password with the username.
mysqldump -p -u username database_name > dbname.sql

To import such a dump, use this command
mysql -p -u username database_name < dbname.sql

Rights management
List numeral access right
stat -c %a [file/dir]

Logging
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log

User management
Add user
sudo adduser [user]

Set user pass
sudo passwd [user]

Remove user and home dir
sudo userdel --remove [user]

Add user to group
sudo usermod -a -G [group] [user]