Where distros differ, each one gets its own command. Alpine examples assume you're root, because Alpine doesn't include sudo by default, and some options differ on Alpine's built-in tools.
Tools for this guide: chmod calculator, Cron expression builder.
Files and folders #
-
List files, including hidden ones, with sizesLinux, macOS
ls -lah -
Show the current folderLinux, macOS
pwd -
Go to your home folderLinux, macOS
cd ~ -
Create a folder and any missing parent foldersLinux, macOS
mkdir -p projects/site/assets -
Copy a folder and everything in itLinux, macOS
cp -r source/ backup/ -
Move or rename a fileLinux, macOS
mv old-name.txt new-name.txt -
Delete a fileLinux, macOS
rm file.txt -
Delete a folder and everything in itLinux, macOS
rm -rf folder/Warning: Deletes without asking and can't be undone. Check the path before you run it.
-
Create a shortcut (symbolic link) to a fileLinux, macOS
ln -s /path/to/target link-name -
Show a file's permissions, size and timestampsLinux, macOS
stat file.txt -
Find files by nameLinux, macOS
find . -name "*.log" -
Find files changed in the last dayLinux, macOS
find . -type f -mtime -1 -
Find files larger than 100 MBLinux, macOS
find / -type f -size +100M 2>/dev/null
Text #
-
Show a file's contentsLinux, macOS
cat file.txt -
Scroll through a long fileLinux, macOS
less file.txt -
Show the first 20 lines of a fileLinux, macOS
head -n 20 file.txt -
Show the last 50 lines of a fileLinux, macOS
tail -n 50 file.txt -
Search for text in a folderLinux, macOS
grep -rn "error" . -
Search for text, ignoring caseLinux, macOS
grep -i "error" app.log -
Show lines that don't contain some textLinux, macOS
grep -v "debug" app.log -
Replace text in a fileLinux
sed -i 's/old/new/g' file.txtWarning: Changes the file straight away, with no backup.
-
Count lines in a fileLinux, macOS
wc -l file.txt -
Sort lines and remove duplicatesLinux, macOS
sort file.txt | uniq -
Count how often each line appearsLinux, macOS
sort file.txt | uniq -c | sort -rn -
Print the first column of each lineLinux, macOS
awk '{print $1}' access.log -
Compare two filesLinux, macOS
diff -u old.txt new.txt -
Edit a file in the terminalLinux
nano file.txt
Permissions #
-
Make a script runnableLinux, macOS
chmod +x script.sh -
Let only the owner read and write a fileLinux, macOS
chmod 600 secrets.env -
Set standard folder permissionsLinux, macOS
chmod 755 folder/ -
Change a file's owner and groupLinux, macOS
sudo chown alice:alice file.txt -
Change the owner of a folder and everything in itLinux, macOS
sudo chown -R alice:alice folder/ -
Show your user ID and groupsLinux, macOS
id -
Open a shell as the root userLinux, macOS
sudo -i
Users and groups #
-
Show which user you areLinux, macOS
whoami -
Show who is logged inLinux, macOS
who -
Create a user with a home folderDebian, Ubuntu
sudo adduser aliceRed Hat, Fedorasudo useradd -m alice && sudo passwd aliceAlpineadduser alice -
Change a user's passwordLinux
sudo passwd alice -
Give a user admin rightsDebian, Ubuntu
sudo usermod -aG sudo aliceRed Hat, Fedorasudo usermod -aG wheel aliceAlpineaddgroup alice wheel -
Add a user to a groupDebian, Ubuntu, Red Hat, Fedora
sudo usermod -aG docker aliceAlpineaddgroup alice docker -
Show a user's groupsLinux, macOS
groups alice -
Delete a user and their home folderDebian, Ubuntu
sudo deluser --remove-home aliceRed Hat, Fedorasudo userdel -r aliceAlpinedeluser --remove-home aliceWarning: Deletes the user's files and can't be undone.
Packages #
-
Refresh the list of available packagesDebian, Ubuntu
sudo apt updateRed Hat, Fedorasudo dnf check-updateAlpineapk update -
Install a packageDebian, Ubuntu
sudo apt install nginxRed Hat, Fedorasudo dnf install nginxAlpineapk add nginx -
Update all installed packagesDebian, Ubuntu
sudo apt update && sudo apt upgradeRed Hat, Fedorasudo dnf upgradeAlpineapk update && apk upgrade -
Remove a packageDebian, Ubuntu
sudo apt remove nginxRed Hat, Fedorasudo dnf remove nginxAlpineapk del nginx -
Search for a packageDebian, Ubuntu
apt search nginxRed Hat, Fedoradnf search nginxAlpineapk search nginx -
List installed packagesDebian, Ubuntu
apt list --installedRed Hat, Fedoradnf list --installedAlpineapk info -
Find which package a file came fromDebian, Ubuntu
dpkg -S /usr/bin/curlRed Hat, Fedoradnf provides /usr/bin/curlAlpineapk info --who-owns /usr/bin/curl -
Remove packages that are no longer neededDebian, Ubuntu
sudo apt autoremoveRed Hat, Fedorasudo dnf autoremove
Services #
-
Check a service's statusDebian, Ubuntu, Red Hat, Fedora
systemctl status nginxAlpinerc-service nginx status -
Start a serviceDebian, Ubuntu, Red Hat, Fedora
sudo systemctl start nginxAlpinerc-service nginx start -
Stop a serviceDebian, Ubuntu, Red Hat, Fedora
sudo systemctl stop nginxAlpinerc-service nginx stop -
Restart a serviceDebian, Ubuntu, Red Hat, Fedora
sudo systemctl restart nginxAlpinerc-service nginx restart -
Start a service now and at every bootDebian, Ubuntu, Red Hat, Fedora
sudo systemctl enable --now nginxAlpinerc-update add nginx default && rc-service nginx start -
Stop a service starting at bootDebian, Ubuntu, Red Hat, Fedora
sudo systemctl disable nginxAlpinerc-update del nginx default -
List running servicesDebian, Ubuntu, Red Hat, Fedora
systemctl list-units --type=service --state=runningAlpinerc-status -
Reload service files after editing themDebian, Ubuntu, Red Hat, Fedora
sudo systemctl daemon-reload
Processes #
-
Show all running processesLinux, macOS
ps aux -
Find a process by nameLinux
pgrep -a nginx -
Watch CPU and memory use liveLinux, macOS
top -
Stop a process by IDLinux, macOS
kill 1234 -
Force a process to stopLinux, macOS
kill -9 1234Warning: The process can't clean up, so unsaved data may be lost.
-
Stop every process with a nameLinux, macOS
pkill nginx -
Keep a command running after you log outLinux, macOS
nohup ./script.sh & -
Show what is using a portLinux, macOS
sudo lsof -i :443
Logs #
-
Follow a log file as it growsLinux, macOS
tail -f app.log -
Read a service's recent logsDebian, Ubuntu, Red Hat, Fedora
journalctl -u nginx -n 50 -
Follow a service's logs liveDebian, Ubuntu, Red Hat, Fedora
journalctl -u nginx -f -
Show logs since the last bootDebian, Ubuntu, Red Hat, Fedora
journalctl -b -
Show errors since the last bootDebian, Ubuntu, Red Hat, Fedora
journalctl -p err -b -
Read the system logAlpine
tail -n 100 /var/log/messages -
Show recent kernel messagesLinux
sudo dmesg | tail -n 50
System #
-
Show which distro and version is installedLinux
cat /etc/os-release -
Show the kernel versionLinux, macOS
uname -r -
Show how long the system has been runningLinux, macOS
uptime -
Show memory useLinux
free -h -
Show CPU detailsLinux
lscpu -
Show the hostnameLinux, macOS
hostname -
Change the hostnameDebian, Ubuntu, Red Hat, Fedora
sudo hostnamectl set-hostname web01 -
Show the date, time and time zoneDebian, Ubuntu, Red Hat, Fedora
timedatectl -
Set the time zoneDebian, Ubuntu, Red Hat, Fedora
sudo timedatectl set-timezone Europe/Dublin -
Restart the systemLinux
sudo rebootWarning: Disconnects everyone and stops running services.
Storage #
-
Check free disk spaceLinux, macOS
df -h -
See what is using disk spaceLinux, macOS
du -sh * | sort -h -
Find the biggest folders on the systemLinux, macOS
sudo du -h -d 1 / 2>/dev/null | sort -h -
List disks and partitionsLinux
lsblk -
Show disk IDs and filesystem typesLinux
sudo blkid -
Mount a partitionLinux
sudo mount /dev/sdb1 /mnt/data -
Unmount a partitionLinux
sudo umount /mnt/data -
List mounted filesystemsLinux
findmnt -
Format a partition as ext4Linux
sudo mkfs.ext4 /dev/sdb1Warning: Erases everything on the partition. Check the device name with lsblk first.
Network #
-
Show this machine's IP addressesLinux
ip -br addr -
Show the routing tableLinux
ip route -
List listening portsLinux
sudo ss -tulpn -
Check whether a host is reachableLinux, macOS
ping -c 4 example.com -
Trace the route to a hostLinux, macOS
traceroute example.com -
Look up a domain's IP addressLinux, macOS
dig +short example.com -
Look up a domain's mail serversLinux, macOS
dig +short MX example.com -
Show the DNS resolver settingsLinux
cat /etc/resolv.conf -
See only the response headers from a siteLinux, macOS
curl -I https://example.com -
Download a fileLinux, macOS
curl -LO https://example.com/file.tar.gz -
Test whether a port is openLinux, macOS
nc -zv example.com 443 -
Show your public IP addressLinux, macOS
curl -s https://ifconfig.me -
Show network connectionsRed Hat, Fedora
nmcli connection show -
Restart networkingAlpine
rc-service networking restart
Firewall #
-
Show the firewall rulesDebian, Ubuntu
sudo ufw status verboseRed Hat, Fedorasudo firewall-cmd --list-all -
Allow SSH through the firewallDebian, Ubuntu
sudo ufw allow OpenSSHRed Hat, Fedorasudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload -
Allow a portDebian, Ubuntu
sudo ufw allow 443/tcpRed Hat, Fedorasudo firewall-cmd --permanent --add-port=443/tcp && sudo firewall-cmd --reload -
Remove a port ruleDebian, Ubuntu
sudo ufw delete allow 443/tcpRed Hat, Fedorasudo firewall-cmd --permanent --remove-port=443/tcp && sudo firewall-cmd --reload -
Turn on the firewallDebian, Ubuntu
sudo ufw enableRed Hat, Fedorasudo systemctl enable --now firewalldWarning: Allow SSH first if you're connected remotely, or you'll lock yourself out.
SSH #
-
Connect to a serverLinux, macOS
ssh user@host -
Connect on a custom portLinux, macOS
ssh -p 2222 user@host -
Run one command on a serverLinux, macOS
ssh user@host 'uptime' -
Create a new SSH keyLinux, macOS
ssh-keygen -t ed25519 -C "label" -
Copy your key to a serverLinux, macOS
ssh-copy-id user@host -
Copy a file to a serverLinux, macOS
scp file.txt user@host:/tmp/ -
Copy a file from a serverLinux, macOS
scp user@host:/var/log/app.log . -
Sync a folder to a serverLinux, macOS
rsync -avz dist/ user@host:/var/www/site/ -
Reach a server's port through SSHLinux, macOS
ssh -L 8080:localhost:80 user@host -
Check the SSH server config for errorsLinux
sudo sshd -t -
Restart the SSH serverDebian, Ubuntu
sudo systemctl restart sshRed Hat, Fedorasudo systemctl restart sshdAlpinerc-service sshd restart
Archives #
-
Compress a folderLinux, macOS
tar -czf backup.tar.gz folder/ -
Extract a .tar.gz archiveLinux, macOS
tar -xzf backup.tar.gz -
Extract a .tar.gz archive into a folderLinux, macOS
tar -xzf backup.tar.gz -C /path/to/folder -
List what is inside a .tar.gz archiveLinux, macOS
tar -tzf backup.tar.gz -
Create a zip fileLinux, macOS
zip -r archive.zip folder/ -
Extract a zip fileLinux, macOS
unzip archive.zip
Scheduled tasks #
-
Edit your scheduled tasksLinux, macOS
crontab -e -
List your scheduled tasksLinux, macOS
crontab -l -
Run a script every day at 2am (add this line with crontab -e)Linux, macOS
0 2 * * * /home/alice/backup.sh -
List scheduled systemd timersDebian, Ubuntu, Red Hat, Fedora
systemctl list-timers