DEV Community

Cover image for Linux Command-Line Fundamentals: A Practical Guide to 32 Essential Linux Commands
Ebhomien Oserieme
Ebhomien Oserieme

Posted on

Linux Command-Line Fundamentals: A Practical Guide to 32 Essential Linux Commands

Introduction

Linux is an open-source operating system widely used in software development, cloud computing, server administration and DevOps. One of its major features is the command-line interface (CLI), which allows users to interact with the operating system by entering text-based instructions known as commands.

Linux commands enable users to navigate directories, manage files, inspect system information, monitor logs, search for specific information and perform administrative tasks.

Understanding these commands is an important skill for anyone interested in Linux system administration, cybersecurity, software engineering and cloud computing.

This article explores 32 Linux commands and command combinations, using practical examples to explain their functions, syntax and applications. The commands are based on the Linux Navigation Drill provided by Real Cloud Projects, with additional explanations to help beginners understand how they work.

Understanding the Linux Command Line

The Linux command line is a text-based interface through which users communicate with the operating system.

Users enter commands into a terminal, and Linux processes those instructions to perform specific operations.

A typical Linux command follows this structure:
command [options] [arguments]

*Command: The instruction to execute.

*Options: Modify how the command behaves.

*Arguments: Specify the file, directory or resource on which the command operates.

For example:

ls -lh /var/log
Here:

  • ls is the command.
  • -lh represents the options.
  • /var/log is the directory being listed.

Essential Linux Commands

I've arranged your commands into related sections to make the article easier to understand.

Checking Your Current Location and Listing Files

Command 1: pwd

Meaning: Print Working Directory.

Purpose: Displays the full path of the directory in which the user is currently working.

Syntax:
pwd

Explanation: When executed, Linux displays the current working directory. This helps users identify their location within the Linux file system.

Print working directory testing

Command 2: ls

Meaning: List.

Purpose: Displays files and directories in the current working directory.

Syntax:
ls

Explanation: This command provides a quick overview of the files and folders available in the current directory.

Listing directory testing

Command 3: ls -l

Purpose: Displays files and directories in long-listing format.

Syntax:
ls -l

Explanation: Unlike the ordinary ls command, this option displays additional information, including file permissions, ownership, file size and modification date.

Command 4: ls -lh

Purpose: Displays files and directories in long format with human-readable file sizes.

Syntax:
ls -lh

Explanation: The -h option makes file sizes easier to understand by displaying them in units such as KB, MB and GB instead of only bytes. It is commonly combined with -l.

Command 5: ls -la

Purpose: Displays all files and directories, including hidden files, in long-listing format.

Syntax:
ls -la

Explanation: In Linux, hidden files usually begin with a dot (.). The -a option displays these hidden files, while -l provides detailed information about each entry.

List all files testing

Command 6: tree

Purpose: Displays directories and their contents in a tree-like structure.

Syntax:
tree

Explanation: This command provides a visual representation of how files and directories are organized. It makes it easier to understand the hierarchical structure of a Linux file system.

Tree testimg

Searching for Files and Monitoring System Logs

Command 7: Finding large log files

sudo find /var/log -type f -exec du -h {} + 2>/dev/null | sort -rh | head -5

Purpose: Searches for the five largest files in the /var/log directory.

Explanation of the components:

  • sudo: Executes the command with administrative privileges.
  • find /var/log: Searches within the system log directory.
  • -type f: Restricts the search to regular files.
  • -exec du -h {} +: Calculates the sizes of the files in human-readable format.
  • 2>/dev/null: Redirects error messages so they are not displayed.
  • sort -rh: Sorts the results in reverse order by human-readable size.
  • head -5: Displays only the first five results. Practical importance: This command is useful when investigating disk-space usage and identifying unusually large log files.

Finding large log testing

Command 8: Monitoring the last 50 lines of a system log

tail -n 50 -f /var/log/syslog

Purpose: Displays the last 50 lines of a system log file and continues monitoring it for new entries.

Explanation:

  • tail: Displays the end of a file.
  • -n 50: Specifies that 50 lines should be displayed.
  • -f: Follows the file as new information is added. This is particularly useful for monitoring system activities and troubleshooting problems as they occur.

Display of last 50 commands

Command 9: cd /etc

Meaning: Change Directory.

Purpose: Moves the user into the /etc directory.

Syntax:
cd /etc

Explanation: The /etc directory contains system-wide configuration files used by Linux and installed applications. This command demonstrates how users navigate to a specific directory using its absolute path.

Change directory testing

Navigating Between Directories

Command 10: cd ..

Purpose: Moves the user one level up to the parent directory.

Syntax:
cd ..

Explanation: The two dots (..) represent the parent directory. This command is useful when navigating backwards through the directory structure.

