Top Linux Commands Every DevOps Engineer Should Know
Command your Success: Key Linux Commands for DevOps Mastery
What is Linux ?
Linux is a type of operating system, similar to Windows or macOS, it's is an open-source OS(it's free and open for anyone to use, modify, and distribute). It's known for being stable, secure, and able to run on a wide range of devices from computers to smartphones and even supercomputers.
What are Linux commands?
Linux commands are instructions used in a command-line interface (CLI) to perform tasks on a Linux-based operating system.
Here are the few essential Linux commands
ls : List the files and directories in the current directory.
ls -a : Lists all files and directories, including hidden ones (starting with
.
).ls -l : Provides a detailed listing format with information like permissions, owner, size, and modification date.
ls directory_name ls -a directory_name ls -l directory_name
cd - change the current directory
cd ..-changes the current directory up one level to the parent directory.
cd directory_name
- View all environment variables:
print env
- View a specific environment variable:
echo $VARIABLE_NAME
Set a temporary environment variable (for the current session):
export VARIABLE_NAME=value
Remove an environment variable:
unset VARIABLE_NAME
Add a permanent environment variable (usually by adding it to
~/.bashrc
,~/.bash_profile
, or~/.profile
):
echo 'export VARIABLE_NAME=value' >> ~/.bashrc
source ~/.bashrc
pwd : The
pwd
(print working directory) command in Unix-like operating systems, including Linux, is used to display the full, absolute path of the current working directory.pwd
tr : the
tr
command in Unix-like operating systems, which is used for translating or deleting characters. It's a very powerful tool for text processing.cat : cat is used to display the contents of files, concatenate files, and redirect output in the terminal
Display File Contents:
cat file.txt
cat file_name | tr a-z A-z > new_filename : The command reads the content of
file_name
, converts all lowercase letters to uppercase, and writes the transformed content tonew_filename
.cat file_name | tr 'a-z' 'A-Z' > new_filename
echo : The echo is used to display a line of text or a string to the standard output (the terminal). It is a simple yet powerful command frequently used in shell scripts and command-line operations.
echo [OPTIONS] [STRING]
man : The man is used to display the manual pages for commands, utilities, and other topics. It provides comprehensive documentation and usage instructions directly from the terminal.
man [OPTION]... [COMMAND/SECTION]
mkdir : The mkdir is used to create directories (folders) in the file system.
mkdir folder_name
mkdir -p : If you want to create a directory structure where "middle" is the new directory between "parent" and "sub directory"
mkdir -p parent/child/grandchild
touch : The touch command in Linux is used to create new empty files or update the timestamp of existing files without changing their content.
touch filename.txt
cp : The 'cp' command in Linux is used to copy files and directories.
cp file1.txt /path/to/destination/
cp -r : Copy a directory and its contents recursively.
cp -r directory1 /path/to/destination/
The
-r
option recursively copiesdirectory1
and its contents to/path/to/destination/
.mv : The 'mv' command in Linux is used to move or rename files and directories.
Rename a file or directory:
mv old_name new_name
Move a file to a different directory:
mv file_name directory/
Move multiple files to a directory:
mv file1 file2 file3 directory/
rm : The 'rm' command in Linux is used to remove files or directories.
Remove a file:
rm file_name
Remove a directory and its contents recursively (use with caution):
rm -r directory_name
Force removal (without prompting for confirmation):
rm -f file_name
rm -rf : The
rm -rf
command in Linux is a powerful and potentially dangerous command combination that forcefully removes files and directories without prompting for confirmation.Remove a directory and its contents recursively:
rm -rf directory_name
sudo: The
sudo
command in Linux stands for "superuser do." It allows a permitted user to execute a command as the superuser or another user, as specified by the security policy configured in thesudoers
file.Execute a single command as superuser:
sudo command_name
Change file permissions or ownership:
sudo chmod 644 file_name sudo chown user_name:group_name file_name
df : The
df
command in Linux is used to display the amount of disk space used and available on filesystems.df
vi :
vi
is a powerful text editor used in Unix and Unix-like systems. It operates in two modes: command mode and insert mode.Opening and Editing Files:
Open a file:
vi filename
Switch to insert mode (for typing text):
Press
i
to insert text before the cursor.Press
a
to append text after the cursor.
Save changes and exit:
While in command mode, type
:wq
and press Enter to save and quit.Alternatively, you can use
ZZ
(uppercaseZ
twice) in command mode to save and exit.
Exit without saving changes:
- In command mode, type
:q!
and press Enter to quit without saving changes.
- In command mode, type
Navigation and Editing:
Move the cursor:
- Use arrow keys or
h
(left),j
(down),k
(up),l
(right).
- Use arrow keys or
Delete characters:
Press
x
to delete the character under the cursor.Press
dd
to delete the entire line.
Copy, cut, and paste:
Position the cursor where you want to start.
Press
yy
to copy (yank) a line.Press
p
to paste below the cursor, orP
to paste above.
Search and replace:
While in command mode, use
/search_term
to search forward.Use
n
to find the next occurrence.Use
:%s/old_text/new_text/g
to replace all occurrences ofold_text
withnew_text
.
Additional Tips:
Undo changes:
- Press
u
in command mode to undo the last change.
- Press
Save changes without exiting:
- In command mode, type
:w
and press Enter to save changes.
- In command mode, type
Open multiple files:
- Use
vi file1 file2
to open multiple files.
- Use
vi
is a modal editor, meaning it has different modes for typing text (insert mode
) and for commands (command mode
). Mastering vi
requires practice, but it's a versatile editor once you get familiar with its commands and shortcuts.
head: The
head
command in Linux is used to output the first part (head) of files or input streams. By default, it displays the first 10 lines of each specified file or standard input.Display the first 10 lines of a file:
head file_name
Display the first N lines (e.g., first 20 lines) of a file:
head -n 20 file_name
tail : The
tail
command in Linux is used to output the last part (tail) of files or input streams. By default, it displays the last 10 lines of each specified file or standard inputDisplay the last 10 lines of a file:
sh
Display the last N lines (e.g., last 20 lines) of a file:
tail -n 20 file_name
diff : The
diff
command in Linux is used to compare files line by line. It identifies differences between two files and outputs them to the terminal.Compare two files and display differences:
diff file1.txt file2.txt
locate :
locate
is a fast and efficient way to find files by name or pattern across the filesystem, making it a convenient tool for system administrators and users who need to locate files quickly.Search for a file by name:
locate filename
find : The
find
command in Linux is used to search for files and directories in a directory hierarchy based on various criteria. It's a powerful tool for locating files that match specific patterns or conditions.Find files by name:
find /path/to/search -name "filename"
Find files by wildcard pattern:
find /path/to/search -name "*.txt"
Find directories:
find /path/to/search -type d
To find all
.txt
files in the current directory and its subdirectories:find . -type f -name "*.txt"
chmod : The
chmod
command in Linux is used to change the permissions (read, write, execute) of files and directories. It stands for "change mode.chmod [options] mode file
Here's a breakdown of the components and how to use chmod
:
Modes can be specified in two ways: symbolic and numeric.
Symbolic Mode
In symbolic mode, you use characters to represent permissions:
r
: readw
: writex
: execute-
: no permission
You also use characters to represent the user classes:
u
: user (file owner)g
: groupo
: othersa
: all (user, group, and others)
Some symbolic operators are:
+
: adds the specified modes-
: removes the specified modes=
: sets the specified modes and removes the others
example:
chmod u+x filename # Add execute permission for the user chmod g-w filename # Remove write permission for the group chmod o=r filename # Set read-only permission for others
Numeric Mode
In numeric mode, permissions are represented by an octal number:
4
: read2
: write1
: execute
These numbers are summed to set multiple permissions. The three digits represent the user, group, and others, respectively.
chmod 755 filename # rwxr-xr-x (user: read/write/execute, group: read/execute, others: read/execute)
chmod 644 filename # rw-r--r-- (user: read/write, group: read, others: read)
chmod 777 filename # rwxrwxrwx (all permissions for everyone)
whoami: The
whoami
command in Linux is used to display the username of the currently logged-in user. It's a simple command with no options or arguments.whoami
grep:
grep
is used to search text using patterns.grep [options] pattern [file...]
Common Options
-i
: Ignore case distinctions.-v
: Invert the match, showing lines that do not match the pattern.-r
or-R
: Recursively search directories.-l
: Show only the names of files containing matches.-n
: Prefix each line of output with the line number within its input file.-w
: Match whole words only.-c
: Count the number of matching lines.--color
: Highlight the matching strings.
grep 'pattern' file.txt # Simple pattern search
grep -i 'pattern' file.txt # Case-insensitive search
grep -r 'pattern' directory/ # Recursive search in directory
grep -n 'pattern' file.txt # Show line numbers
grep -v 'pattern' file.txt # Inverted match
- history:
history
displays the command history of the current session.
Common Options
-c
: Clear the history list.-w
: Write the current history to the history file.
history # Display command history
history -c # Clear the command history
history -w # Write current history to the history file
- ping:
ping
checks the network connection to a host.
ping [options] destination
Common Options:
-c count
: Stop after sending count packets.-i interval
: Wait interval seconds between sending each packet.-t ttl
: Set the time to live.
sort: sort
sorts lines of text files.
Common Options
-r
: Reverse the result of comparisons.-n
: Compare according to numerical value.-k
: Sort via a key.-u
: Output only the first of an equal run.
Examples
sort file.txt # Sort file.txt alphabetically
sort -r file.txt # Sort in reverse order
sort -n file.txt # Sort numerically
sort -k 2 file.txt # Sort by the second column
sort -u file.txt # Sort and remove duplicates
zip: zip
compresses files into a ZIP archive.
Common Options
-r
: Recurse into directories.-m
: Move files into zipfile (delete files after including them in zip archive).-q
: Quiet mode.
Examples
zip archive.zip file1.txt file2.txt # Create archive.zip containing file1.txt and file2.txt
zip -r archive.zip directory/ # Create archive.zip with all files in directory
zip -m archive.zip file1.txt # Move file1.txt into archive.zip
unzip: unzip
extracts files from a ZIP archive.
Common Options
-l
: List contents of ZIP archive.-d
: Extract files into a different directory.-o
: Overwrite existing files without prompting.
Examples
unzip archive.zip # Extract files from archive.zip
unzip -l archive.zip # List contents of archive.zip
unzip archive.zip -d /path/to/dir # Extract into specified directory
unzip -o archive.zip # Overwrite existing files without prompting