Linux and the Command Line
Linux and the Command Line: Shell Commands, Bash, and Administration
Linux proficiency starts with navigating the file system, managing files and directories, reading and editing text, managing processes and services, setting permissions, and using package managers. Bash scripting ties these commands together into repeatable automation. Fluency with the command line is expected in every infrastructure, DevOps, and security role.
Navigating the file system and essential commands
The Linux file system is a single tree rooted at /. Important directories include /etc (configuration files), /var (variable data like logs), /home (user home directories), /tmp (temporary files cleared on reboot), /usr (user programs and libraries), and /boot (kernel and bootloader files). Navigation uses cd to change directories, pwd to print the current path, and ls to list directory contents. ls -la shows all files including hidden ones (names starting with a dot) with detailed permissions, ownership, and size.
File manipulation commands: cp copies files, mv moves or renames them, rm removes them (rm -rf removes directories recursively without prompting, so use it carefully), mkdir creates directories, and touch creates empty files or updates timestamps. Viewing file contents: cat prints the entire file, less pages through it interactively, head shows the first lines, tail shows the last lines, and tail -f follows a file as it grows (essential for watching log files in real time).
Text processing and searching
grep searches for patterns in files or output. grep -r 'error' /var/log/ searches recursively through the log directory for lines containing 'error'. grep -i makes the search case-insensitive. Piping (the | character) chains commands: ls -la | grep '.conf' filters the listing to show only files containing '.conf'. awk and sed are more powerful text processing tools; awk is excellent for working with columnar data, and sed makes pattern-based substitutions in text streams.
find locates files by name, type, size, modification time, or permissions: find /etc -name '*.conf' lists all configuration files under /etc. wc -l counts lines (useful for counting log entries). sort and uniq sort and deduplicate output. Redirection operators send output to files: > overwrites, >> appends. 2> redirects standard error, and 2>&1 merges error output into standard output, which is useful for capturing both in logs.
Processes, services, and system state
ps aux lists all running processes with their PID (process ID), owner, CPU, and memory usage. top and htop provide dynamic, interactive views of running processes and system resource consumption. kill sends a signal to a process by PID: kill -9 forces immediate termination when the process is not responding to a normal SIGTERM. pgrep finds processes by name. Processes running in the background can be managed with jobs, bg, and fg.
On systemd-based distributions, systemctl manages services: systemctl status sshd checks the state of the SSH daemon, systemctl start/stop/restart changes it, and systemctl enable/disable controls whether it starts at boot. journalctl reads the systemd journal: journalctl -u sshd shows logs for just the SSH service, journalctl -f follows new log entries in real time. df -h shows disk usage by mount point, du -sh * shows disk usage by directory, and free -h shows memory and swap usage.
Bash scripting fundamentals
A Bash script is a text file starting with #!/bin/bash (the shebang line) that contains shell commands executed in sequence. Variables are set without spaces: NAME='value', and referenced with $NAME or ${NAME}. Command substitution captures output: RESULT=$(command). Arithmetic uses $(( )) notation. The if/then/elif/else/fi construct branches on conditions; [[ ]] is the preferred test syntax in Bash. for and while loops iterate over lists or conditions.
Functions group reusable code: function greet() { echo "Hello $1"; } defines a function where $1 is the first argument. $?, the exit status, is 0 for success and nonzero for failure; checking it is how scripts handle errors. set -e makes a script exit immediately on any command that fails, and set -u treats references to unset variables as errors. These two options together catch the most common scripting mistakes early. Make a script executable with chmod +x scriptname.sh.
Key concepts
- + File system hierarchy: /etc holds config, /var holds logs and runtime data, /home holds user data, /tmp is cleared on reboot.
- + Permissions: chmod and chown set file permissions and ownership; understand rwx in both symbolic and octal notation.
- + Pipes and redirection: | chains commands; > and >> redirect output to files; grep filters lines by pattern.
- + Process management: ps, top, kill, and systemctl are the primary tools for viewing and controlling running processes and services.
- + Package management: apt for Debian/Ubuntu (apt install, apt update, apt upgrade); dnf/yum for Red Hat family distributions.
- + Bash scripting: Shebang line, variables, conditionals, loops, and functions; set -e and set -u catch common errors.
Frequently asked questions
What is the difference between sudo and su?
How do I edit a file from the command line?
What does chmod 600 mean and when is it used?
How do I search the command history?
What is SSH and how do I use key-based authentication?
Related topics
Reserved for affiliate links to recommended technical books and study guides. Not yet wired.
Reserved for affiliate links to online learning platforms. Not yet wired.
Self-hosted newsletter signup. Not yet wired to an email provider.