You are here

Linux commands every Drupal developer needs to know.

paulbooker's picture
Submitted by paulbooker on Mon, 06/05/2017 - 17:46

Keys & Permissions

Creating encryption keys

ssh-keygen -t rsa

Uploading your public encryption key to the web server (two ways)

ssh-copy-id [email protected]
cat ~/.ssh/ | ssh [email protected] "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Logging into a server using SSH

ssh -p port-number  [email protected]

Adding "super" permissions to a user

adduser new-ssh-user
usermod -aG sudo  your-ssh-user-name

Allowing access to a service (e.g apache) through the firewall

sudo ufw app list
sudo ufw app info "Apache Full"
sudo ufw allow in "Apache Full"


Connecting to a MySQL database

mysql -h your-database-host-name -u your-database-user-name -p

Exporting from a MySQL database

mysqldump -h your-database-host-name -u your-database-user-name  -p your-database-name | gzip -9 > your-export-name.sql.gz

Importing to a database

gunzip < your-export-name.sql.gz | mysql -u your-database-user-name -p your-database-name
mysql -u your-database-user-name -p your-database-name <  your-export-name.sql

Files & Directories

Taking a backup of the web root directory

tar  --exclude='/path/to/excluded/directory' -cf - /path/to/web/root/directory | gzip > backup.tar.gz

Downloading files from another web server

scp -r [email protected]:/path/to/web/root/directory .

Synchronizing your files directory on the development server with the files directory on the production server

rsync -azP [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server
rsync -a --delete [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server

Free disk space ?

df -h .

Size of a directory

du -sh

Largest files and directories (under a directory)

du -a /path/to/directory/ | sort -n -r | head -n 10

Searching for files (e.g tar.gz) with sizes

find / -name *.tar.gz -exec du -sh {} \;

List all files modified in the last 7 days

find ./ -type f -mtime -7 

Number of lines that match a given search query

grep -r something /path/to/directory | grep something-else | wc -l
zgrep something  /path/to/gzip/file | wc -l


Monitoring log files

tail -f /var/log/syslog /var/log/messages
tail -n 10 /var/log/syslog

IP addresses assigned to a web server

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'


Show all modified files.

git status

Reversing a git commit that has not been pushed.

git reset --soft HEAD~1

Reversing a git commit that has been pushed.

git reset --hard HEAD~1
git push --force origin master

If you need Drupal support for your website: please send me an email, and let me know what services you need.