Basic Linux Commands for Beginners: A Complete Guide

 

Introduction

Linux is a powerful and widely used operating system, especially in DevOps, cloud computing, and system administration. As a DevOps and Cloud Engineer, understanding Linux commands is essential for managing servers, automating tasks, and troubleshooting issues.

In this guide, we’ll cover the most important Linux commands that every beginner should know. These commands will help you navigate the Linux filesystem, manage processes, control permissions, and more.

1. Navigating Files and Directories

Listing Files and Directories (ls)

The ls command is used to list files and directories in the current location. By default, it only shows names, but adding options can provide more details.

  • ls → Lists all files and directories.
  • ls -l → Displays detailed information (permissions, owner, size, modified date).
  • ls -a → Shows hidden files (files starting with .).

Changing Directories (cd)

To move between directories, use the cd command.

  • cd /home/user → Moves to the /home/user directory.
  • cd .. → Moves one level up in the directory structure.
  • cd / → Moves to the root directory.

Finding Your Current Directory (pwd)

If you ever get lost in the directory structure, use the pwd command to print the full path of your current location.

2. Managing Files and Directories

Creating Files and Directories

  • touch file.txt → Creates an empty file named file.txt.
  • mkdir my_folder → Creates a new directory called my_folder.

Copying and Moving Files

  • cp file1.txt file2.txt → Copies file1.txt to file2.txt.
  • mv oldname.txt newname.txt → Renames a file.
  • mv file.txt /home/user/ → Moves file.txt to another directory.

Deleting Files and Directories

  • rm file.txt → Deletes a file.
  • rm -r my_folder → Deletes a directory and all its contents.

3. Viewing File Content

Displaying a File (cat)

To view the content of a file, use the cat command.

  • cat file.txt → Displays the contents of file.txt.

Viewing Large Files (less and more)

For large files, less and more allow scrolling through the content.

  • less file.txt → Opens the file with scrolling support.
  • more file.txt → Displays the file page by page.

Checking the First or Last Lines of a File

  • head file.txt → Shows the first 10 lines.
  • tail file.txt → Shows the last 10 lines.
  • tail -f logfile.log → Continuously monitors a log file in real-time.

4. Managing Users and Permissions

Checking the Current User (whoami)

If you're unsure which user account you're logged in as, run:

  • whoami → Displays the currently logged-in user.

Adding and Removing Users

  • sudo adduser newuser → Creates a new user named newuser.
  • sudo deluser username → Deletes a user.

Changing File Permissions (chmod)

File permissions control who can read, write, or execute a file.

  • chmod 755 file.txt → Grants read/write/execute to the owner and read/execute to others.

Changing File Ownership (chown)

To change the owner of a file:

  • chown user:group file.txt → Assigns ownership to user and group.

5. Monitoring Processes and System Performance

Checking Running Processes (ps)

The ps command lists active processes on your system.

  • ps aux → Displays all running processes along with CPU and memory usage.

Real-Time Process Monitoring (top and htop)

For real-time monitoring:

  • top → Displays active processes, CPU usage, and memory usage.
  • htop → A user-friendly alternative (needs installation).

Killing Processes (kill and pkill)

  • kill 1234 → Terminates a process with the given PID (1234).
  • pkill firefox → Kills all Firefox processes.

6. Disk and Memory Management

Checking Disk Usage (df)

To see how much space is available on your system:

  • df -h → Shows disk usage in a human-readable format.

Checking Directory Size (du)

To see the size of a specific directory:

  • du -sh /home/user → Displays the total size of the /home/user directory.

Checking Memory Usage (free)

To check system memory:

  • free -h → Displays free and used memory.

7. Networking Commands

Checking Network Connectivity (ping)

To test if a server is reachable:

  • ping google.com → Sends packets to Google’s server to check connectivity.

Viewing Network Configuration (ip and ifconfig)

  • ip a → Shows all network interfaces and IP addresses.
  • ifconfig → Alternative (deprecated on some systems).

Checking Open Ports (netstat)

To see active network connections:

  • netstat -tulnp → Displays all open ports.

8. Installing and Managing Software

On Debian-based systems (Ubuntu, Debian):

  • sudo apt update → Updates the package list.
  • sudo apt upgrade → Upgrades all installed packages.
  • sudo apt install nginx → Installs the Nginx web server.
  • sudo apt remove nginx → Uninstalls Nginx.

On Red Hat-based systems (CentOS, Fedora):

  • sudo yum update → Updates the package list.
  • sudo yum install nginx → Installs Nginx.

9. Compressing and Archiving Files

Creating and Extracting Tar Archives (tar)

  • tar -cvf archive.tar folder/ → Creates a tar archive of a folder.
  • tar -xvf archive.tar → Extracts the archive.

Compressing Files (gzip and gunzip)

  • gzip file.txt → Compresses file.txt.
  • gunzip file.txt.gz → Decompresses file.txt.gz.

10. Automating Tasks with Cron Jobs

Editing Cron Jobs (crontab)

Cron jobs allow you to schedule tasks. To edit your crontab file:

  • crontab -e → Opens the cron job editor.

To schedule a script to run every 10 minutes:

*/10 * * * * /path/to/script.sh

To list scheduled cron jobs:

  • crontab -l → Displays existing cron jobs.

Conclusion

Mastering these basic Linux commands will help you navigate, manage, and automate Linux systems efficiently. Whether you're working in DevOps, cloud automation, or system administration, these commands are essential for your daily tasks.

Want to learn more about advanced Linux scripting and automation? Stay tuned for my next blog! 🚀


💬 Call-to-Action (CTA):

💡 Did I miss any important Linux commands? Let me know in the comments!
📩 Subscribe for more DevOps & Cloud tutorials!

Comments

Popular posts from this blog

🔍 Why You Only See a Private IP Inside an AWS EC2 Instance — Even If It Has a Public IP

A Day in the Life of a DevOps Engineer – Roles & Responsibilities

Docker and Its Usage in DevOps – A Complete Guide