Linux Command-Line Notes

Basic File and Directory Commands:

  1. ls: List directory contents
    • ls – lists files in the current directory
    • ls -l – lists files in long format (permissions, size, owner, etc.)
    • ls -a – lists all files, including hidden ones (starting with a dot .)
  2. cd: Change directory
    • cd [directory] – change to specified directory
    • cd .. – move up one level (parent directory)
    • cd / – move to the root directory
    • cd ~ – move to the home directory
  3. pwd: Print working directory
    • Displays the current directory path.
  4. mkdir: Make directories
    • mkdir [directory_name] – creates a new directory
  5. rmdir: Remove directories
    • rmdir [directory_name] – deletes an empty directory
  6. rm: Remove files or directories
    • rm [file] – removes file
    • rm -r [directory] – removes directory recursively
    • rm -rf [directory] – forcefully removes a directory without prompting
  7. cp: Copy files or directories
    • cp [source][destination] – copies file from source to destination
    • cp -r [source_directory][destination] – copies directory recursively
  8. mv: Move or rename files or directories
    • mv [source][destination] – moves or renames a file or directory
  9. touch: Create an empty file or update file timestamps
    • touch [filename] – creates a new file if it doesn’t exist

File Viewing and Manipulation Commands:

  1. cat: Concatenate and display files
    • cat [file] – displays the content of a file
    • cat [file1][file2] – displays the content of both files in order
  2. less: View file content page by page
    • less [file] – allows scrolling through a large file
  3. head: Display the first lines of a file
    • head -n [number][file] – displays the first n lines of the file
  4. tail: Display the last lines of a file
    • tail -n [number][file] – displays the last n lines of the file
    • tail -f [file] – continuously monitors and displays new lines added to a file (useful for log monitoring)
  5. grep: Search for patterns within files
    • grep [pattern][file] – searches for a pattern in a file
    • grep -r [pattern][directory] – searches recursively in a directory

File Permissions and Ownership Commands:

  1. chmod: Change file permissions
    • chmod [permissions][file] – modifies the permission of a file
    • chmod u+x [file] – gives the user execution rights for the file
  2. chown: Change file ownership
    • chown [owner][file] – changes the ownership of the file
  3. chgrp: Change group ownership of a file
    • chgrp [group][file] – changes the group ownership of the file

Process and System Management Commands:

  1. ps: View currently running processes
    • ps – displays processes running in the current session
    • ps -aux – shows detailed information about all running processes
  2. top: Real-time view of system processes
    • Displays real-time information about running processes, CPU, and memory usage
  3. kill: Terminate processes
    • kill [PID] – sends the terminate signal to a process with a specific Process ID (PID)
    • kill -9 [PID] – forcefully kills a process
  4. htop: Interactive process viewer (requires installation)
    • htop – a more interactive and user-friendly version of top
  5. df: Show disk space usage
    • df -h – displays disk usage in human-readable format
  6. du: Show directory space usage
    • du -sh [directory] – shows the size of the specified directory
  7. free: Check memory usage
    • free -h – displays memory and swap usage in human-readable format
  8. uname: Display system information
    • uname -a – displays detailed information about the system (kernel version, architecture, etc.)

Networking Commands:

  1. ping: Test network connectivity
    • ping [hostname/IP] – sends ICMP packets to a host to test connectivity
  2. ifconfig / ip: Network interface configuration
    • ifconfig – displays network interface information (Linux pre-2019, now deprecated in favor of ip)
    • ip addr – displays IP address and network details
  3. netstat: Display network connections and listening ports
    • netstat -tuln – displays TCP/UDP listening ports
  4. curl: Transfer data to or from a server
    • curl [URL] – makes HTTP requests to a URL and retrieves the content
  5. wget: Download files from the web
    • wget [URL] – downloads a file from the specified URL

Archive and Compression Commands:

  1. tar: Archive and extract files
    • tar -cvf [archive.tar][file/directory] – creates an archive
    • tar -xvf [archive.tar] – extracts the contents of an archive
  2. gzip / gunzip: Compress and decompress files
    • gzip [file] – compresses the file
    • gunzip [file.gz] – decompresses the file
  3. zip / unzip: Compress and decompress files in zip format
    • zip [archive.zip][file] – compresses file into a zip archive
    • unzip [archive.zip] – extracts a zip archive

User and Group Management:

  1. whoami: Display the current user
    • Shows the currently logged-in user.
  2. adduser / useradd: Add a new user
    • adduser [username] – adds a new user (simpler, with prompts)
    • useradd [username] – adds a new user (requires manual configuration)
  3. passwd: Change user password
    • passwd [username] – sets or changes the password for the user
  4. groups: Display group membership
    • groups [username] – shows the groups a user belongs to

Text Manipulation Commands:

  1. echo: Print text to the screen
    • echo [text] – prints the provided text
  2. sort: Sort lines in a file
    • sort [file] – sorts the lines in the file
  3. uniq: Remove duplicate lines from a file
    • uniq [file] – removes adjacent duplicate lines
  4. cut: Extract sections from a file
    • cut -d [delimiter] -f [fields][file] – extracts specific fields based on delimiter
  5. wc: Word count
    • wc -l [file] – counts the number of lines in a file
  6. awk: Pattern scanning and processing language
    • Used to manipulate data and generate reports.
  7. sed: Stream editor for modifying files
    • sed 's/old/new/g' [file] – replaces all occurrences of old with new in a file

Package Management Commands:

  1. apt / apt-get (Debian-based systems):
    • apt-get update – update package lists
    • apt-get install [package] – install a package
  2. yum / dnf (Red Hat-based systems):
    • yum install [package] – install a package