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
pwd | Show the folder you are in. |
ls -lah | List 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/img | Make a folder, with all missing parent folders. |
cp -r source/ target/ | Copy a folder with everything in it. |
mv old.txt new.txt | Rename 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 ~/photos | Make a shortcut (symbolic link). |
Reading and searching
less file.log | Read a long file. Space = next page, / = search, q = quit. |
head -n 20 file tail -n 50 file | The first 20 / last 50 lines. |
tail -f /var/log/syslog | Follow 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 +7 | Find .log files older than 7 days in this folder and below. |
du -sh * | sort -h | How big is each folder here, smallest to biggest. |
df -h | Free space on all disks. |
Permissions and users
sudo command | Run one command as administrator (root). |
chmod +x script.sh | Make a script executable. |
chmod 644 file chmod 755 folder | Usual rights: owner can write, others only read. |
sudo chown -R jernej:jernej folder/ | Change the owner of a folder and everything in it. |
whoami id | Who am I, and in which groups. |
sudo adduser name sudo usermod -aG sudo name | Add a user / give them sudo rights. |
Processes and system
htop | Live list of processes, CPU and memory (top if htop is not installed). |
ps aux | grep nginx | Find a running process by name. |
kill 1234 kill -9 1234 | Ask a process to stop / force it to stop. |
free -h | Memory and swap use. |
uname -a cat /etc/os-release | Kernel version / which Linux this is. |
lsblk -f | All disks and partitions with file systems. |
uptime | How long since the last restart, and the load. |
Packages (Debian, Ubuntu, Mint)
sudo apt update && sudo apt upgrade | Refresh the package list and install all updates. |
apt search name | Find a package. |
sudo apt install name | Install a package. |
sudo apt remove name sudo apt autoremove | Remove a package / remove packages nobody needs any more. |
sudo dnf upgrade | The same on Fedora. |
Services and logs (systemd)
systemctl status nginx | Is the service running, and its last log lines. |
sudo systemctl restart nginx | Restart a service, for example after changing its settings. |
sudo systemctl enable --now nginx | Start a service now and at every boot. |
systemctl --failed | Which services failed to start. |
journalctl -u nginx -f | Follow the log of one service live. |
journalctl -b -p err | All errors since the last boot. |
Archives
tar -czf backup.tar.gz folder/ | Pack and compress a folder. |
tar -xzf backup.tar.gz | Unpack it here. |
zip -r archive.zip folder/ unzip archive.zip | The same with .zip, for sending to Windows users. |
Shell tricks
Tab | Complete a command or file name. Press twice to see all options. |
Ctrl+R | Search through the commands you typed before. |
sudo !! | Repeat the last command with sudo – when you forgot it. |
command > file.txt command >> file.txt | Write the output to a file / add it to the end. |
command1 | command2 | Send the output of one command into the next. |
Ctrl+C Ctrl+D | Stop the running command / log out of the shell. |