Cheat Sheets

JPNAS.NET / Cheat Sheets

🐧 Linux Command Line

The commands I use most on Linux servers and desktops – files, searching, permissions, processes, packages, services and logs. Examples are for Debian and Ubuntu; most work everywhere.

Open in the interactive Cheat Sheets

Moving around and files

pwdShow the folder you are in.
ls -lahList all files, also hidden ones, with sizes in KB/MB.
cd ~ cd - cd ..Go to your home folder / back to the previous folder / one level up.
mkdir -p projects/site/imgMake a folder, with all missing parent folders.
cp -r source/ target/Copy a folder with everything in it.
mv old.txt new.txtRename or move a file.
rm file.txt rm -r folder/Delete a file / a folder. There is no recycle bin – check twice.
ln -s /mnt/nas/photos ~/photosMake a shortcut (symbolic link).

Reading and searching

less file.logRead a long file. Space = next page, / = search, q = quit.
head -n 20 file tail -n 50 fileThe first 20 / last 50 lines.
tail -f /var/log/syslogFollow a log live as new lines are written. Ctrl+C to stop.
grep -rni "error" /etc/nginx/Search for text in all files of a folder, ignoring case, with line numbers.
find . -name "*.log" -mtime +7Find .log files older than 7 days in this folder and below.
du -sh * | sort -hHow big is each folder here, smallest to biggest.
df -hFree space on all disks.

Permissions and users

sudo commandRun one command as administrator (root).
chmod +x script.shMake a script executable.
chmod 644 file chmod 755 folderUsual rights: owner can write, others only read.
sudo chown -R jernej:jernej folder/Change the owner of a folder and everything in it.
whoami idWho am I, and in which groups.
sudo adduser name sudo usermod -aG sudo nameAdd a user / give them sudo rights.

Processes and system

htopLive list of processes, CPU and memory (top if htop is not installed).
ps aux | grep nginxFind a running process by name.
kill 1234 kill -9 1234Ask a process to stop / force it to stop.
free -hMemory and swap use.
uname -a cat /etc/os-releaseKernel version / which Linux this is.
lsblk -fAll disks and partitions with file systems.
uptimeHow long since the last restart, and the load.

Packages (Debian, Ubuntu, Mint)

sudo apt update && sudo apt upgradeRefresh the package list and install all updates.
apt search nameFind a package.
sudo apt install nameInstall a package.
sudo apt remove name sudo apt autoremoveRemove a package / remove packages nobody needs any more.
sudo dnf upgradeThe same on Fedora.

Services and logs (systemd)

systemctl status nginxIs the service running, and its last log lines.
sudo systemctl restart nginxRestart a service, for example after changing its settings.
sudo systemctl enable --now nginxStart a service now and at every boot.
systemctl --failedWhich services failed to start.
journalctl -u nginx -fFollow the log of one service live.
journalctl -b -p errAll errors since the last boot.

Archives

tar -czf backup.tar.gz folder/Pack and compress a folder.
tar -xzf backup.tar.gzUnpack it here.
zip -r archive.zip folder/ unzip archive.zipThe same with .zip, for sending to Windows users.

Shell tricks

TabComplete a command or file name. Press twice to see all options.
Ctrl+RSearch through the commands you typed before.
sudo !!Repeat the last command with sudo – when you forgot it.
command > file.txt command >> file.txtWrite the output to a file / add it to the end.
command1 | command2Send the output of one command into the next.
Ctrl+C Ctrl+DStop the running command / log out of the shell.