← Back to the blog

Command Line Cheat Sheet

A practical reference for navigating files, inspecting processes, permissions, disk usage, and everyday terminal work.

Terminal command reference

The command line is fastest when the commands you use regularly are easy to find and safe to apply. This reference keeps the everyday operations in one place.

Commands that delete files, change ownership, or stop processes can cause data loss or downtime. Check the resolved path, process ID, and current directory before running them—especially with sudo, -r, or -f.

Navigate directories

Print the current directory:

pwd

Move into a directory, return to its parent, or go back to the previous directory:

cd folder_name
cd ..
cd -

List files, including hidden entries:

ls -la

Create a directory and any missing parents:

mkdir -p path/to/folder

Work with files

Copy or move a file:

cp source.txt destination.txt
mv old-name.txt new-name.txt

Copy a directory recursively:

cp -R source_folder target_folder

Remove a file:

rm file_name

Remove a directory recursively only after verifying the exact path:

rm -r path/to/folder

Use rm -rf only when you deliberately need non-interactive forced removal. It bypasses useful safeguards and deserves a second path check.

Read a file or follow an actively written log:

cat file_name
tail -f path/to/application.log

Empty a log file without replacing it:

truncate -s 0 path/to/application.log

Inspect disk usage

Show the total size of a directory and the size of its immediate children:

du -sh path/to/folder
du -sh path/to/folder/*

Show filesystem capacity in a human-readable format:

df -h

Permissions and ownership

Use symbolic modes when the intent matters more than the numeric representation:

chmod u+x script.sh
chmod -R u+rwX,go-rwx private-folder

Change ownership for a known user and group:

sudo chown user_name:group_name file_name
sudo chown -R user_name:group_name path/to/folder

Avoid applying recursive permissions to broad or unresolved paths.

Processes and ports

Find a process listening on a port:

lsof -nP -iTCP:3000 -sTCP:LISTEN

Ask a process to stop gracefully before forcing it:

kill process_id
kill -9 process_id

SIGKILL (-9) gives the process no chance to clean up. Treat it as the fallback, not the default.

Services

On a systemd-based Linux host, inspect and restart a service:

sudo systemctl status service_name
sudo systemctl restart service_name

Service names and management tools differ across operating systems, containers, and hosting platforms. Verify the runtime before copying a production command.

Make commands explainable

A useful cheat sheet records intent, not just syntax. Before keeping a command, note what it changes, how to verify the result, and whether it is reversible. That small habit turns a list of terminal fragments into an operational tool.

← Back to the blog