Mastering Basic Linux Commands: A Comprehensive Guide

Linux is a powerful and widely used operating system, especially favored in development and server environments. Understanding basic Linux commands is essential for anyone looking to enhance their productivity and manage their systems effectively. In this blog post, we will cover a comprehensive list of basic Linux commands that every user should know.

1. Navigating the File System

ls

Lists the contents of a directory.

  • Options:

    • -R: Lists all files in subdirectories.

    • -a: Shows all files, including hidden ones.

    • -lh: Displays file sizes in a human-readable format (MB, GB).

cd

Changes the current directory.

  • Usage: cd /directory/path

  • Shortcuts:

    • cd ..: Moves one directory up.

    • cd -: Switches to the previous directory.

pwd

Prints the current working directory's path.

2. Creating and Managing Directories

mkdir

Creates a new directory.

  • Usage: mkdir directory_name

  • Options:

    • -p: Creates parent directories as needed.

    • -m: Sets directory permissions.

rmdir

Removes an empty directory.

rm

Deletes files or directories.

  • Usage: rm filename

  • Options:

    • -i: Prompts for confirmation before deletion.

    • -f: Forces deletion without confirmation.

3. Moving and Copying Files

cp

Copies files and directories.

  • Usage: cp source destination

  • Options:

    • -R: Copies directories recursively.

mv

Moves or renames files and directories.

  • Usage: mv oldname newname or mv file.txt /new_directory/

touch

Creates a new empty file or updates the timestamp of an existing file.

4. Viewing File Contents

cat

Displays the content of a file.

  • Usage: cat filename

head

Displays the first ten lines of a file.

  • Usage: head filename

tail

Displays the last ten lines of a file.

  • Usage: tail filename

less

Allows viewing the content of a file one screen at a time.

  • Usage: less filename

5. Searching and Manipulating Text

grep

Searches for a specific string in a file.

  • Usage: grep 'search_term' filename

sed

Stream editor for filtering and transforming text.

  • Usage: sed 's/old/new/g' filename replaces "old" with "new".

awk

Pattern scanning and processing language.

  • Usage: awk '{print $1}' filename prints the first column of a file.

6. System Information and Monitoring

top

Displays real-time system processes and resource usage.

htop

An alternative to top that offers an interactive process viewer.

df

Shows disk space usage for file systems.

  • Usage: df -h for human-readable format.

du

Displays disk usage of files and directories.

  • Usage: du -h /path gives a human-readable usage summary.

free

Shows memory usage.

  • Usage: free -h for human-readable output.

7. Network Configuration

ifconfig

Displays and configures network interfaces. (Note: It’s being replaced by ip in newer distributions.)

netstat

Displays network connections, routing tables, and interface statistics.

  • Options:

    • -a: Displays listening and closed sockets.

    • -t: Shows TCP connections.

    • -u: Lists UDP connections.

ping

Tests connectivity to another host.

  • Usage: ping hostname_or_IP

8. User Management

whoami

Displays the current username.

useradd

Adds a new user.

passwd

Changes a user’s password.

9. File Compression

zip and unzip

Compresses and extracts ZIP files.

tar

Archives files without compression.

  • Usage: tar -cvf archive.tar /path to create an archive, and tar -xvf archive.tar to extract.

10. Process Management

ps

Displays the currently running processes.

  • Usage: ps aux

kill

Terminates a process using its PID.

  • Usage: kill pid

jobs

Lists all current jobs running in the background.

bg and fg

Resume a job in the background or foreground, respectively.

Conclusion

Mastering these basic Linux commands can vastly improve your ability to manage and navigate your system. Whether you’re new to Linux or looking to refine your skills, these commands form the backbone of effective Linux usage. Dive in, practice often, and you’ll become more proficient with every command you learn! Happy Linuxing!