learning

Phase 15: Linux

1031 words6 min read
Phase 15: Linux
Authors

Welcome to the comprehensive guide on Linux and the Command Line Interface (CLI).

Imagine you are learning to fly a spaceship. At first, you might want a nice, graphical dashboard with a steering wheel (a Graphical User Interface, or GUI, like Windows or macOS). But true astronauts learn how to use the raw control panels—the text-based commands that allow for infinite precision and automation.

For software engineers, the vast majority of web servers, cloud infrastructure (AWS/GCP), and Docker containers run Linux. Furthermore, these servers are "headless"—meaning they do not have a monitor or graphical interface. If you want to deploy a backend, check production logs, or debug a networking issue, you must be comfortable navigating a Linux system using only your keyboard.

In this textbook-level tutorial, we will break down everything a beginner needs to confidently wield the Linux CLI.


Part 1: Navigation and The Filesystem

In Windows, your files live in C:\. In Linux, the filesystem is a giant tree that starts at the root, denoted simply by a forward slash: /.

Everything branches out from /. For example:

  • /home/username/ - Where your personal files live.
  • /var/log/ - Where system logs are typically kept.
  • /etc/ - Where configuration files are stored.

Essential Navigation Commands

  • pwd (Print Working Directory): Tells you exactly where you are in the tree.
  • ls (List): Shows you the files in your current directory.
    • Pro-tip: Use ls -la to show hidden files (which start with a dot, like .env) and see detailed file permissions.
  • cd (Change Directory): Moves you around the filesystem.
    • cd /var/log (Moves to an absolute path)
    • cd .. (Moves up one directory level)
    • cd ~ (Moves instantly to your home directory)

File Manipulation

  • mkdir super_app (Makes a new directory called super_app)
  • touch index.js (Creates an empty file named index.js)
  • cp config.json config.backup.json (Copies a file)
  • mv old_name.txt new_name.txt (Moves or renames a file)
  • rm useless_file.txt (Removes a file permanently. Warning: There is no Recycle Bin in Linux CLI!)

Part 2: Viewing and Manipulating Text

As an engineer, you will spend an enormous amount of time reading logs and config files. Opening a 2GB log file in a normal text editor will crash your computer. Linux tools are designed to handle massive text streams efficiently.

Reading Files

  • cat package.json: Prints the entire contents of a file to the screen. Good for small files.
  • less system.log: Opens a file in an interactive, scrollable viewer. You can use the arrow keys to scroll up and down, and press q to quit.
  • tail -f error.log: The most important command for debugging. tail shows the last 10 lines of a file. The -f (follow) flag keeps the stream open. As new errors are appended to the file in real-time, they will instantly print to your screen.

Searching with Grep

grep is a powerful tool to search for specific text patterns within files.

The Analogy: Like hitting Ctrl+F in a document, but on steroids.

# Search for the word "ERROR" inside the server log file
grep "ERROR" /var/log/nginx/error.log

# Search for "database timeout" across ALL log files in a directory, case-insensitive (-i)
grep -i "database timeout" /var/log/myapp/*.log

The Magic of Piping |

In Linux, you can chain commands together using the pipe character |. It takes the output of the first command and feeds it as the input to the second command.

# 1. Print a massive log file
# 2. Pipe it to grep to find only lines with "Timeout"
# 3. Pipe that result to less so you can scroll through it comfortably
cat server.log | grep "Timeout" | less

Part 3: Permissions (chmod and chown)

Linux is a multi-user system. Security is built into every file. If you run a web server, you want the web server to be able to read files, but absolutely not be able to execute malicious scripts.

Every file has three levels of permissions for three types of users:

  1. Owner (The user who created it)
  2. Group (A group of users)
  3. Others (Everyone else)

The permissions are: Read (4), Write (2), and eXecute (1).

# Changing permissions using chmod (Change Mode)
# 7 = Read (4) + Write (2) + Execute (1)
# 5 = Read (4) + Execute (1)
# 0 = No permissions

# Give the owner full rights (7), but no rights to anyone else (00)
chmod 700 secret_keys.txt

# Make a script executable so you can run it
chmod +x deploy.sh

Part 4: Networking and Troubleshooting

When your backend cannot connect to the database, you need to prove whether it's a code issue or a network issue.

  • ping: Tests basic connectivity. "Are you there?"
    ping google.com
    
  • curl: Makes HTTP requests directly from the terminal. Incredible for testing APIs without Postman.
    # Send a GET request and view the JSON response
    curl https://api.github.com/users/wanilhami
    
    # Send a POST request with JSON data
    curl -X POST https://api.mycoolapp.com/login \
         -H "Content-Type: application/json" \
         -d '{"username":"admin", "password":"123"}'
    
  • netstat / ss: Shows what ports are open and listening on your machine.
    # "Is my Node.js server actually listening on port 3000?"
    ss -tulpn | grep 3000
    

Part 5: Secure Shell (SSH)

You have a server sitting in an AWS data center in Virginia. How do you control it from your laptop in your bedroom? SSH.

SSH gives you a secure, encrypted terminal session on a remote machine.

The Analogy: It's like remote-controlling a robot on Mars. Every keystroke you type on your laptop is securely beamed to the server, and the server's screen output is beamed back to you.

Instead of passwords (which can be brute-forced), professionals use Cryptographic Key Pairs (A Public Key and a Private Key).

# 1. Generate a key pair on your laptop
ssh-keygen -t ed25519 -C "your_email@example.com"

# 2. Connect to the remote server using the key
ssh ubuntu@198.51.100.22

Once connected, your terminal prompt changes, and every command you type is now executing on the cloud server!

Tags

#linux#cli#networking