DEV Community

Cover image for ๐Ÿ’ป Common Useful Linux Commands ๐Ÿง
Truong Phung
Truong Phung

Posted on • Edited on

๐Ÿ’ป Common Useful Linux Commands ๐Ÿง

Common Linux Commands and Descriptions

Linux FileSystem Structure

/
โ”œโ”€โ”€ bin/              # Essential user binaries (e.g., ls, cat), accessible by all users.
โ”œโ”€โ”€ boot/             # Files needed to boot the system, like the kernel and bootloader configs.
โ”œโ”€โ”€ dev/              # Device files representing hardware (e.g., /dev/sda for hard drives).
โ”œโ”€โ”€ etc/              # System-wide configuration files (e.g., fstab, hosts).
โ”‚   โ”œโ”€โ”€ init.d/       # Service scripts (start, stop, restart)
โ”‚   โ”œโ”€โ”€ nginx/        # Configuration for the nginx web server
โ”‚   โ””โ”€โ”€ ssh/          # SSH configuration files
โ”œโ”€โ”€ home/             # User home directories for personal data.
โ”‚   โ””โ”€โ”€ user/         # Directory for user 'user',
โ”œโ”€โ”€ lib/              # Shared libraries essential for the system and kernel modules.
โ”œโ”€โ”€ media/            # Mount points for removable media, external devices like USBs or DVDs.
โ”œโ”€โ”€ mnt/              # Temporary mount directory
โ”œโ”€โ”€ opt/              # Optional software packages or third-party apps.
โ”œโ”€โ”€ proc/             # Process and kernel information
โ”œโ”€โ”€ root/             # Home directory for root user
โ”œโ”€โ”€ sbin/             # System binaries for administrative tasks (e.g., reboot, iptables).
โ”œโ”€โ”€ tmp/              # Temporary files, often cleared on reboot.
โ”œโ”€โ”€ usr/              # User programs and libraries, typically for installed packages.
โ”‚   โ”œโ”€โ”€ bin/          # User commands
โ”‚   โ”œโ”€โ”€ lib/          # User libraries
โ”‚   โ””โ”€โ”€ share/        # Shared files
โ””โ”€โ”€ var/              # Variable files (logs, databases, etc.)
    โ”œโ”€โ”€ log/          # Log files
    โ”œโ”€โ”€ cache/        # Application cache data
    โ””โ”€โ”€ tmp/          # Temporary files created by applications
Enter fullscreen mode Exit fullscreen mode

File and Directory Operations

  • ls โ€“ Lists the contents of a directory.

    ls
    ls -l        # Long listing format
    ls -a        # List all files, including hidden ones
    
  • cd โ€“ Changes the current directory.

    cd /path/to/directory
    cd ..        # Go up one directory
    cd ~         # Go to the home directory
    
  • mkdir โ€“ Creates a new directory.

    mkdir new_directory
    
  • rmdir โ€“ Removes an empty directory.

    rmdir directory_name
    
  • cp โ€“ Copies files or directories.

    cp source_file destination
    cp -r source_directory destination_directory   # Copy directories recursively
    
  • mv โ€“ Moves or renames files and directories.

    mv old_name new_name
    mv file_name /path/to/destination/
    
  • rm โ€“ Removes files or directories.

    rm file_name
    rm -r directory_name   # Remove directories recursively
    
  • touch โ€“ Creates an empty file or updates the timestamp of an existing file.

    touch file_name
    

File Viewing & Manipulation

  • cat โ€“ Displays the contents of a file.

    cat file_name
    
  • less โ€“ Allows you to view file contents page by page.

    less file_name
    
  • head โ€“ Shows the first 10 lines of a file (default).

    head file_name
    head -n 5 file_name    # Show the first 5 lines
    
  • tail โ€“ Shows the last 10 lines of a file (default).

    tail file_name
    tail -n 5 file_name    # Show the last 5 lines
    
  • grep โ€“ Searches for patterns within files.

    grep 'search_term' file_name
    grep -r 'search_term' /path/to/directory    # Search recursively in directories
    

Permissions & Ownership

  • chmod โ€“ Changes file permissions.

    chmod 755 file_name    # Gives read, write, execute permissions to the owner and read, execute to others
    chmod +x script.sh     # Make file executable
    
  • chown โ€“ Changes the file owner and group.

    chown user:group file_name
    
  • umask โ€“ Sets default file creation permissions.

    umask 022   # Sets default permissions to 755 for directories and 644 for files
    

Process Management

  • ps โ€“ Displays the currently running processes.

    ps
    ps aux      # Show all processes
    
  • top โ€“ Displays real-time system processes and resource usage.

    top
    
  • kill โ€“ Terminates a process by its PID.

    kill process_id
    kill -9 process_id    # Forcefully kill a process
    
  • htop โ€“ Interactive process viewer (requires installation).

    htop
    

System Information

  • df โ€“ Shows disk space usage.

    df -h      # Human-readable format
    
  • du โ€“ Shows disk usage for files and directories.

    du -h /path/to/directory
    
  • free โ€“ Displays memory usage.

    free -h    # Human-readable format
    
  • uname โ€“ Shows system information.

    uname -a   # Display all system info
    
  • uptime โ€“ Shows how long the system has been running.

    uptime
    
  • whoami โ€“ Displays the current logged-in user.

    whoami
    
  • hostname โ€“ Displays or sets the system's hostname.

    hostname
    
  • lscpu โ€“ Displays CPU architecture information.

    lscpu
    

Network Commands

  • ping โ€“ Tests connectivity to a host.

    ping google.com
    
  • ifconfig โ€“ Displays network interface information (may require net-tools installation on some systems).

    ifconfig
    
  • ip โ€“ Configures network interfaces and routing.

    ip addr show      # Show IP addresses of network interfaces
    ip route show     # Show routing table
    
  • curl โ€“ Fetches data from a URL.

    curl https://example.com
    
  • wget โ€“ Downloads files from the web.

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

Package Management

  • apt-get (for Debian/Ubuntu-based distributions) โ€“ Installs, updates, or removes software packages.

    sudo apt-get update           # Update package list
    sudo apt-get install package  # Install a package
    sudo apt-get remove package   # Remove a package
    
  • yum (for RedHat/CentOS-based distributions) โ€“ Installs, updates, or removes software packages.

    sudo yum update               # Update package list
    sudo yum install package      # Install a package
    sudo yum remove package       # Remove a package
    

File Compression

  • tar โ€“ Archives or extracts files.

    tar -czvf archive_name.tar.gz /path/to/directory   # Create a compressed archive
    tar -xzvf archive_name.tar.gz                      # Extract a compressed archive
    
  • zip โ€“ Compresses files into a zip archive.

    zip archive_name.zip file1 file2
    
  • unzip โ€“ Extracts a zip archive.

    unzip archive_name.zip
    

Miscellaneous

  • echo โ€“ Prints a message or variables to the terminal.

    echo "Hello, World!"
    
  • date โ€“ Displays or sets the system date and time.

    date
    
  • alias โ€“ Creates an alias for a command.

    alias ll='ls -la'    # Create a shortcut for 'ls -la'
    
  • history โ€“ Shows the command history.

    history
    
  • clear โ€“ Clears the terminal screen.

    clear
    

These are just a few of the many powerful commands in Linux, but they cover most of the common operations you'll perform daily.

If you found this helpful, let me know by leaving a ๐Ÿ‘ or a comment!, or if you think this post could help someone, feel free to share it! Thank you very much! ๐Ÿ˜ƒ

Top comments (0)