Linux Commands

As this Tips section of the web site implies, you will find a few hard-to-remember Linux command sequences for specific tasks, useful for CLI (Command Line Interface) on-line with SSH, in Cron jobs and for bash scripts.  If not running as root, you will need to prefix some of these commands with sudo:

Edit a file:

nano -w /etc/apache2.conf

Delete a Directory:

rm -r /var/www/

Empty a Directory:

cd /var/www/
rm -r .*
rm -r *
  • Expect the following messages:
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'

Copy the Contents of one Directory to another:

cp -a /var/www/ /var/www/
  • Use -r instead of -a if this message is displayed:
cp: preserving times for '/var/www/': Operation not permitted

Rename a file or directory:

mv program-1.4.7 program

Change the URL of a WordPress site (not a Network/Multi-Site/WPMU), if WP-CLI is installed as the wp command:

cd /var/www/
wp search-replace "//" "//" --allow-root

Change Owner to let Apache access all web site files:

chown -R www-data:www-data /var/www

Create a Symlink:

ln -s /media/vgcloud/lvcloud/var/www /var/www
ls -ld /var/www
lrwxrwxrwx 1 root root 11 May  3 18:39 /var/www -> /media/vgcloud/lvcloud/var/www

Delete a Symlink without deleting what it points to:

rm /var/www

Find all .htaccess files on all web sites:

find /var/www/ -name .htaccess

Find all tmp directories, system-wide:

find / -name tmp

Find all files containing “phpversion” on a web site:

grep -r 'phpversion' /var/www/*

Decompress a Tar file that has been GZipped:

tar -xzf website.tar.gz

Compress a Directory, including hidden files and subdirectories, into a GZipped Tar file:

tar -czf website.tar.gz /var/www/

System Management

Restart immediately:


CLI as root:

sudo su
{root commands}

Check Free Space:

df -h
  • To prevent fragmentation, Linux needs 20% free space in each partition.

Update Ubuntu:

apt update
apt dist-upgrade

Uninstall any unneeded Ubuntu software:

apt autoremove

Add a domain/subdomain defined in /etc/apache2/sites-enabled/

apache2ctl configtest
systemctl restart apache2

Backup All MySQL Databases:

mysqldump -u ubuntu -p --all-databases --routines > alldb.sql

Update Cron:

crontab -l
crontab -r
crontab {cronfile}
crontab -l

Display MySQL Version:

mysql -V

Display Ubuntu Version:

lsb_release -a

Display Last Boot date/time:

who -b