Shell cheatsheet

Published on 20 Apr 2016

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

List all files and folders in the current directory

List all files and folder including hidden files in current directory

Move into a directory
cd folder/

Move up a directory
cd ../

Find text in files, list their location, exclude /var/
grep -rin 'searchterm' -l --exclude-dir=var

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 mydirectory/ and unzip

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

Trim 5px borders from images
convert input.jpg -crop 1x1-5-5@ output.jpg

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

Secure copy file from local to server
scp filename.ext

Secure copy folder from local to server
scp -r folder/*

Secure copy download from server to local
scp -r /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

Login to database
mysql -u[user] -p[password]

List all databases

Create new database
CREATE DATABASE [databasename];

Select database
USE [databasename];

Show all tables in current selected database

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

Magento specific: get payment method for specific range of order IDs
SELECT p.method, count(o.increment_id) AS num
FROM sales_flat_order AS o,
sales_flat_order_payment AS p
WHERE o.entity_id = p.parent_id
AND o.increment_id >= first_order_increment
AND o.increment_id <= last_order_increment GROUP BY p.method ORDER BY num desc;

Replace column value oldvalX for newvalY (replace tablename, colname and values obvs)
UPDATE `tablename`
SET `colname` = replace(`colname`, 'oldvalX', 'newvalY')

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

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]

Image manipulation
Resize via mogrify
mogrify -resize 50% *.png # keep image aspect ratio
mogrify -resize 320x240 *.png # keep image aspect ratio
mogrify -resize 320x240! *.png # don't keep image aspect ratio
mogrify -resize x240 *.png # don't keep image aspect ratio
mogrify -resize 320x *.png # don't keep image aspect ratio


Short notes and snippets as reminders for myself and my work.

All categories

All tags

All posts