04. Essential Commands
A DevOps engineer spends the majority of their time navigating the filesystem, viewing logs, and processing text. Mastering these essential commands is the key to working efficiently in the terminal.
04.1 Navigation & File Management
Section titled “04.1 Navigation & File Management”Navigating the Filesystem
Section titled “Navigating the Filesystem”pwd(Print Working Directory): Shows exactly where you currently are in the filesystem.cd(Change Directory): Moves you to a different folder.cd ~(Go to home directory)cd -(Go back to the previous directory)
ls(List): Lists directory contents.ls -la(List all files, including hidden ones, in a long-format list)
tree: Displays directories as a tree (may require installation viaapt install tree).
Creating & Destroying
Section titled “Creating & Destroying”touch: Creates an empty file or updates the timestamp of an existing file.touch config.yaml
mkdir(Make Directory): Creates a new folder.mkdir -p /app/data/logs(The-pflag creates parent directories if they don’t exist).
rm(Remove): Deletes files or directories. Warning: There is no recycle bin in Linux!rm file.txt(Delete a file)rm -rf /folder(Recursively and forcefully delete a folder and all its contents)
Copying & Moving
Section titled “Copying & Moving”cp(Copy): Copies files or directories.cp source.txt dest.txtcp -r /source_dir /dest_dir(Recursive copy for folders)
mv(Move): Moves or renames files and directories.mv old_name.txt new_name.txt(Rename)mv file.txt /var/www/(Move)
04.2 Viewing Files
Section titled “04.2 Viewing Files”When troubleshooting, you often need to read configuration files and logs without opening a full text editor.
cat(Concatenate): Prints the entire contents of a file to the terminal. Best for short files.less&more: Pagers that let you scroll through large files one screen at a time. (lessis modern and allows backward scrolling).- Press
qto quit,/to search forward.
- Press
head: Prints the first 10 lines of a file.head -n 20 file.txt(Prints first 20 lines)
tail: Prints the last 10 lines of a file.tail -f /var/log/syslog(The-fflag “follows” the file, printing new lines in real-time as they are written. Essential for watching logs!)
04.3 Searching for Files & Commands
Section titled “04.3 Searching for Files & Commands”find: The most powerful tool for searching the filesystem based on various criteria.find /var/log -name "*.log"(Find all files ending in .log in /var/log)find / -type f -size +1G(Find all files larger than 1GB)
locate: A faster alternative tofindthat searches a pre-built database (requires runningupdatedb).which: Shows the absolute path of the executable that would run if you typed a command.which docker(Outputs/usr/bin/docker)
whereis: Similar towhich, but also locates the source and manual pages for a command.
04.4 Text Processing
Section titled “04.4 Text Processing”Linux treats everything as text. These tools allow you to slice, filter, and modify text streams.
grep: Searches text for matching patterns.grep "ERROR" /var/log/sysloggrep -i "error" file.txt(Case-insensitive)
sort: Sorts lines of text alphabetically or numerically.uniq: Removes duplicate consecutive lines. Often paired withsort.wc(Word Count): Counts lines, words, and characters.wc -l file.txt(Counts the number of lines)
cut: Extracts specific columns or fields from a file.cut -d':' -f1 /etc/passwd(Extracts the first column, delimited by a colon)
awk&sed: Advanced text processing languages.awk '{print $2}' file.txt(Prints the second column of every line)sed 's/old/new/g' file.txt(Finds “old” and replaces it with “new”)
tr(Translate): Translates or deletes characters.tr 'a-z' 'A-Z'(Converts input to uppercase).
04.5 Pipes & Redirection
Section titled “04.5 Pipes & Redirection”The true power of Linux comes from chaining small, single-purpose tools together using Pipes and Redirection.
Output Redirection
Section titled “Output Redirection”By default, commands output to the terminal (Standard Output / stdout).
>(Overwrite): Redirects output to a file, overwriting the file if it exists.echo "Hello" > file.txt
>>(Append): Redirects output to a file, appending to the end.echo "World" >> file.txt
Input & Error Redirection
Section titled “Input & Error Redirection”<(Input): Passes the contents of a file as input to a command.2>(Standard Error): Redirects errors (stderr) to a file instead of the screen.find / -name "secret" 2> errors.log
2>&1: Redirectsstderrto the same place asstdout. (Combines regular output and errors).
The Pipe (|)
Section titled “The Pipe (|)”Takes the output of the command on the left, and uses it as the input for the command on the right.
# List all running processes, but filter only the ones containing "nginx"ps aux | grep nginx
# Read a massive log file, find errors, and count how many there arecat /var/log/syslog | grep "ERROR" | wc -l04.6 Command Chaining
Section titled “04.6 Command Chaining”You can execute multiple commands in a single line based on logical conditions.
;(Sequential): Runs the second command regardless of whether the first succeeded.mkdir new_folder ; cd new_folder
&&(Logical AND): Runs the second command ONLY if the first command succeeds (exit code 0).apt update && apt upgrade -y
||(Logical OR): Runs the second command ONLY if the first command fails.cat config.txt || echo "File not found!"