59 most used Linux commands - with usage examples

59 most used Linux commands -  with usage examples
Photo by Gabriel Heinzer / Unsplash

This is a compilation of some of the most used commands in Linux. I find that if you already know 50% of these, you're doing quite well.

10 Concepts Every Linux User Should Know About
Linux is a powerful and versatile operating system that can run on a variety of devices, from desktops and laptops to servers and smartphones. Linux is also free and open source, which means anyone can use, modify, and distribute it. However, Linux can also be intimidating for new users, especially

The list

  1. ls: List directory contents.
    • Scenario 1: View files in a directory: ls
    • Scenario 2: Display detailed information: ls -l
    • Scenario 3: Display detailed information, including hidden files, in human readable format: ls -lah
  2. cd: Change directory.
    • Scenario 1: Move to the home directory: cd ~
    • Scenario 2: Navigate to a specific directory: cd /path/to/directory
  3. pwd: Print working directory.
    • Scenario 1: View the current directory: pwd
    • Scenario 2: Use the absolute path in a script: echo $(pwd)
  4. cp: Copy files and directories.
    • Scenario 1: Duplicate a file: cp file1 file2
    • Scenario 2: Copy a directory and its contents recursively: cp -r dir1 dir2
  5. mv: Move or rename files and directories.
    • Scenario 1: Rename a file: mv oldname.txt newname.txt
    • Scenario 2: Move a file to another directory: mv file1 /path/to/directory
  6. rm: Remove files and directories.
    • Scenario 1: Delete a file: rm file
    • Scenario 2: Remove a directory and its contents recursively: rm -r directory
  7. mkdir: Create directories.
    • Scenario 1: Make a new directory: mkdir new_directory
    • Scenario 2: Create nested directories: mkdir -p parent_directory/new_directory
  8. touch: Create an empty file.
    • Scenario 1: Create a new file: touch new_file.txt
    • Scenario 2: Update the modification time of an existing file: touch existing_file.txt
  9. cat: Concatenate and display file content.
    • Scenario 1: View file content: cat file.txt
    • Scenario 2: Display line numbers in the output: cat -n file.txt
  10. grep: Search for patterns in files.
    • Scenario 1: Find occurrences of a word in a file: grep "word" file.txt
    • Scenario 2: Search recursively in directories for a specific pattern: grep -r "pattern" /path/to/directory
    • Scenario 3: Find text within files: grep "pattern" file
    • Scenario 4: Count occurrences of a pattern in multiple files: grep -c "pattern" file1 file2
    • Scenario 5: Search for a pattern in a file recursively: grep -r "pattern" /path/to/directory
    • Scenario 6: Display lines that do not match a pattern: grep -v "pattern" file.txt
  11. chmod: Change file permissions.
    • Scenario 1: Grant execute permission to a file: chmod +x script.sh
    • Scenario 2: Restrict permissions for a file: chmod go-rwx file
  12. chown: Change file owner and group.
    • Scenario 1: Change the owner of a file: chown user:group file
    • Scenario 2: Change the owner of a directory and its contents recursively: chown -R user:group directory
  13. ps: Display currently running processes.
    • Scenario 1: List all processes: ps aux
    • Scenario 2: Show processes of a specific user: ps -u username
  14. kill: Terminate processes by ID or name.
    • Scenario 1: Stop a process using its ID: kill PID
    • Scenario 2: Forcefully terminate a process: kill -9 PID
  15. tar: Archive files.
    • Scenario 1: Create a compressed archive: tar -czvf archive.tar.gz directory
    • Scenario 2: Extract an archive to a specific directory: tar -xzvf archive.tar.gz -C /path/to/destination
  16. gunzip: Decompress files.
    • Scenario 1: Decompress a file: gunzip file.gz
    • Scenario 2: Decompress multiple files with a wildcard: gunzip *.gz
    • Scenario 3: Decompress multiple files: gunzip file1.gz file2.gz
  17. wget: Retrieve files from the internet.
    • Scenario 1: Download a file: wget URL
    • Scenario 2: Continue an interrupted download: wget -c URL
  18. scp: Securely copy files over SSH.
    • Scenario 1: Copy a file from a remote server to local machine: scp user@remote:/path/to/file /local/path
    • Scenario 2: Copy a directory from local to remote: scp -r /local/directory user@remote:/remote/path
    • Scenario 3: Copy a file from local to remote: scp file.txt user@remote:/path
  19. ssh: Securely access remote servers.
    • Scenario 1: Connect to a server using a specific private key: ssh -i /path/to/private_key user@host
    • Scenario 2: Establish an SSH tunnel for port forwarding: ssh -L 8080:localhost:80 user@host
  20. ifconfig/ip: Display network interface information.
    • Scenario 1: List all network interfaces: ifconfig -a
    • Scenario 2: Set an IP address for a specific interface: sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0
  21. netstat: Display network connections, routing tables, interface statistics.
    • Scenario 1: Show all listening ports: netstat -tulnp
    • Scenario 2: Display statistics for a specific network protocol: netstat -s
  22. route: View and manipulate IP routing tables.
    • Scenario 1: Add a new route to the routing table: sudo route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1
    • Scenario 2: Delete a specific route: sudo route del -net 192.168.1.0 netmask 255.255.255.0
  23. ping: Send ICMP echo requests to check connectivity.
    • Scenario 1: Test network connectivity to a host: ping google.com
    • Scenario 2: Ping continuously and record the output to a file: ping -i 2 google.com >> ping_log.txt
  24. traceroute/tracert: Display the route packets take to a network host.
    • Scenario 1: Trace the path packets take: traceroute google.com
    • Scenario 2: Limit the number of hops: traceroute -m 20 google.com
  25. nslookup/dig: Query DNS servers for domain information.
    • Scenario 1: Retrieve DNS information: nslookup domain.com
    • Scenario 2: Perform a reverse DNS lookup: nslookup 8.8.8.8
  26. hostname: Display or set system hostname.
    • Scenario 1: View the current hostname: hostname
    • Scenario 2: Change the hostname temporarily: sudo hostname temporary_name
  27. uname: Display system information.
    • Scenario 1: Check kernel version: uname -r
    • Scenario 2: Show system architecture: uname -m
    • Scenario 1: Show only the kernel name: uname -s
  28. whoami: Display current username.
    • Scenario 1: Identify the current logged-in user: whoami
    • Scenario 2: Use in a script to confirm executing user: if [ "$(whoami)" != "root" ]; then echo "Please run as root"; fi
  29. df: Display disk space usage.
    • Scenario 1: Show available space in human-readable format: df -h
    • Scenario 2: Display space usage of a specific filesystem: df -T /dev/sda1
  30. du: Display directory space usage.
    • Scenario 1: Show sizes of directories in the current location: du -h
    • Scenario 2: Display total size of a directory: du -sh directory
  31. find: Search for files and directories.
    • Scenario 1: Find files modified within the last 24 hours: find / -mtime -1
    • Scenario 2: Search for files owned by a specific user: find / -user username
  32. awk: Pattern scanning and text processing tool.
    • Scenario 1: Print lines longer than a specific length: awk 'length > 80' file
    • Scenario 2: Extract and print specific fields from a CSV file: awk -F',' '{print $1, $3}' file.csv
    • Scenario 3: Print lines matching a specific field: awk '$3 ~ /pattern/' file.txt
    • Scenario 4: Calculate the sum of a specific column: awk '{sum+=$2} END {print sum}' file.txt
  33. sed: Stream editor for filtering and transforming text.
    • Scenario 1: Replace text in multiple files: sed -i 's/old/new/g' file1 file2
    • Scenario 2: Insert text at the beginning of a file: sed -i '1iThis is a header' file
    • Scenario 3: Remove leading and trailing whitespaces in a file: sed -i 's/^[ \t]*//;s/[ \t]*$//' file.txt
    • Scenario 4: Replace a specific string in a file: sed -i 's/old/new/g' file.txt
  34. systemctl: Control system services.
    • Scenario 1: Enable a service to start at boot: sudo systemctl enable servicename
    • Scenario 2: Check the status of a service: systemctl status ssh
  35. journalctl: Query and display system logs.
    • Scenario 1: Filter logs for a specific service: journalctl -u servicename
    • Scenario 2: Display logs since a specific time: journalctl --since "2023-12-01 00:00:00"
  36. firewall-cmd/ufw: Configure firewall rules.
    • Scenario 1: Allow all traffic from a specific IP address: sudo ufw allow from 192.168.1.100
    • Scenario 2: Deny all incoming traffic by default: sudo ufw default deny incoming
  37. lsof: List open files and processes.
    • Scenario 1: List processes using a specific file: lsof /path/to/file
    • Scenario 2: Show network-related processes: lsof -i
  38. tcpdump: Network packet analyzer.
    • Scenario 1: Capture packets on a specific interface: sudo tcpdump -i eth0
    • Scenario 2: Display HTTP traffic: sudo tcpdump -i eth0 port 80
  39. netcat (nc): Network utility for reading and writing data across network connections.
    • Scenario 1: Create a simple TCP server: nc -l -p 1234
    • Scenario 2: Transfer files between systems: nc -l -p 1234 < file.txt
  40. ssh-keygen: Create SSH key pairs for secure authentication.
    • Scenario 1: Generate an RSA key pair: ssh-keygen -t rsa
    • Scenario 2: Create a key pair with a specific filename: ssh-keygen -t rsa -f /path/to/key
  41. ssh-copy-id: Copy SSH keys to a remote server.
    • Scenario 1: Copy the public key to a remote host: ssh-copy-id user@host
    • Scenario 2: Use a specific port for copying the key: ssh-copy-id -p port user@host
  42. rsync: Remote file synchronization and transfer.
    • Scenario 1: Synchronize files and directories between local and remote: rsync -avz /source user@remote:/destination
    • Scenario 2: Delete files at the destination that are not in the source: rsync -av --delete /source user@remote:/destination
  43. tail: Display the last part of a file.
    • Scenario 1: View the last 10 lines of a file: tail file.txt
    • Scenario 2: Continuously display new lines appended to a file: tail -f log.txt
  44. head: Display the beginning of a file.
    • Scenario 1: View the first 10 lines of a file: head file.txt
    • Scenario 2: Display a specific number of lines from the start: head -n 15 file.txt
  45. more: View file content one page at a time.
    • Scenario 1: Display content page by page: more file.txt
    • Scenario 2: Search for a specific pattern in a file: more file.txt +/pattern
  46. less: View file content with backward navigation.
    • Scenario 1: Scroll through file content: less file.txt
    • Scenario 2: Search for text and navigate through matches: less +/pattern file.txt
  47. echo: Display a line of text or a variable.
    • Scenario 1: Print a message: echo "Hello, World!"
    • Scenario 2: Assign a value to a variable: variable="value"; echo $variable
    • Scenario 3: Output text to a file: echo "Content" > file.txt
    • Scenario 4: Append text to a file: echo "More content" >> file.txt
  48. date: Display or set the system date and time.
    • Scenario 1: Show the current date and time: date
    • Scenario 2: Display date and time in a specific format: date +"%Y-%m-%d %H:%M:%S"
  49. curl: Transfer data from or to a server.
    • Scenario 1: Retrieve content from a URL: curl URL
    • Scenario 2: Send data via POST request: curl -X POST -d "data" URL
  50. history: Display command history.
    • Scenario 1: Show the list of recent commands: history
    • Scenario 2: Search for commands containing specific text: history | grep "pattern"
  51. which: Locate a command.
    • Scenario 1: Find the path of an executable: which command
    • Scenario 2: Check if a command is available in the system: which ls
  52. wc: Display line, word, and character count for a file.
    • Scenario 1: Count lines in a file: wc -l file.txt
    • Scenario 2: Count words in a file: wc -w file.txt
  53. uniq: Report or omit repeated lines in a file.
    • Scenario 1: Show unique lines in a sorted file: sort file.txt | uniq
    • Scenario 2: Display count of occurrences for unique lines: sort file.txt | uniq -c
  54. sort: Sort lines of text files.
    • Scenario 1: Sort lines in a file: sort file.txt
    • Scenario 2: Sort lines numerically: sort -n file.txt
  55. alias: Create shortcuts for commands.
    • Scenario 1: Create an alias for a command: alias ll='ls -l'
    • Scenario 2: List all defined aliases: alias
  56. diff: Compare files line by line.
    • Scenario 1: Compare two files and display differences: diff file1.txt file2.txt
    • Scenario 2: Show differences between two directories: diff -r dir1/ dir2/
  57. gzip: Compress files.
    • Scenario 1: Compress a file: gzip file.txt
    • Scenario 2: Compress multiple files: gzip file1 file2
    • Scenario 3: Compress multiple files using wildcards: gzip *.txt
  58. date: Display or set the system date and time.
    • Scenario 1: Set the system date and time: sudo date MMDDhhmmYYYY
    • Scenario 2: Display date and time in UTC: date -u
  59. uptime: Show how long the system has been running.
    • Scenario 1: Display system uptime: uptime
    • Scenario 2: Show load average for the last 1, 5, and 15 minutes: uptime -p

Conclusion

It's important to understand that nobody knows these Linux commands 100% by heart. For example, I sometimes forgot the usage find - a quick Google search is no big deal. Using them more often gives you tho aha! moments where you remembered it for sure. What's important is knowing the right tool for the job.