My neverending post with Linux commands

Last update: 2015-10-20
NOTE: This post will probably never be finished. It’s my little collection of useful Linux commands. I will continue to update this when I stumble on something I will need again :)

Since I don’t use Linux that frequently and I always tend to forget the commands or snippets to do what I wan’t. So, here you go, future Björn.

Backup a single MySQL database

mysqldump -u root -p DATABASE_NAME > FILENAME.sql

Restore a single MySQL database

mysql -u root -p DATABASE_NAME < FILENAME.sql

Backup a single MongoDB collection

mongodump --db NAME_OF_DB --collection COLLECTION_NAME --out /where/to/put/it/mongodb-dump

Restore a MongoDB collection

mongorestore --db NAME_OF_DB --collection COLLECTION_NAME /path/to/backup/mongodb-dump/COLLECTION_NAME.bson

Transfer a file locally to server

scp -P 22 /LOCAL/PATH/TO/FILE/myfile.jpg USERNAME@IP:/SERVER/PATH/myfile.jpg

Transfer from the server to local

scp -P 22 USERNAME@IP:/SERVER/PATH/myfile.jpg /LOCAL/PATH/TO/FILE/myfile.jpg

See free memory in MB

free -m

List all running processes

ps ax

Piping and searching for words

ps ax | grep apache

Disk usage in specific folder

du -ah

See CPU-usage


Compress a directory

tar -zcvf ARCHIVE_NAME.tar.gz /directory

Extract a compressed file

tar -zxvf ARCHIVE_NAME.tar.gz

Create a memory swap-file

1. sudo dd if=/dev/zero of=/var/swapfile bs=1M count=2048
2. sudo chmod 600 /var/swapfile
3. sudo mkswap /var/swapfile
4. echo /var/swapfile none swap defaults 0 0 | sudo tee -a /etc/fstab
5. sudo swapon -a

File stuffs

Show deleted files that could be in limbo…

lsof | grep '(deleted)'

List size of folders (good to find evil evil big files)

du -h --max-depth=1

Show how much space is left on HDD