Edit Template

Linux Command Line Cheat Sheet: Essential Commands for Beginners

Introduction

Whether you're just starting your tech journey or aiming to level up your DevOps skills, mastering the Linux command line is non-negotiable. Linux powers most of the world's servers, cloud infrastructure, and DevOps tooling – making command line proficiency a must-have skill in today's tech landscape.

This comprehensive cheat sheet compiles the most essential Linux commands that beginners need to know. Bookmark this page, keep it handy, and watch your confidence with the terminal grow with each command you master.

Why Learn the Command Line?

Before diving into commands, let's understand why the command line matters:

  • Efficiency: Actions that would take multiple clicks in a GUI can be accomplished with a single command
  • Automation: Scripts can string together commands to automate repetitive tasks
  • Remote System Management: Manage servers without needing a graphical interface
  • DevOps Workflows: CI/CD pipelines, infrastructure as code, and containerization all rely heavily on command line operations

Now, let's get to the commands!

Navigation and Directory Management

The first step to becoming comfortable with Linux is learning how to navigate the file system.

1. Finding Your Location

pwd

The pwd (print working directory) command shows your current location in the file system. This is your "You Are Here" marker in the Linux file system.

2. Listing Directory Contents

ls

The ls command lists files and directories in your current location. It has many useful options:

  • ls -l: Long format showing permissions, owner, size, and modification date
  • ls -a: Shows hidden files (those starting with a dot)
  • ls -h: Shows file sizes in human-readable format (KB, MB, GB)
  • ls -lah: Combines all three options above

Try this: ls -lah /etc to see system configuration files with detailed information.

3. Changing Directories

cd /path/to/directory

The cd (change directory) command lets you move around the file system. Some common variations:

  • cd ~: Go to your home directory
  • cd ..: Move up one directory level
  • cd -: Return to the previous directory
  • cd /: Go to the root directory

image_1

File Management

Once you can navigate the file system, you'll need to work with files and directories.

1. Creating Directories

mkdir my_new_directory

The mkdir (make directory) command creates new folders. For nested directories, use:

mkdir -p parent/child/grandchild

The -p flag creates parent directories if they don't exist.

2. Removing Directories

rmdir empty_directory

For empty directories, use rmdir. For directories with content, use:

rm -r directory_with_files

Warning: Be extremely careful with rm -rf as it deletes everything without confirmation.

3. Copying Files and Directories

cp source.txt destination.txt

For directories, add the recursive flag:

cp -r source_directory/ destination_directory/

4. Moving/Renaming Files and Directories

mv old_name.txt new_name.txt

The mv command both moves files between locations and renames them:

mv file.txt /path/to/new/location/

5. Creating Empty Files

touch new_file.txt

The touch command creates an empty file or updates the timestamp of an existing file.

File Creation and Editing

Linux offers several ways to view and edit file contents directly from the terminal.

1. Viewing File Contents

cat file.txt

The cat command displays the entire file content. For larger files, use:

less file.txt

With less, use:

  • Space bar: Page down
  • b: Page up
  • q: Quit
  • /text: Search for "text"

For just the beginning or end of a file:

head -n 10 file.txt    # Shows first 10 lines
tail -n 10 file.txt    # Shows last 10 lines

2. Text Editors

For quick edits, nano is beginner-friendly:

nano file.txt

For more advanced editing, try vim:

vim file.txt

Vim has a steeper learning curve but is extremely powerful once mastered.

image_2

File Permissions and Ownership

Understanding permissions is crucial for security and proper system functioning.

1. Viewing Permissions

ls -l file.txt

You'll see something like: -rw-r--r-- 1 user group 1234 Jan 1 12:34 file.txt

The permissions part (-rw-r--r--) breaks down as:

  • First character: File type (- for regular file, d for directory)
  • Next three: Owner permissions (r=read, w=write, x=execute)
  • Next three: Group permissions
  • Last three: Everyone else's permissions

2. Changing Permissions

chmod 755 script.sh

The numeric method is fast:

  • 4 = read
  • 2 = write
  • 1 = execute

Add them together for each position (owner, group, others). So 755 means:

  • Owner: 7 (4+2+1) = read, write, execute
  • Group: 5 (4+1) = read, execute
  • Others: 5 (4+1) = read, execute

3. Changing Ownership

chown user:group file.txt

This changes both the user and group ownership of a file.

System Information Commands

Need to know what's happening on your system? These commands help:

1. System and Kernel Information

uname -a

2. Disk Space Usage

df -h    # Shows disk space by partition
du -sh directory/    # Shows directory size

3. Memory Usage

free -h

4. System Uptime

uptime

Process Management

Monitoring and controlling processes is essential for system management.

1. Viewing Running Processes

ps aux

For a dynamic, real-time view:

top

Or the more user-friendly alternative:

htop    # May need installation: apt install htop

2. Killing Processes

kill PID    # Gracefully terminate process
kill -9 PID    # Force terminate process

Replace PID with the process ID you want to terminate.

image_3

Package Management

Different Linux distributions use different package managers. Here are the most common:

For Debian/Ubuntu:

apt update                   # Update package list
apt upgrade                  # Upgrade installed packages
apt install package_name     # Install a package
apt remove package_name      # Remove a package

For Red Hat/CentOS:

yum update                   # Update package list and upgrade
yum install package_name     # Install a package
yum remove package_name      # Remove a package

For Fedora:

dnf update                   # Update package list and upgrade
dnf install package_name     # Install a package
dnf remove package_name      # Remove a package

Networking Commands

These commands help you understand and troubleshoot network connections.

1. Checking Connectivity

ping google.com

2. Viewing Network Configuration

ifconfig    # May need installation: apt install net-tools

Or the newer alternative:

ip addr

3. DNS Lookup

nslookup domain.com
dig domain.com

4. Checking Open Ports

netstat -tuln

Or:

ss -tuln

File Search and Text Processing

1. Finding Files

find /path/to/search -name "filename"

Example: find /home -name "*.txt" finds all text files in /home.

2. Searching File Contents

grep "search term" file.txt

To search recursively through directories:

grep -r "search term" /path/to/search

3. Text Processing with Pipes

Linux commands can be chained together with pipes:

cat file.txt | grep "important" | sort | uniq > results.txt

This pipeline:

  1. Displays file.txt
  2. Finds lines containing "important"
  3. Sorts those lines
  4. Removes duplicates
  5. Saves the result to results.txt

Tips for Linux Command Line Beginners

  1. Use Tab Completion: Press Tab to autocomplete commands and file paths
  2. Access Command History: Press Up Arrow to cycle through previously used commands
  3. Read the Manual: Use man command_name to access detailed documentation
  4. Create Aliases: Add shortcuts for common commands in your .bashrc file
  5. Use Command Line Help: Most commands support --help flag for quick reference

Conclusion

This cheat sheet covers the essential Linux commands that every beginner should know. The command line might seem intimidating at first, but with practice, it becomes an incredibly powerful tool in your DevOps toolkit.

Remember that mastering Linux is a journey, not a destination. Start with these fundamentals, practice regularly, and gradually expand your command repertoire as you tackle more complex tasks.

For more DevOps learning resources, check out our other guides on Docker, Git, and CI/CD pipelines.

Have questions about Linux commands or DevOps practices? Drop them in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Recent Posts

Category

content created for you!

Company

About Us

FAQs

Contact Us

Terms & Conditions

Features

Copyright Notice

Mailing List

Social Media Links

Help Center

Products

Sitemap

New Releases

Best Sellers

Newsletter

Help

Copyright

Mailing List

© 2023 DevOps Horizon