DEV Community

Cover image for Keep calm and learn Linux
osagie anolu
osagie anolu

Posted on

Keep calm and learn Linux

Comprehensive Guide to Essential Linux Commands

Image description
Linux is a powerful, open-source operating system that’s highly popular among developers, sysadmins, and IT professionals due to its flexibility, stability, and command-line interface (CLI). Mastering Linux commands is crucial for efficient system management and development tasks. This guide introduces essential Linux commands, covering everything from navigating directories to managing files and permissions.

Table of Contents

  1. Getting Started with Linux Basics

  2. File and Directory Management

  3. File Permissions and Ownership

  4. Working with Text Files

  5. Process Management

  6. Networking and System Information

  7. Conclusion


Image description

Image description

Getting Started with Linux Basics

Before diving into Linux commands, open a terminal window to access the command line. For beginners, understanding the basic navigation commands is a great starting point.

pwd - Displays the current working directory.

pwd

This command is handy to check which directory you’re in.

cd - Changes the directory.

cd /path/to/directory

Use cd followed by the path to navigate to a specific directory. cd .. moves up one directory level, and cd ~ brings you back to your home directory.


File and Directory Management

Linux allows comprehensive control over files and directories, which is crucial for organization and system structure.

ls - Lists files and directories.

ls

Adding -l provides a detailed list, -a shows hidden files, and -h displays file sizes in a human-readable format. Combining options, like ls -lah, can give you detailed information on all files.

touch - Creates an empty file.

touch filename.txt

Use touch to create new, empty files.

mkdir - Creates a new directory.

mkdir directory_name

Adding -p creates parent directories if they don’t exist (e.g., mkdir -p parent/child).

cp - Copies files and directories.

cp source_file destination_file

Add -r to copy directories recursively.

mv - Moves or renames files and directories.

mv old_name new_name

This command can rename a file or move it to a new directory.

rm - Deletes files and directories.

rm filename.txt

To remove directories recursively, add -r. Be cautious with this command, as deletions are permanent by default.

Image description


File Permissions and Ownership

Linux uses permissions to secure files and directories. Permissions are divided into read (r), write (w), and execute (x) permissions, assigned to the file owner, group, and others.

chmod - Changes file permissions.

chmod 755 filename.txt

Permissions can be represented by numbers (755 gives full access to the owner, read and execute to the group, and read-only to others). Use symbolic notation like chmod u+x filename.txt to add execute permission for the user.

chown - Changes file ownership.

chown user:group filename.txt

Only root or users with sudo privileges can change ownership. You can also add -R to recursively change ownership within directories.

Image description

Working with Text Files

Manipulating text files is a common task in Linux. These commands help you view, edit, and search through files.

cat - Displays file contents.

cat filename.txt

Add -n to display line numbers. To concatenate multiple files, list them one after another.

less and more - Views file content page by page.

less filename.txt

less is more flexible for navigating large files, while more only allows forward movement.

head and tail - Views the beginning or end of a file.

head -n 10 filename.txt
tail -n 10 filename.txt

Both commands display the first or last 10 lines by default. Adjust the number with -n.

nano, vim, and gedit - Edits files.

nano filename.txt

nano and vim are terminal-based editors, while gedit is GUI-based. nano is beginner-friendly, and vim offers more powerful editing capabilities.

grep - Searches within files.

grep "search_term" filename.txt

Add -i for case-insensitive searches, -r for recursive searches in directories, and -v to exclude matches.


Process Management

Linux allows direct interaction with processes, from viewing to killing unresponsive ones.

ps - Lists currently running processes.

ps aux

Adding aux displays detailed information about all processes, including user and memory usage.

top - Provides a real-time view of system processes.

top

Press q to quit. For a more user-friendly interface, use htop, which may need to be installed first.

kill - Terminates a process by its PID (Process ID).

kill 1234

Use ps or top to find the PID. To force termination, use kill -9 1234.

killall - Kills processes by name.

killall process_name

This command terminates all instances of a given process name.

Image description


Networking and System Information

Linux provides various commands to check networking and system stats, which are essential for troubleshooting and configuration.

ifconfig or ip - Views and configures network interfaces.

ifconfig
ip addr show

ifconfig is older but commonly used. ip is more versatile and part of newer Linux distributions.

ping - Tests connectivity to a host.

ping google.com

ping sends packets to the target to test connection. Add -c to limit the number of packets sent (e.g., ping -c 4 google.com).

netstat - Displays network connections.

netstat -tuln

Add -tuln to show active connections and listening ports. Note that ss is often recommended as a modern alternative.

df - Displays disk usage.

df -h

Add -h for human-readable output. This command shows available disk space on all mounted filesystems.

du - Estimates file and directory space usage.

du -h filename_or_directory

Use -h for human-readable sizes, and add -s for a summary.

uname - Shows system information.

uname -a

uname -a provides detailed info about the kernel and system architecture.

uptime - Shows how long the system has been running.

uptime

This command displays the system's uptime, current time, and load averages.

Mastering these Linux commands is key to efficiently managing files, processes, permissions, and networking. While this guide provides a strong foundation, the Linux CLI offers even more flexibility and power. By building on this knowledge, you can unlock the full potential of Linux as an operating system for development, administration, and automation. Happy coding and exploring Linux!

Top comments (0)