Important Linux Commands (This Cheat Sheet Saves Me a Ton of Time)

Linux Commands
I write a weekly newsletter, sharing my articles and thoughts on building and running websites. There’s no spam and it’s totally free.
Join 1500+ subscribers!

Here’s a list of important Linux commands I refer to when I can’t remember a command that I need.

Even basic Linux commands sometimes escape my memory. This is a cheat sheet that saves me a lot of time I would otherwise waste searching for them.

Run command as root.

sudo

Restart Apache2.

service apache2 restart

Restart MySQL.

service mysql restart

Compress directory with gzip.

tar -zcvf archive-name.tar.gz directory-name

Extract a compressed file to the current directory.

tar -zxvf archive-name.tar.gz

Extract a compressed file to a different directory.

tar -C /directory-name -zxvf archive-name.tar.gz

Extract a compressed file without the first “container” directory.

tar -xvf archive-name.tar.gz --strip-components 1

Delete file.

rm file-name

Rename file, move file.

mv old-file-name new-file-name

Delete an empty directory.

rmdir directory-name

Delete directory and its contents recursively.

rm -r directory-name

Delete directory and its contents recursively (remove the files without prompting for confirmation, regardless of the file’s permissions).

rm -rf directory-name

Delete directory contents recursively, but do not delete the target directory itself. Note: Does not remove hidden files from the target directory. Note: Does not remove hidden files from the target directory.

rm -r directory-name/*

Delete directory and its contents recursively, but do not delete the target directory itself (remove the files without prompting for confirmation, regardless of the file’s permissions). Note: Does not remove hidden files from the target directory.

rm -rf directory-name/*

Create directory.

mkdir directory-name

Change owner of a file.

chown someuser:somegroup file-name

Change owner of directory and all it’s contents recursively.

chown -R someuser:somegroup directory-name

Change permissions for a file or directory.

chmod 644 some_file_or_directory

Change permissions for a directory and subdirectories.

find directory-name -type d -exec chmod 755 {} \;

Change permissions for all files in a directory and subdirectories.

find directory-name -type f -exec chmod 644 {} \;

Show current directory (Print Working Directory).

pwd

Copy the content of directory a to directory b, including hidden files (.), preserve file ownership, permissions, etc. (-a).

cp -a a/. b

Download latest version of WordPress.

wget https://wordpress.org/latest.tar.gz

Then unzip the package.

tar -xzvf latest.tar.gz

Download the package list from the repositories, get information on the newest versions of packages.

sudo apt-get update

Install the newest versions of all packages currently installed on the system.

sudo apt-get upgrade

In addition to performing the function of upgrade, also intelligently handles changing dependencies with new versions of packages.

sudo apt-get dist-upgrade

Performs the function of upgrade but may also remove installed packages if that is required in order to resolve a package conflict.

sudo apt-get full-upgrade

MySQL / MariaDB

Log in to MySQL / MariaDB shell.

mysql -u username -p

Create new database.

create database databasename;

Delete database.

drop databasename;

Show all databases.

show databases;

Select database.

use databasename;

Show all tables.

show tables;

Show table structure.

describe tablename;

Create database user and grant all privileges.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Back up database into a file with mysqldump.

mysqldump -u username -p databasename > filename

Restore database from a file.

mysql -u username -p databasename < filename

I write a weekly newsletter, sharing my articles and thoughts on building and running websites. There’s no spam and it’s totally free. Join 1500+ subscribers!

Leave a Reply

Close Menu