Essential Linux Commands for DevOps
A task-based Linux command reference: navigating and managing files, reading logs, grep, sed, cut and awk, redirection, Vim, services, and basic networking.

ON THIS PAGE
Most server work over SSH comes down to a few dozen commands: moving through the file system, reading logs, searching and editing text, and controlling services. This reference groups those commands by task, with the flags that matter and a short example for each. Users, permissions, packages, archives and processes are covered in the next part, so each command is explained only once.
Getting help
| Command | What it does |
|---|---|
Tab | Completes a command, file or directory name |
Tab Tab | Shows all possible completions |
man ls | Opens the manual page for a command (press q to quit) |
ls --help | Prints a short list of options |
info ls | Opens the longer GNU info page |
apropos copy | Searches manual page descriptions; useful when you forget a command's name |
Navigating directories
| Command | What it does |
|---|---|
pwd | Prints the current directory |
cd /opt | Goes to an absolute path |
cd .. | Goes up one level |
cd ../.. | Goes up two levels |
cd - | Goes back to the previous directory |
cd ~ or cd | Goes to your home directory |
Listing with ls
| Command | What it does |
|---|---|
ls | Lists the contents of the current directory |
ls -a | Includes hidden files (names that start with .) |
ls -l | Long list: permissions, owner, size, date |
ls -al | Long list including hidden files |
ls -lh | Long list with human-readable sizes (4.0K, 1.2G) |
ls -lr | Long list in reverse order |
ls -lt | Long list sorted by modification time, newest first |
ls -ltu | Long list sorted by last access time |
ls -i | Shows inode numbers (what an inode is) |
tree / tree -a | Shows a directory tree, -a includes hidden files (install with sudo apt install tree) |
The first character of each ls -l line is the file type (- file, d directory, l link). All seven types are listed in Introduction to Linux.
Creating, copying, moving and deleting
$ touch notes.txt
$ mkdir project
$ mkdir -p devops/scripts/yaml
$ mkdir dir{1..5}mkdir -p creates missing parent directories. dir{1..5} is brace expansion: the shell turns it into dir1 dir2 dir3 dir4 dir5.
| Command | What it does |
|---|---|
cp report.txt backup/ | Copies a file |
cp -i a.txt b.txt | Asks before overwriting |
cp -r dir1/ dir2/ | Copies a directory and everything inside it |
mv old.txt new.txt | Renames a file |
mv file.txt /home/user/docs/ | Moves a file |
mv dir1 dir2 | Moves dir1 into dir2 (or renames it if dir2 does not exist) |
mv -i a.txt b.txt | Asks before overwriting |
rm file.txt | Deletes a file |
rm -i file.txt | Asks before deleting |
rm -r dir | Deletes a directory and its contents |
rm -rf dir | Same, without any prompts |
rmdir dir or rm -d dir | Deletes a directory only if it is empty |
mv does not need -r; it moves directories with their contents by default.
To inspect a file:
| Command | What it does |
|---|---|
file name | Shows the file type (text, binary, directory) |
stat name | Shows size, permissions, owner, inode and timestamps |
Reading files and logs
| Command | What it does |
|---|---|
cat file | Prints the whole file |
cat -n file | Prints with line numbers |
cat file1 file2 | Prints several files one after another |
tac file | Prints the file with the last line first |
head file | First 10 lines |
head -n 20 file | First 20 lines |
tail file | Last 10 lines |
tail -n 20 file | Last 20 lines |
tail -f file | Follows the file and prints new lines as they arrive |
wc file | Counts lines, words and bytes |
uniq file | Removes repeated lines that are next to each other (use sort file | uniq for all duplicates) |
For long files, use a pager:
more file:Entermoves one line,Spacemoves one page.less file(more capable):Spacenext page,bprevious page,dhalf a page down,/textsearches forward,nnext match,qquits.
The most useful logs for troubleshooting live in /var/log:
| Log file | What it contains |
|---|---|
/var/log/syslog | General system and service messages |
/var/log/dpkg.log | Package installs and removals |
/var/log/cloud-init-output.log | Output of user data scripts that run when a cloud VM first boots |
$ tail -f /var/log/syslog
$ wc -l /var/log/syslogFinding files
find
find walks the directory tree and checks each file against your tests.
$ sudo find / -name dpkg.log
$ find . -name "*.log"
$ find / -type d -name Pictures
$ find . -type d -name "*terraform*"
$ sudo find / -type l -ls
$ sudo find /etc/ -group shadow -ls| Option | Searches by |
|---|---|
-name | File name (quote patterns with *) |
-type | Type: f file, d directory, l link |
-user / -group | Owner or group |
-inum | Inode number |
-ls | Prints details for each match |
The starting point matters: / searches the whole system (an absolute path), . searches from the current directory (a relative path).
locate
locate is faster than find because it searches a prebuilt database instead of the disk. Refresh the database with updatedb to include new files.
$ sudo apt install plocate
$ sudo updatedb
$ locate dpkg.log
$ locate -i readme.mdOlder releases use the mlocate package; newer Debian and Ubuntu releases ship plocate. -i ignores case.
which
which shows the full path of a command:
$ which aptSearching and editing text
grep
grep prints the lines of a file that match a pattern.
| Command | What it does |
|---|---|
grep name test.txt | Lines containing name (case-sensitive) |
grep -E "cat|dog" test.txt | Enables Extended Regular Expressions (ERE) for advanced patterns (e.g., alternation with |) |
grep -i name test.txt | Same, ignoring case |
grep -n name test.txt | Adds line numbers |
grep -v hello test.txt | Lines that do not contain hello |
grep -r error /var/log/ | Searches all files under a directory |
grep "^name" test.txt | Lines that start with name |
grep "name$" test.txt | Lines that end with name |
grep "[nN]ame" test.txt | Matches name or Name |
grep "[0-9][0-9]$" test.txt | Lines that end with two digits |
^, $ and [...] are regular expression symbols. Quote the pattern so the shell does not change it.
sed
sed (stream editor) processes text line by line. It prints selected lines of a file and performs find-and-replace, which makes it a standard tool in scripts.
$ sed -n '3p' /etc/passwd
$ sed -n '$p' /etc/passwd
$ sed -n '3,6!p' /etc/passwd
$ sed -e 's/ansible/devops/g' /etc/passwd
$ sed -e 's/ansible/devops/g' word.txt > newtmp.txt
$ sed -i 's/Suryaraj/raj/g' abc.txt-nstopssedfrom printing every line;pprints only the lines you select.3pis line 3,$pis the last line,3,6!pis everything except lines 3 to 6.- In
s/find/replace/g,smeans substitute andgreplaces every match on the line, not only the first. -egives an expression; the result goes to the screen and the file is unchanged.-iedits the file in place.
In my WordPress lab, sed set the database name in the WordPress config file, running as the www-data user:
$ sudo -u www-data sed -i 's/database_name_here/wordpress/' /srv/www/wordpress/wp-config.phpcut
cut extracts characters or fields (columns) from each line. A delimiter is the character that separates fields, such as : in /etc/passwd.
| Command | What it does |
|---|---|
cut -c1 /etc/passwd | First character of each line |
cut -c1-5 /etc/passwd | Characters 1 to 5 |
cut -d ':' -f1 /etc/passwd | First field, using : as the delimiter (the usernames) |
cut -d ':' -f2-3 /etc/passwd | Fields 2 to 3 |
cut -d ' ' -f1 file | First field, using a space as the delimiter |
awk
awk is a small language for processing text, mostly used to print columns and build quick reports. The syntax is awk '/pattern/ {action}' file.
awk splits each line on whitespace and stores the fields in $1, $2, $3 and so on; $0 is the whole line. -F sets a different field separator. Use single quotes so the shell does not expand $1.
$ awk -F ':' '{print $1}' /etc/passwd
$ df -h | awk '{print $2}'
$ awk '/error/ {print $0}' /var/log/syslogRedirection and pipes
Every command has three streams:
| Stream | Number | Example |
|---|---|---|
| Standard input (stdin) | 0 | wc with no file waits for you to type; press Ctrl+D to finish |
| Standard output (stdout) | 1 | The normal output of ls, whoami, date |
| Standard error (stderr) | 2 | Messages like command not found, No such file or directory, Permission denied |
| Operator | What it does |
|---|---|
> or 1> | Sends stdout to a file and overwrites it |
>> | Sends stdout to a file and appends to it |
2> / 2>> | Sends stderr to a file (overwrite / append) |
&> | Sends both stdout and stderr to a file |
> /dev/null | Throws the output away (/dev/null discards everything written to it) |
< | Reads stdin from a file |
<< EOF | Here document: reads stdin from the lines that follow, up to EOF |
| | Pipe: sends stdout of one command to stdin of the next |
$ who > users.txt
$ date >> command.out
$ whoami >> command.out
$ lss 2> error.out
$ ls /binn 2>> error.out
$ wc -l << EOF
This is a simple program
EOFtee writes its input to a file and to the screen. tee overwrites the file and tee -a appends. Combined with a here document, tee -a adds several lines to a file in one command:
$ tee -a filename << EOF
We are adding some content
to the file filename.
EOFsudo tee applies the same pattern to files owned by root. sudo echo ... >> file fails there, because the shell performs the redirection as your own user.
Editing files with Vim
Vim (Vi Improved), or at least vi, is present on almost every server, which makes it the editor you can always rely on over SSH. Install it with sudo apt install vim (Debian, Ubuntu) or sudo dnf install vim (RHEL, Fedora).
Vim has three main modes:
| Mode | How to enter it | Used for |
|---|---|---|
| Normal mode | Esc | Moving around and editing with keys |
| Insert mode | i (before cursor), a (after cursor) | Typing text |
| Command-line mode | : from normal mode | Saving, quitting, search and replace |
Moving around (normal mode)
| Keys | Action |
|---|---|
h j k l | Left, down, up, right |
w / b / e | Next word / previous word / end of word (3w jumps three words) |
0 / $ | Start / end of the line |
gg / G / 2G | First line / last line / line 2 |
f x / F x | Next / previous occurrence of character x on the line |
* / # | Next / previous occurrence of the word under the cursor |
/text then n / N | Search forward, then next / previous match |
Editing (normal mode)
| Keys | Action |
|---|---|
o / O | New line below / above and enter insert mode |
x / X | Delete the character under / before the cursor |
r | Replace the character under the cursor |
dw / d2e | Delete a word / delete to the end of the second word |
dd / 3dd | Delete a line / three lines |
yy / 3yy | Copy a line / three lines |
p / P | Paste below / above |
u / U / Ctrl+r | Undo / undo all changes on the line / redo |
v | Visual mode: select text with movement keys, then d to delete or y to copy |
Saving and quitting (command-line mode)
| Command | Action |
|---|---|
:w | Save |
:q | Quit |
:wq or :x | Save and quit |
:q! | Quit without saving |
:w! / :wq! | Force save / force save and quit |
:20 | Go to line 20 |
:set nu / :set nonu | Show / hide line numbers |
:%s/old/new/ | Replace the first old on every line |
:%s/old/new/g | Replace every old in the file |
:help | Open Vim help |
vim +7 test.txt opens the file with the cursor on line 7.
System information
| Command | What it does |
|---|---|
date | Current date and time |
timedatectl | Time, time zone and NTP sync status |
sudo timedatectl set-timezone Asia/Kathmandu | Changes the time zone |
sudo timedatectl set-ntp true | Turns on NTP time sync (fixes clock drift) |
hostname | Shows the host name |
sudo hostnamectl set-hostname jenkins | Changes the host name |
whoami | Shows the current username |
df -h | Disk usage per file system, human-readable |
df -hi | Inode usage per file system |
du -sh * | Size of each item in the current directory |
free -m | Memory usage in MB |
echo $SHELL | Your login shell |
echo $? | Exit code of the last command: 0 means success, anything else means failure |
clear | Clears the screen |
passwd | Changes your password |
history lists the commands you ran before. !136 runs command number 136 again and !! repeats the last command (sudo !! reruns the last command with sudo).
Networking and downloads
| Command | What it does |
|---|---|
ip addr show | All network interfaces and their IP addresses |
ip -4 addr show | Only IPv4 addresses |
ifconfig | Older tool with similar output; needs the net-tools package |
wget URL | Downloads a file into the current directory |
curl URL | Prints the response to the screen |
curl -o file.zip URL | Saves the response as file.zip |
curl -O URL | Saves the response with the remote file name |
curl -k URL | Skips TLS certificate checks (only for testing self-signed certificates) |
curl localhost | Quick check that a local web server answers |
Managing services with systemctl
systemctl controls services (background programs) on systems that use systemd, which includes current Ubuntu, Debian and RHEL.
| Command | What it does |
|---|---|
sudo systemctl start apache2 | Starts a service now |
sudo systemctl stop apache2 | Stops it |
sudo systemctl restart apache2 | Stops and starts it |
sudo systemctl reload apache2 | Re-reads the config without a full restart (if the service supports it) |
sudo systemctl enable apache2 | Starts it automatically at boot |
sudo systemctl disable apache2 | Stops starting it at boot |
systemctl status apache2 | Shows state and recent log lines |
systemctl is-active apache2 | Prints active or inactive |
The older service command still works and calls systemd underneath: sudo service apache2 status (also start, stop, restart, reload). To write your own service, see Running Tomcat as a systemd Service.
Key takeaways
man,--helpandaproposanswer most "how do I" questions without leaving the terminal.tail -f,grep,lessand/var/logare the core of log debugging.>overwrites and>>appends; send errors with2>and discard output with/dev/null.- Test
sedwithout-ibefore editing a file in place, and double-check everyrm -rf. systemctl enablecontrols boot behaviour;startandstopcontrol the service right now.
Next in this series: Linux Users, Permissions and Package Management.
Keep reading
- Linux Users, Permissions and Package Management
Manage users, groups, sudo, file permissions, links, apt packages, archives and processes on Ubuntu, with the commands and the files behind each one.
- How ssh works
Ever wondered what actually happens behind the scenes when you open your terminal and type ssh user@server
- tmux Cheat Sheet: Sessions, Windows, Panes and Copy Mode
A tmux reference for remote work: sessions that survive SSH drops, windows, split panes, copy mode, synchronized panes, and a minimal tmux.conf.