Linux Fundamentals
The shell commands you reach for every day: moving around, files, text, permissions, processes and pipes.
Move around
pwdPrint the directory you are in
ls -lahList everything, with sizes and permissions, in a readable form
cd ~ | cd - | cd ..Go home, go back to the previous directory, go up one level
find . -name "*.log" -mtime -1Find files named *.log changed in the last day
which python3 | type lsShow which program a command name runs
man tar | tar --helpRead the manual page, or the short help
history | grep sshSearch the commands you typed before (Ctrl+R searches as you type)
Files and folders
mkdir -p app/logs/archiveCreate a folder and any missing parents
touch notes.txtCreate an empty file, or update its time
cp -r src/ backup/Copy a folder with everything in it
mv old.txt new.txtRename or move a file
rm -r build/Delete a folder and its contents (there is no trash can)
ln -s /var/log/app app-logsMake a symbolic link, a shortcut to another path
stat notes.txtShow size, owner, permissions and all the timestamps
du -sh * | sort -hSize of each item here, smallest to largest
df -hFree and used space on each disk
Read and search text
less +F app.logPage through a file; press Ctrl+C then F to follow it like tail -f
head -n 20 file | tail -n 20 fileFirst or last 20 lines
tail -f /var/log/syslogFollow a log as it grows
grep -rn "error" /var/log/app/Search a folder recursively and show line numbers
grep -v "debug" app.logShow the lines that do NOT match
wc -l fileCount lines
cut -d: -f1 /etc/passwdTake the first field of each line, split on ":"
awk '{print $1, $NF}' filePrint the first and the last column of each line
sed 's/old/new/g' fileReplace every old with new (add -i to edit the file in place)
sort data.txt | uniq -c | sort -nrCount how often each line occurs, most frequent first
diff -u a.conf b.confShow what differs between two files
Pipes and redirection
The output of one command can feed the next, or go to a file.
cmd > out.txt | cmd >> out.txtWrite the output to a file, replacing it or appending to it
cmd 2> err.txtSend only the error messages to a file
cmd > all.txt 2>&1Send normal output and errors to the same file
ls | wc -lPipe: feed the output of ls to wc
cmd | tee out.txtShow the output and also save it
find . -name "*.tmp" | xargs rmTurn lines of input into arguments of another command
cmd1 && cmd2 | cmd1 || cmd2Run cmd2 only if cmd1 succeeded, or only if it failed
echo "Today is $(date +%F)"Put the output of a command inside another
Permissions and ownership
r=4, w=2, x=1. Three groups: owner, group, others.
chmod 640 fileOwner read+write, group read, others nothing
chmod u+x script.shLet the owner execute the file
chmod -R g+w shared/Give the group write access to a whole folder
chown alice:devs fileChange owner and group
chmod g+s shared/setgid on a folder: new files inherit its group
chmod +t /srv/dropboxSticky bit: only a file owner can delete their files in the folder
umask 027New files get 640 and new folders 750 by default
sudo -lList what you are allowed to run with sudo
Processes and services
ps aux | grep nginxFind a running process
top | htopLive view of CPU and memory use
kill PID | kill -9 PIDAsk a process to stop, or force it
pkill -f "python app.py"Stop processes by matching their command line
sleep 300 & then jobs, fg, bgRun in the background and manage shell jobs
nohup ./job.sh > job.log 2>&1 &Keep a job running after you log out
systemctl status nginxSee if a service is running (also start, stop, restart, enable)
journalctl -u nginx -fFollow a service's logs
free -h | uptimeMemory in use, and the load average
Archives and the network
tar -czf backup.tar.gz folder/Make a compressed archive
tar -xzf backup.tar.gz -C /tmpUnpack an archive into a folder
curl -I https://example.comFetch only the response headers
curl -s https://example.com | headFetch a page quietly and look at the start
ss -tulpnList listening TCP/UDP ports and the programs that own them
ping -c 3 example.comCheck that a host answers
Nothing matches that search.
Cheatsheet for the Linux Fundamentals path · Deboistech · All cheatsheets