moving to parent directory testing

Command 11: cd ~

Purpose: Takes the user directly to their home directory.

Syntax:
cd ~

Explanation: The tilde (~) represents the current user's home directory. Instead of manually entering the full directory path, users can use this shortcut.

taking user directly to home testing

Command 12: cd -

Purpose: Returns the user to the previous working directory.

Syntax:
cd -

Explanation: This command switches between the current and previous directories. It is particularly useful when working between two different locations.

return to working directory testing

Terminal Management and Command History

Command 13: clear

Purpose: Clears the visible contents of the terminal screen.

Syntax:
clear

Explanation: This command removes previously displayed terminal output from the screen, providing a cleaner workspace. It does not delete files or erase command history.

clear terminal screen

Command 14: history

Purpose: Displays a list of previously executed commands.

Syntax:
history

Explanation: "Shells such as Bash maintain a history of previously entered commands. The history command allows users to review and reuse commands they have executed."

This is more technically accurate because command history is managed by the shell.

History testing

Inspecting Files and System Information

Command 15: file /bin/bash

Purpose: Identifies the type of a file.

Syntax:
file /bin/bash

Explanation: The file command examines a file and reports information about its format or type. In this example, it identifies the Bash executable located at /bin/bash.

Identity of a file

Command 16: stat /etc/passwd

Purpose: Displays detailed information about a file.

Syntax:
stat /etc/passwd

Explanation: The stat command provides information such as file size, permissions, ownership, inode number and timestamps

"The /etc/passwd file stores user account information, including usernames, user IDs, group IDs, home directories and login shells. Despite its name, it does not normally store users' actual passwords."

Display  detailed information on a file

Command 17: cat /etc/hostname

Purpose: Displays the contents of the system hostname file.

Syntax:
cat /etc/hostname

Explanation: The cat command reads and displays file contents. In this example, it displays the hostname assigned to the Linux machine.

Display content of a file

Command 18: less /etc/services

Purpose: Allows users to view file contents one screen at a time.

Syntax:
less /etc/services

Explanation: The less command is useful when working with large text files because it allows users to move through the contents without displaying everything at once.

view file content testing

Command 19: head -n 5 /etc/passwd

Purpose: Displays the first five lines of a file.

Syntax:
head -n 5 /etc/passwd

Explanation: The head command displays the beginning of a file. The -n 5 option specifies that only the first five lines should be displayed.

This is useful when users need to inspect the beginning of a file without reading the entire content.

Display first five line of a file

Command 20: tail /var/log/syslog

Purpose: Displays the last ten lines of a file by default.

Syntax:
tail /var/log/syslog

Explanation: This command displays the most recent entries in the system log file. It is useful when checking recent system activities and identifying errors.

Display of last ten line

Command 21: tail -f /var/log/syslog

Purpose: Continuously monitors a system log file.

Syntax:
tail -f /var/log/syslog

Explanation: Unlike the ordinary tail command, the -f option keeps the terminal open and displays new entries as they are added to the file.

This is useful for real-time system monitoring and troubleshooting. Press Ctrl + C to stop monitoring.

monitoring of system logs

Searching for Specific Information Using grep

Command 22: grep 'ERROR' /var/log/syslog

Purpose: Searches a file for lines containing a specific word or pattern.

Syntax:
grep 'ERROR' /var/log/syslog

Explanation: The grep command searches through text and displays lines matching the specified pattern. In this example, it searches the system log for the uppercase word ERROR.

Searching for specific words or patterns

Command 23: grep -i 'error' /var/log/syslog

Purpose: Searches for text without considering letter case.

Syntax:
grep -i 'error' /var/log/syslog

Explanation: The -i option makes the search case-insensitive. Therefore, it can find matches such as ERROR, Error and error.

Searching for text without considering lettercasing

Command 24: grep -n 'ERROR' /var/log/syslog

Purpose: Searches for a specific pattern and displays the corresponding line numbers.

Syntax:
grep -n 'ERROR' /var/log/syslog

Explanation: The -n option instructs Linux to display the line number alongside each matching result. This makes it easier to locate specific information within large files.

Search for specific command

Counting and Displaying Text

Command 25: wc /etc/passwd

Meaning: Word Count.

Purpose: Counts the number of lines, words and bytes in a file.

Syntax:
wc /etc/passwd

Explanation: The wc command provides a summary of the contents of a file. Its default output includes the line count, word count and byte count.

Counts the number of lines words and byte in a file

Command 26: echo 'Hello World'

Purpose: Displays a text message in the terminal.

Syntax:
echo 'Hello World'

Explanation: The echo command prints the supplied text to standard output. It is commonly used in shell scripts, simple tests and terminal operations.

Display text message in terminal

Creating and Modifying Files

