This cheatsheet contains commands for Ubuntu, but they should work for any debian based Linux flavour. Maintained by @DavidCWebs - get in touch with me via Twitter.

This should really be in wiki format, but I'm playing with GitHub pages!

General Commands

Change Directory Move up one Directory Move to Root
cd /path/to/dir
cd /
Move to Users Home Show the directory you're in Find file whose name starts with "file", starting from root directory
cd ~
find / -name 'file*'

Managing Files & Directories

Copy an entire directory and it's contents, including sub directories & files:

sudo cp -r /path/to/source /path/to/destination
Delete Directory Delete directory and contents with no warnings Move file to destination directory
rm -r dirname
rm -rf dirname
mv file.txt destination-dir
List all files in a directory Write files & directories list to a file Include hidden files and directories, preceded with "."
ls -l
ls -LR >> list.txt
ls -a
List contents of parent directory Print directory contents to print.txt Remove file1 in current directory
ls ../
ls > print.txt
rm file1

Tree is a directory listing programme that displays files and folders recursively in a tree structure.

Install Tree Write files/dirs in tree format Tree format with file permissions
sudo apt-get install tree
tree >> inventory.txt
tree -p >> inventory.txt

Rename file extensions:

rename 's/\.foo$/\.bar/' * SPECIFIC EXAMPLE: rename 's/\.gddoc$/\.txt/' *

Managing Groups

List all groups on system Create new group Add user www-data to group
cat /etc/group
sudo groupadd newgrp
sudo adduser www-data newgrp

BASH Scripts

Make / executable

sudo chmod +x /

Move to /usr/local/bin, execute in terminal with

sudo mv /usr/local/bin

Managing Users

Add User to Sudoers:

sudo adduser <username> sudo

Create user with password and home folder:

sudo useradd -d /home/testuser -m testuser
sudo passwd testuser

Set Directory & File Permissions

Set Directory Permissions to 755:

find /var/www/ -type d -exec chmod 755 {} \;

Set File Permissions to 644:

find /var/www/path-to-wp -type f -exec chmod 644 {} \;

Create a MySQL Database with new user & password:

mysql -u root -pROOTPASSWORD -e "create database db_name; GRANT ALL PRIVILEGES ON db_name.* TO new_user@localhost IDENTIFIED BY 'NewUserPassword'"

Entering root password is optional and may be inadvisable. Leave it out, and the system will prompt for Root password in normal way before carrying out the mysql command.

Sync to Remote Server

This is probably better than scp - since it only transfers the differences between two sets of files. Assumes that SSH has been set up between local and remote.

rsync -az /path/to/source username@host:/path/to/destination

May need to specify SSH:

rsync -az -e ssh /path/to/source username@host:/path/to/destination

HTML version of rsync man page:

Common options used with rsync commands:

  • -v : verbose
  • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  • -a : archive mode - preserves almost everything - copies files recursively, preserves symbolic links, file permissions, user & group ownerships and timestamps
  • -z : compress file data
  • -h : human-readable, output numbers in a human-readable format

Good description of rsync flags:

Secure Copy to/from Remote Server

Copy example.txt from remote machine to local:

scp your_username@ /local/directory

Note that this command must be entered in a terminal on the LOCAL machine - permission will be denied if working in a remote terminal.