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?
sudo runs a single command with elevated privileges as another user (by default root), logging the action. su switches the current shell to another user's account; su - starts a login shell for that user, loading their environment. sudo is preferred in modern administration because it logs every privileged command, can be restricted by the sudoers file to specific commands, and does not require sharing the root password.
How do I edit a file from the command line?
The most common command-line editors are nano (beginner-friendly, on-screen key hints), vim (powerful, modal, steep learning curve), and emacs (powerful, keyboard-shortcut-centric). nano is the easiest starting point: nano filename opens the file, Ctrl+O saves, Ctrl+X exits. vim is worth learning for servers where it is often the only editor available: i enters insert mode, Esc returns to normal mode, :wq saves and quits.
What does chmod 600 mean and when is it used?
chmod 600 sets permissions to rw------- meaning only the file owner can read and write it; the group and others have no access. This is the correct permission for private SSH keys (~/.ssh/id_rsa) and other files containing secrets. If an SSH key has broader permissions, the SSH client will refuse to use it as a security measure.
How do I search the command history?
Pressing Ctrl+R in Bash starts a reverse incremental search through your command history. Start typing and Bash shows the most recent matching command. Press Ctrl+R again to cycle to earlier matches. Press Enter to run the matched command or the right arrow to place it on the command line for editing. The history command lists your recent commands with numbers; !123 re-runs command number 123.
What is SSH and how do I use key-based authentication?
SSH (Secure Shell) provides an encrypted remote terminal session. Key-based authentication uses an asymmetric key pair instead of a password: the private key stays on your machine, and the public key is copied to ~/.ssh/authorized_keys on the remote server. Generate a key pair with ssh-keygen -t ed25519, copy the public key with ssh-copy-id user@host, and connect with ssh user@host. Key-based auth is more secure than passwords and supports automation without stored passwords.

Related topics

AFFILIATE_SLOT_BOOKS Recommended books on Linux and the command line

Reserved for affiliate links to recommended technical books and study guides. Not yet wired.

AFFILIATE_SLOT_COURSES Online courses for Linux and the command line

Reserved for affiliate links to online learning platforms. Not yet wired.

LEAD_SLOT_NEWSLETTER Get Linux and the command line reference updates

Self-hosted newsletter signup. Not yet wired to an email provider.

Technical Resources publishes vendor-neutral educational information about information technology. It is general reference material, not professional, safety, legal, or warranty advice, and it is not affiliated with, endorsed by, or sponsored by any vendor, certification body, or manufacturer named on the site. Product names, standards, and certification programs are referenced for identification and education only and belong to their respective owners. Technology, standards, exam objectives, and safety guidance change over time, so always verify any decision-critical detail against the current official documentation for your specific hardware, software, standard, or exam before you rely on it. Procedures involving electricity, batteries, or live equipment can be hazardous; follow the manufacturer's instructions and applicable safety codes, and consult a qualified professional when in doubt.