Command 27: Creating a file and writing content into it

echo 'Investigation started' > report.txt && cat report.txt

Purpose: Creates a file named report.txt, writes text into it and displays its contents.

Explanation:

  • echo 'Investigation started': Produces the text.
  • >: Redirects the text into the file, creating it if necessary or overwriting its existing contents.
  • &&: Executes the next command only if the previous command succeeds.
  • cat report.txt: Displays the file contents.

Creating and writing content in a file

Command 28: Appending additional content to a file

echo 'Found timeout error' >> report.txt && cat report.txt

Purpose: Adds new text to an existing file without deleting its previous contents.

Explanation:

  • >>: Appends content to the end of a file.
  • &&: Ensures the next command runs only if the preceding command succeeds.
  • cat report.txt: Displays the updated file contents. The difference between > and >> is important: > overwrites existing content, while >> preserves existing content and adds new text.

Adds new text to exiting file

Combining Commands Using Pipes and Operators

Command 29: ls -la | wc -l

Purpose: Counts the number of lines produced by a detailed directory listing.

Syntax:
ls -la | wc -l

Explanation:

  • ls -la: Displays all files and directories, including hidden entries, in long format.
  • |: The pipe operator sends the output of one command to another.
  • wc -l: Counts the number of lines received. This demonstrates how Linux commands can be combined to perform more complex operations.

Note that the output includes the total line printed by ls -l, so it is not necessarily the exact number of directory entries.

Counts number of lines produced

Command 30: mkdir backup && cd backup

Purpose: Creates a directory named backup and moves into it.

Syntax:
mkdir backup && cd backup

Explanation:

  • mkdir backup: Creates a new directory called backup.
  • &&: Runs the next command only if directory creation succeeds.
  • cd backup: Changes the current working directory to the newly created directory. This command demonstrates how two operations can be linked together.

Creating a backup to move into

Command 31: cd /fake-dir || echo 'Directory missing!'

Purpose: Attempts to access a directory and displays an error message if the operation fails.

Syntax:
cd /fake-dir || echo 'Directory missing!'

Explanation:

  • cd /fake-dir: Attempts to change into the specified directory.
  • ||: Executes the next command only if the preceding command fails.
  • echo 'Directory missing!': Displays a message indicating that the directory could not be accessed. This demonstrates conditional command execution in Linux.

Attempt to access directory and display error command

Command 32: pwd ; ls

Purpose: Executes two commands sequentially.

Syntax:
pwd ; ls

Explanation:

  • pwd: Displays the current working directory.
  • ;: Separates commands and allows them to execute sequentially.
  • ls: Lists the files and directories in the current location. Unlike &&, the semicolon allows the second command to execute even if the first command fails.

Execute two commands sequentially

What I Learned

Through the Linux Navigation Drill, I gained practical knowledge of how to interact with the Linux operating system using the command line. I learned how to navigate directories, list files, identify hidden files, inspect system information and monitor log files.

I also gained an understanding of how commands can be combined using pipes, redirection operators and conditional operators to perform multiple operations efficiently.

These exercises improved my familiarity with the Linux terminal and provided a foundation for more advanced Linux administration and DevOps activities.

Importance of Linux Commands in DevOps and Cloud Computing

Linux commands are important in DevOps and cloud computing because many servers, cloud environments and container platforms rely on Linux-based systems.

Some practical applications include:

  • Server administration: Managing files, directories and system configurations.
  • Troubleshooting: Searching logs to identify errors and investigate system problems.
  • Automation: Combining commands to perform repetitive tasks.
  • Monitoring: Observing system logs and activities.
  • Cloud computing: Interacting with remote Linux virtual machines and cloud servers.
  • Container management: Working with Linux-based environments used by Docker and other container technologies.

Conclusion

Learning Linux commands is an important step towards understanding how Linux operating systems work. The 32 commands explored in this article demonstrate how users can navigate directories, manage files, inspect system information, monitor logs and combine commands to perform more complex operations.

Although these commands represent foundational Linux knowledge, they provide the skills needed to progress towards more advanced topics such as shell scripting, system administration, cloud computing and DevOps.

Regular practice is essential for becoming comfortable with the Linux command line and applying these skills in real-world technical environments.

Working through these commands helped me move beyond simply understanding Linux terminology to actually interacting with the operating system through the terminal. It also showed me how individual commands can be combined to solve practical problems.

References

  1. Real Cloud Projects. Linux Navigation Drill. https://realcloudprojects.io/?lab=linux-navigation-drill
  2. Ubuntu Documentation. Command-line cheat sheet. https://ubuntu.com/server/docs/reference/cli-cheatsheet/
  3. Linux manual pages. https://man7.org/linux/man-pages/

Top comments (0)