A cheatsheet of the Ubuntu commands I keep forgetting, plus a few curl one-liners.
Basic Commands
# print working directory (the current directory you are in)pwd
# Go to current user home directorycd ~ # or justcd
# List command historyhistory# Search command historyhistory | grep php# Run command from command history number!48
# Get system information on Ubuntu / Debianlsb_release -a# Get linux kernel versionuname -r# Get 32 vs 64 bituname -i# See disk usage informationdf -h# See RAM and Swap usagefree -m
# Get list of all running processesps aux# Search a processps aux | grep php# See currently running processes and resource usagetop# Or a more nicer way, but you may need to install it firsthtop
# Compress a directorytar -zcvf archive-name.tar.gz directory-name
# List disk usagedu -shc /var/*Installing and Removing Packages
Updating Indexes of Available Packages
Package index files hold all of the available packages that can be installed on our machine. Before installing or upgrading any package, we have to update these index files, so our machine knows the latest changes on all of the available packages.
sudo apt-get updateRunning this command will re-synchronize the package index files from their sources. Note that you don’t have to run this command on every consecutive install command. You can check the list of the repository sources in:
# The default repository sources list file/etc/apt/sources.list
# Extra custom repository's sources list goes to this directory/etc/apt/sources.list.dSearching for a Package
You can search for a package with apt-cache:
# Will search package's name & descriptionsudo apt-cache search php
# Will search package by its name onlysudo apt-cache search -n php
# Show package detailsudo apt-cache show php7.1-fpmInstalling a Package
Finally, to install a package, run this command:
# Will prompt you before installationsudo apt-get install php7.1-fpm
# Will skip the confirmation prompt and install directlysudo apt-get install -y php7.1-fpm
# You can install multiple packages in one linesudo apt-get install -y php7.1-fpm php7.1-cliRemoving a Package
To remove a package you can do the following:
# Uninstall the package but will keep the dependencies & config filessudo apt-get remove php7.1-fpm
# Uninstall the package and remove the config filessudo apt-get remove --purge php7.1-fpm # Orsudo apt-get purge php7.1-fpm
# Remove package's dependencies that automatically installed and no longer usedsudo apt-get autoremoveUpdating the Default Editor
nano is the default text editor. To change it, use the following command:
sudo update-alternatives --config editor
# vim.basic => Is the usual Vim# vim.tiny => Is a slim down version of VimRemove Old Kernel Versions
Open the terminal and check your current kernel. DO NOT REMOVE THIS KERNEL!
uname -rNext, type the command below to view all installed kernels on your system.
dpkg --list | grep linux-imageFind all the kernels that are lower than your current kernel. When you know which kernel to remove, run the command below to remove the one you selected.
sudo apt-get purge linux-image-x.x.x.x-genericFinally, run the command below to update grub2:
sudo update-grub2Then reboot your system.
Check Disk Space
Check available space:
sudo df -hList used space and sort it:
sudo du /var/log -h --max-depth=1 | sort -hrDelete Files With a Pattern
sudo find /var/log/mysql -name 'mariadb-bin.0001*' -deleteCurl
# Include headers data.curl -i https://example.com
# Get headers data only.curl -I https://example.com
# Send request with header.curl -H 'accept: application/json' https://example.com/api/users
# Check if it's gziped.curl -H "Accept-Encoding: gzip" -I https://example.com
# Check if file with the given ETag is modifiedcurl -H 'If-None-Match: "59c6041a-1e823"' -I https://example.com/main.css