Linux Commands Cheat Sheet [Quick Reference]

Basic Linux commands cheat sheet for common use (including basic VI commands)

These will get you through 80% of your day-to-day work.

Folders & Files Linux Commands Cheat Sheet

ActionsLinux commands
View contents of a folder ls -l
List files and folder sizes (only first level) du -sh *
Find free, used, and total space useddf -h
Count files in a directoryls -1 | wc -l
Zip a directoryzip -r myfolder
Unzip a fileunzip
Create a new foldermkdir foldername
Recursively delete a folderrm -rf foldername
Create a new filetouch new-file.txt
Delete a filerm new-file.txt
Move files from subfolder to parentmv mysubfolder/* ./
Copy a folder recursively cp -R myfolder/ myfolder_backup/
Copy a filecp file.txt file-copy.txt
Rename a filemv oldFileName newFileName
Display the current directorypwd
Search in the current directoryfind . -name pattern
find . -name mynameprefix\*
("pattern" = file and folder name)
Find all file and folder namesfind / -name pattern
Combine many text files into onecat ./folder/* all.txt

More resources for handling files and folders/directories in Linux.

Basic VI Cheat Sheet

VI and VIM have many functions, but these are the top commands to open, save, close, edit and search a file. For more actions, check out a more complete VI cheat sheet or VIM cheat sheet.

ActionVi Commands
Edit filevi filename
Save file:w
Begin editingi
Exit editing modeESC
Save the file and quit the editor:wq
Quit file:q
Quit file without saving:q!
Display file numbers:set nu
Delete an entire linedd
Search for a string or pattern/pattern

Services Linux Commands Cheat Sheet

ActionLinux commands
View the Linux versionuname -a
Find the PHP versionphp -i
Search for PHP settings locationfind / -name "php.ini"
php -i | grep php.ini
Restart Apache/etc/init.d/apache2 restart
Start MySQLservice mysqld start
Check if MySQL is runningps afux | grep -i mysql
Stop MySQLservice mysqld stop

MySQL At The Command Line

Although I prefer to use a MySQL GUI like Sequel Ace for managing SQL, sometimes it’s you have to get your hands dirty and access the data from the command line.

ActionMySQL Code
MySQL loginmysql -u root -p
("root" is your username)
Find running threadsmysqladmin processlist;
List all databasesshow databases;
Select a database use dbname;
List tables in a databaseshow tables;
Show table structuredescribe tablename;
View table contentsSELECT * FROM tablename;
Insert a table rowINSERT INTO tablename (col1, col2) VALUES(15, 22);
Update a table rowUPDATE user_posts SET status_id = 2 WHERE id = '109';
Delete a table rowDELETE FROM table WHERE user = 'john';
Export databasemysqldump -uroot -p1234 dbname > dump.sql
(where "root" = username and "1234" = password)

In-depth resources for MySQL.

ubuntu linux logo

Cleanup Space on Linux

From time to time, server space issues can pop up. Here are quick ways to free up space in Ubuntu.

ActionLinux Commands
Access admin rightssudo -i
Clean APT cache
(check size, clean, check size)
du -sh /var/cache/apt/archives
sudo apt-get clean
du -sh /var/cache/apt/archives
Remove old kernelssudo apt-get autoremove --purge
Uninstall unused appssudo apt-get remove package-name1
Remove no longer required packages sudo apt-get autoremove

Further resources for freeing up space on a Linux Server.

Bonus: Debug PHP Files

Syntax check all php files in a directoryfind . -type f -name ".php" -exec php -l {} \; | grep -v 'No syntax errors'

More Linux Commands Cheat Sheets

Leave a Comment