Essential Linux Commands for File and Directory Management | HackerNoon

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

  • 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.