Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Linux Cheat Sheet

David Egan


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

General Commands

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

Excellent tool for explaining commands:

Finding Stuff

Use grep to find a string recursively in a directory, output to a file:

grep -r "carawebs_download_link_repeater()" /var/www/thinkup/wp-content/themes/thinkup > thinkup-grep

Output File Info Recursively

Very handy for /wp-content/uploads to track down large files.

find /target-parent-dir -type f -follow -print|xargs ls -l > report

Get System Info

# Output IP address and other useful bits of info:
ifconfig | grep inet

# Output hostname:

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

Determine File/Directory Size

The du command summarises “Disk Usage”:

By default, the Single Unix Specification (SUS) specifies that du is to display the file space allocated to each file and directory contained in the current directory. Links will be displayed as the size of the link file, not what is being linked to; the size of the content of directories is displayed, as expected.

As du reports allocation space and not absolute file space, the amount of space on a file system shown by du may vary from that shown by df if files have been deleted but their blocks not yet freed. Also the minfree setting that allocates datablocks for the filesystem and the super user processes creates a discrepancy between total blocks and the sum of used and available blocks. The minfree setting is usually set to about 5% of the total filesystem size.

du wikipedia article.

To return directory size (not individually listed content size) in human-readable format (e.g. 3.9G):

du -hs /path/to/directory

du takes a single argument, specifying a pathname. If no pathname is specified, the current directory is used. du options:

  • -a In addition to the default output, include information for each non-directory entry
  • -c display a grand total of the disk usage found by the other arguments
  • -d #, the depth at which summing should occur. -d 0 sums at the current level, -d 1 sums at the subdirectory, -d 2 at sub-subdirectories, etc.
  • -H calculate disk usage for link references specified on the command line
  • -k show sizes as multiples of 1024 bytes, not 512-byte
  • -L calculate disk usage for link references anywhere
  • -s report only the sum of the usage in the current directory, not for each file
  • -x only traverse files and directories on the device on which the pathname argument is specified.

Other Unix and Unix-like operating systems may add extra options - BSD and GNU du specify a -h option, displaying disk usage in a format easier to read by the user (e.g. 10 MB).

WordPress Settings

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 {} \;

Note that permissions may need to be more permissive to allow uploading of files, depending on the server environment:

“Any file that needs write access from WordPress should be owned or group-owned by the user account used by the WordPress (which may be different than the server account). For example, you may have a user account that lets you FTP files back and forth to your server, but your server itself may run using a separate user, in a separate usergroup, such as dhapache or nobody. If WordPress is running as the FTP account, that account needs to have write access, i.e., be the owner of the files, or belong to a group that has write access. In the latter case, that would mean permissions are set more permissively than default (for example, 775 rather than 755 for folders, and 664 instead of 644).” From WordPress Codex.

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

Remote Server with Non-Standard SSH Port

If connecting on a non-standard SSH port (which you should be doing), set the port in the rsync command:

-e "ssh -p 1234" where “1234” is the SSH port number.

-e is the short form of rsh, which specifies the remote shell to use.

Sample rsync command for SSH port 1234:

cmd: 'rsync --progress -a -v -rz --checksum --delete -e "ssh -p 1234" _site/ username@12.23.567.89:/var/www/path/to/deploy'

Rsync Commands

May need to specify SSH:

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

Copy the contents of mu-plugins from a dev site to a local copy: rsync -raz -e "ssh -p 1234" username@ /var/www/local-copy/wp-content/mu-plugins

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
  • –delete : delete files on the destination that do not exist in source
  • –checksum : use checksum instead of timestamps to check whether the directory/site has changed

Exclude files from an rsync with --exclude. To exclude several different types of files, use --exclude more than once:

rsync -avP --exclude='*.zip' --exclude='*.gz'

Alternatively, use an exclude file: list each file/directory on a separate line.

Good description of rsync flags: to-dropbox-jungle-disk/

Good description of rsync & Jekyll:

Rsync from Remote Server

Copy directories recursively:

david@david-desktop:~$ rsync -raz username@12.123.1234.1234:/var/www/ ~/carawebs-server-backups

Combine this with a MYSQL backup:

mysqldump -u user -ppassword --all-databases > all_databases.sql

Restore from this:

mysql -u user -ppassword < all_databases.sql

Secure Copy to/from Remote Server

Copy example.txt from remote machine to local:

scp your_username@ /local/directory

Note for the LOCAL machine!

SCP with Non-Standard Port Number

Copy example.html from local to remote, on a non-standard port:

scp -P 1234 ~/example.html username@

Note: to specify port number, SCP needs an uppercase “P” - in contrast to rsync.

comments powered by Disqus