Common Linux Commands

Mastering the command line is a key skill for anyone working with Linux, whether you're managing servers, developing software, or orchestrating containers. Linux commands offer powerful ways to interact with the system, automate tasks, and manage resources efficiently. This guide covers some of the most commonly used Linux commands that form the foundation of daily tasks in a Linux environment.

Basic Navigation and File Management Commands

ls - List Directory Contents

The ls command lists the files and directories in the current directory. It's often the first command you'll use when navigating the filesystem. Learn more about the Linux File System.

ls
  • Options:
    • -l: Displays a detailed list, including file permissions, ownership, size, and modification date.
    • -a: Shows all files, including hidden ones (those starting with a dot .).
    • -h: Makes file sizes human-readable (e.g., KB, MB).

cd - Change Directory

The cd command changes the current working directory, essential for navigating the Linux file system.

cd /path/to/directory
  • Shortcuts:
    • cd ~: Goes to your home directory.
    • cd ..: Moves up one directory level.
    • cd -: Switches to the previous directory.

pwd - Print Working Directory

The pwd command shows the full path of the current working directory, helping you confirm your location in the directory structure.

pwd

cp - Copy Files and Directories

The cp command copies files or directories from one location to another.

cp source_file destination_file
  • Options:
    • -r: Recursively copies directories and their contents.
    • -i: Prompts before overwriting existing files.

mv - Move or Rename Files and Directories

The mv command moves or renames files and directories.

mv old_name new_name
  • Use cases:
    • Renaming a file: mv old_name new_name
    • Moving a file to a different directory: mv file_name /path/to/directory

rm - Remove Files or Directories

The rm command deletes files or directories.

rm file_name
  • Options:
    • -r: Recursively removes directories and their contents.
    • -i: Prompts before each removal, adding a layer of safety.

Caution: rm is irreversible, so use it carefully, especially with the -r option.

mkdir - Create Directories

The mkdir command creates new directories.

mkdir directory_name
  • Options:
    • -p: Creates parent directories as needed (e.g., mkdir -p /parent/child/grandchild).

rmdir - Remove Empty Directories

The rmdir command removes empty directories.

rmdir directory_name

For removing directories with contents, use rm -r.

File Viewing and Editing Commands

cat - Concatenate and Display File Contents

The cat command displays the contents of a file.

cat file_name
  • Options:
    • -n: Numbers all output lines.

less and more - View File Contents Page by Page

less and more allow you to view large files one page at a time.

less file_name
  • Navigation:
    • Space: Move to the next page.
    • q: Quit the viewer.

head and tail - View the Beginning or End of a File

head shows the first few lines of a file, while tail shows the last few lines.

head file_name
tail file_name
  • Options:
    • -n X: Displays the first/last X lines (e.g., head -n 10 file_name).

nano and vim - Text Editors

nano and vim are command-line text editors. nano is more beginner-friendly, while vim offers powerful features for advanced users.

nano file_name
vim file_name

Learn more about Shell Scripting Best Practices to enhance your editing and scripting skills in Linux.

System Monitoring and Management Commands

ps - Display Running Processes

The ps command displays currently running processes.

ps
  • Options:
    • aux: Shows detailed information for all processes.

top - Real-Time System Monitoring

top provides a real-time view of system processes, including CPU and memory usage.

top
  • Navigation:
    • q: Quit top.

htop - Interactive Process Viewer

htop is an enhanced version of top with a more user-friendly interface (requires installation on some systems).

htop

df - Disk Space Usage

The df command displays disk space usage for the file system.

df -h
  • Options:
    • -h: Displays sizes in human-readable format (e.g., GB, MB).

du - Directory Space Usage

The du command estimates file and directory space usage.

du -h directory_name
  • Options:
    • -h: Displays sizes in human-readable format.
    • -s: Provides a summary of the directory's total size.

free - Display Memory Usage

The free command shows the amount of free and used memory in the system.

free -h
  • Options:
    • -h: Displays sizes in human-readable format.

File Permissions and Ownership Commands

chmod - Change File Permissions

The chmod command changes the permissions of a file or directory.

chmod 755 file_name
  • Common Permission Settings:
    • 755: Read and execute access for everyone, write access for the owner.
    • 644: Read access for everyone, write access for the owner.

Learn more about Linux File Permissions to better understand how chmod works.

chown - Change File Ownership

The chown command changes the owner and group of a file or directory.

chown user:group file_name

Networking Commands

ping - Test Network Connectivity

The ping command tests the network connection to a host.

ping example.com
  • Options:
    • -c X: Sends X number of packets (e.g., ping -c 4 example.com).

ifconfig - Configure Network Interfaces

The ifconfig command configures or displays network interfaces.

ifconfig
  • Alternative:
    • ip a: Modern command to display network interfaces.

Learn more about Linux Networking Commands for advanced networking tasks.

curl - Transfer Data from or to a Server

The curl command transfers data to or from a server, using various protocols like HTTP, FTP, and more.

curl http://example.com
  • Options:
    • -O: Saves the output to a file (e.g., curl -O http://example.com/file.txt).

wget - Download Files from the Web

wget is a command-line utility to download files from the web.

wget http://example.com/file.zip