Comprehensive reference for linux commands reference covering essential concepts, practical examples, and production best practices.
This page documents standard Linux command-line utilities, focusing on POSIX-compliant tools and core system daemons available on most distributions. It covers the mechanics of process management, file manipulation, text processing, and system control. It is designed for engineers who need to execute precise operations via shell scripts or interactive terminals, distinguishing between high-level wrappers and low-level system calls.
Managing process lifecycle requires distinguishing between user-space commands and kernel-level states. The ps command provides a snapshot of process states. The most common failure mode is misinterpreting the STAT column. S indicates a sleeping process waiting for an event, while R indicates running or runnable. Z denotes a zombie process: it has finished execution but remains in the process table because its parent has not yet called wait(). Zombie processes consume no CPU or memory but can exhaust the process ID table if left unchecked.
To terminate processes, kill sends signals. The default signal is SIGTERM (15), which allows the process to perform cleanup before exiting. SIGKILL (9) forces immediate termination without cleanup. A common error is using kill on a process group. By default, kill targets a single PID. To terminate a group, use kill -TERM -- -1 or pipe the output of pgrep to xargs kill.
The systemctl utility manages systemd units. It distinguishes between services, sockets, targets, and mounts. When debugging service failures, systemctl status <service> displays the most recent log lines and the exit code. A non-zero exit code in the Active line indicates a failure. However, a service can be active (running) but unhealthy if its main process crashes and is restarted by systemd's restart policy. Check the RestartCount field in the status output to detect flapping services.
The top and htop commands provide real-time resource monitoring. top is part of procps-ng. Its PID column is critical for identifying the process owner. Note that CPU usage in top is per-core; on a multi-core system, a single-threaded process can show 100% CPU usage, while a multi-threaded process might show 400% on a 4-core machine. This is not an error but a reflection of total available CPU time.
File manipulation utilities operate on inode metadata and data blocks. The find command is powerful but often misused due to operator precedence. The -name and -mtime predicates are combined with implicit AND logic. To use OR logic, enclose expressions in parentheses and escape them for the shell: find /var/log -name "*.log" \( -mtime -7 -o -size +10M \). Without escaping, the shell interprets the parentheses as subshell grouping, breaking the find syntax.
The cp command behaves differently based on whether the destination is a directory. If the destination does not exist, cp creates it as a copy of the source. If it exists and is a directory, cp places the source inside it. The -a flag is a shorthand for -dR --preserve=all. It preserves modes, ownership, timestamps, and context. A common pitfall is using cp -a on a symlink; it preserves the symlink itself, whereas cp -aL dereferences it.
The mv command moves files within the same filesystem by updating directory entries (hard link operations), which is instantaneous. Moving across filesystems involves a copy followed by a delete. This distinction matters for large files on slow networks or high-latency storage. The stat command displays detailed file metadata, including inode number, block size, and link count. Use stat -c %i to get the inode number, which is essential for tracking files across moves and renames.
Text processing tools operate on streams of characters. grep searches for patterns. The -E flag enables extended regular expressions, allowing | (OR), + (one or more), and ? (zero or one) without escaping. The -v flag inverts the match, printing non-matching lines. A subtle behavior is that grep returns exit code 0 if a match is found, and 1 if no match is found. In shell scripts, this must be accounted for in conditional statements.
awk is a pattern scanning and processing language. It divides each input line into fields. The default field separator is whitespace. To process CSV files with commas, use -F','. The BEGIN and END blocks execute before and after processing all input lines, respectively. A common error is using print instead of printf. print appends a newline, while printf does not, allowing for precise output formatting.
sed is a stream editor. It reads input, applies commands, and writes output. The s command performs substitution. The global flag g replaces all occurrences in a line, not just the first. The delimiter / can be changed to avoid escaping slashes in paths: sed 's|/usr/local|/opt|g'. The -i flag edits files in place. On macOS, sed -i '' is required because BSD sed expects the extension argument, whereas GNU sed on Linux allows sed -i without an argument.
xargs builds and executes command lines from standard input. It is often used with find. The find ... | xargs rm pattern is efficient but can fail if the argument list exceeds the system limit. The -x flag causes xargs to exit if the argument list is too large, and -s sets the maximum command line size. For very large datasets, find ... -exec rm {} \; is safer but slower because it spawns a new process for each file.
Network utilities interact with the kernel’s network stack. curl transfers data from or to a server. It supports HTTP, HTTPS, FTP, and more. The -v flag enables verbose output, showing headers and connection details. The -d flag sends POST data. A common mistake is confusing -d with -F. -d sends data as application/x-www-form-urlencoded, while -F sends multipart/form-data, which is necessary for file uploads.
ss is the successor to netstat. It displays socket statistics. The -t flag shows TCP sockets, -u shows UDP, and -l shows listening sockets. The -p flag displays the process using the socket. Unlike netstat, ss reads directly from /proc/net and is faster. It also provides more detailed information about TCP states, such as ESTAB (established), TIME-WAIT, and CLOSE-WAIT.
ip is the modern replacement for ifconfig and route. It manages IP addresses, routes, and tunnels. The ip addr show command displays network interfaces and their addresses. The ip route command displays the routing table. To add a route, use ip route add <network> via <gateway>. The metric keyword sets the priority of the route. Lower metrics are preferred.
journalctl queries the systemd journal. It displays log messages from the boot up to the present. The -u <service> flag filters logs for a specific service. The -f flag follows the log output, similar to tail -f. The --since and --until options allow filtering by time. A common issue is log rotation; if the journal is not rotated correctly, it can grow indefinitely. Check the MaxFileSize setting in /etc/systemd/journald.conf to control this.
The shell environment manages variables and job control. echo and printf output text. echo adds a newline by default, while printf does not. The export command makes a variable available to child processes. Unexported variables are local to the current shell. The env command displays all environment variables.
Job control allows managing multiple jobs. The & symbol runs a command in the background. The jobs command lists background jobs. The fg command brings a background job to the foreground, and bg resumes a stopped job in the background. The kill %1 command kills job number 1.
The source (or .) command executes a script in the current shell environment, whereas bash script.sh executes it in a subshell. Changes to variables in a sourced script persist after the script finishes. This is crucial for configuration scripts that set environment variables.
Debugging shell scripts requires understanding exit codes. Every command returns an exit code: 0 for success, non-zero for failure. The $? variable holds the exit code of the last command. The set -e option causes the script to exit immediately if any command returns a non-zero status. The set -x option prints commands and their arguments as they are executed, providing a trace of execution.
The trap command sets signals to be caught. For example, trap 'echo "Cleaning up"; rm temp_file' EXIT ensures that temp_file is removed when the script exits, regardless of whether it exits normally or due to an error. This is essential for robust scripts.
Standard error (stderr) is file descriptor 2. Standard output (stdout) is file descriptor 1. Redirecting stderr to stdout is done with 2>&1. Redirecting stdout to a file and stderr to another file is done with > file.out 2> file.err. This separation is critical for parsing logs and debugging.
The diff command compares files. The -u flag produces unified output, which is the default format for patch files. The patch command applies these differences. A common error is applying a patch to the wrong directory structure. The -p flag in patch strips leading path components, allowing patches to be applied from different directories.
The strace command traces system calls and signals. It is invaluable for debugging application crashes and permission errors. The -e trace=file flag filters for file-related system calls. The output shows the system call, its arguments, and its return value. A return value of -1 with an errno indicates an error. The errno can be looked up in /usr/include/asm-generic/errno.h.
This page was rewritten on 26 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at hello@thegarnetwiki.com and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy