Skip to content

Latest commit

 

History

History
669 lines (536 loc) · 17.9 KB

File metadata and controls

669 lines (536 loc) · 17.9 KB

Below is a list of common Linux commands with brief descriptions and examples. For more detailed explanations and options, please follow the links to the specific .md files.

Table of Contents


Basic Commands

  1. ls - List files and directories.

    ls  # list files and directories in the current location

    For extra details, visit ls.md.

  2. cd - Change directory.

    cd /path/to/directory  # change to the specified directory

    For extra details, visit cd.md.

  3. pwd - Print working directory.

    pwd  # display the current directory path
  4. mkdir - Create a new directory.

    mkdir new_directory  # create a new directory named 'new_directory'
  5. rmdir - Remove an empty directory.

    rmdir old_directory  # remove the empty directory 'old_directory'
  6. touch - Create an empty file or update the timestamp of an existing file.

    touch newfile.txt  # create a new empty file named 'newfile.txt'
  7. rm - Remove files or directories.

    rm file.txt  # remove the file named 'file.txt'

    For extra details, visit rm.md.

  8. cp - Copy files or directories.

    cp source.txt destination.txt  # copy 'source.txt' to 'destination.txt'

    For extra details, visit cp.md.

  9. mv - Move or rename files and directories.

    mv oldname.txt newname.txt  # rename 'oldname.txt' to 'newname.txt'

    For extra details, visit mv.md.

  10. cat - Concatenate and display file contents.

    cat file.txt  # display the contents of 'file.txt'
  11. echo - Display a line of text.

    echo "Hello, World!"  # print 'Hello, World!' to the terminal
  12. head - Output the first part of files.

    head file.txt  # display the first 10 lines of 'file.txt'

    For extra details, visit head.md.

  13. tail - Output the last part of files.

    tail file.txt  # display the last 10 lines of 'file.txt'

    For extra details, visit tail.md.

  14. less - View file contents interactively.

    less file.txt  # view 'file.txt' in interactive mode
  15. more - View file contents one page at a time.

    more file.txt  # view 'file.txt' one page at a time
  16. find - Search for files in a directory hierarchy.

    find /path -name "filename"  # find 'filename' within '/path'

    For extra details, visit find.md.

  17. grep - Search text using patterns.

    grep "pattern" file.txt  # search for 'pattern' in 'file.txt'

    For extra details, visit grep.md.

  18. chmod - Change file permissions.

    chmod 755 script.sh  # set execute permissions for 'script.sh'

    For extra details, visit chmod.md.

  19. chown - Change file owner and group.

    chown user:group file.txt  # change owner and group of 'file.txt'

    For extra details, visit chown.md.

  20. man - Display the manual page for a command.

    man ls  # show the manual page for the 'ls' command

    For extra details, visit man.md.

  21. wc - Print newline, word, and byte counts for each file.

    wc file.txt  # display line, word, and byte count of 'file.txt'

    For extra details, visit wc.md.

  22. date - Display or set the system date and time.

    date  # display the current system date and time

    For extra details, visit date.md.

  23. file - Determine file type.

    file file.txt  # determine the type of 'file.txt'

    For extra details, visit file.md.

  24. history - Display or manipulate the command history.

    history  # show the command history

    For extra details, visit history.md.

  25. ln - Create links between files.

    ln source.txt link.txt  # create a hard link named 'link.txt' to 'source.txt'

    For extra details, visit ln.md.


System Information

  1. uname - Print system information.

    uname -a  # display all system information

    For extra details, visit uname.md.

  2. df - Display disk space usage.

    df -h  # display disk space in human-readable format
  3. du - Estimate file and directory space usage.

    du -sh /path  # show total size of '/path'

    For extra details, visit du.md.

  4. top - Display real-time system processes.

    top  # show running processes and system statistics

    For extra details, visit top.md.

  5. htop - Interactive process viewer.

    htop  # interactive system monitor with more features than 'top'
  6. ps - Report a snapshot of current processes.

    ps aux  # list all running processes with detailed information

    For extra details, visit ps.md.

  7. kill - Terminate processes.

    kill PID  # terminate the process with specified PID

    For extra details, visit kill.md.

  8. uptime - Show how long the system has been running.

    uptime  # display system uptime and load average
  9. free - Display memory usage.

    free -h  # show memory usage in human-readable format
  10. who - Show who is logged into the system.

    who  # list users currently logged in
  11. hostname - Show or set the system's hostname.

    hostname  # display current hostname
  12. dmesg - Print kernel ring buffer messages.

    dmesg | less  # view kernel messages interactively
  13. lscpu - Display CPU architecture information.

    lscpu  # show CPU architecture details
  14. lsblk - List block devices.

    lsblk  # display information about block devices
  15. ifconfig - Configure network interfaces (deprecated, use ip).

    ifconfig  # display network interface information

    For extra details, visit ifconfig.md.

  16. ip - Show/manipulate routing, devices, policy routing.

    ip addr show  # display IP addresses for all interfaces

    For extra details, visit [ip.md](./system-info/ip

.md).

  1. ping - Send ICMP ECHO_REQUEST to network hosts.

    ping example.com  # ping 'example.com' to check connectivity
  2. netstat - Print network connections, routing tables, etc.

    netstat -tuln  # list listening ports and connections
  3. ss - Another utility to investigate sockets.

    ss -tuln  # display open ports and connections
  4. traceroute - Trace the route to a network host.

    traceroute example.com  # show the path packets take to 'example.com'

File Compression and Archiving

  1. tar - Archive files.

    tar -cvf archive.tar /path  # create 'archive.tar' from '/path'

    For extra details, visit tar.md.

  2. gzip - Compress files.

    gzip file.txt  # compress 'file.txt' to 'file.txt.gz'
  3. gunzip - Decompress files.

    gunzip file.txt.gz  # decompress 'file.txt.gz'
  4. zip - Package and compress files.

    zip archive.zip file1 file2  # compress 'file1' and 'file2' into 'archive.zip'
  5. unzip - Extract compressed files.

    unzip archive.zip  # extract 'archive.zip'

Text Processing

  1. awk - Pattern scanning and processing language.

    awk '{print $1}' file.txt  # print the first column of 'file.txt'
  2. sed - Stream editor for filtering and transforming text.

    sed 's/old/new/g' file.txt  # replace 'old' with 'new' in 'file.txt'

    For extra details, visit sed.md.

  3. cut - Remove sections from each line of files.

    cut -d ',' -f 1 file.csv  # extract the first column from 'file.csv'
  4. sort - Sort lines of text files.

    sort file.txt  # sort lines in 'file.txt'
  5. uniq - Report or omit repeated lines.

    uniq sorted.txt  # remove duplicate lines in 'sorted.txt'

Advanced Commands and Topics

  1. ln - Create links between files.

    ln source.txt link.txt  # create a hard link named 'link.txt' to 'source.txt'

    For extra details, visit ln.md.

  2. Expansion - Shell expansion allows you to use variables, wildcards, and other features in commands.

    echo {1..5}  # generates a sequence: 1 2 3 4 5

    For extra details, visit expansion.md.

  3. Pattern Matching - Matching file and directory names using patterns.

    ls *.txt  # list all '.txt' files in the directory

    For extra details, visit pattern-matching.md.

  4. Redirect & Pipelines - Redirect input and output, and use pipelines to connect commands.

    ls > files.txt  # redirect output of 'ls' to 'files.txt'
    cat file.txt | grep "pattern"  # use a pipeline to search 'pattern' in 'file.txt'

    For extra details, visit redirect-pipelines.md.

  5. Environment Variables - Variables that affect the way processes run on your system.

    echo $PATH  # display the current PATH environment variable

    For extra details, visit environment-variables.md.


User Management Commands

  1. useradd - Create a new user account.

    useradd newuser  # create a user named 'newuser'

    For extra details, visit useradd.md.

  2. usermod - Modify a user account.

    usermod -aG groupname username  # add 'username' to 'groupname'

    For extra details, visit usermod.md.

  3. userdel - Delete a user account.

    userdel username  # delete the user 'username'

    For extra details, visit userdel.md.

  4. passwd - Change a user password.

    passwd username  # change password for 'username'

    For extra details, visit passwd.md.

  5. groupadd - Create a new group.

    groupadd groupname  # create a group named 'groupname'

    For extra details, visit groupadd.md.

  6. groupdel - Delete a group.

    groupdel groupname  # delete the group named 'groupname'

    For extra details, visit groupdel.md.

  7. groups - Show group memberships.

    groups username  # display groups 'username' belongs to

Detailed Command Files

For commands that require additional explanations, options, or examples, create markdown files as follows:

grep.md

grep - Search text using patterns.

  • Options:

    • -i : Ignore case distinctions.
    • -r : Read all files under each directory, recursively.
    • -v : Invert match to select non-matching lines.
    • -n : Prefix each line of output with the line number within its input file.
    • -l : Print file names with matches, once for each file.
    • -c : Suppress normal output, counting lines matching instead.
  • Example:

    grep -i "error" /var/log/syslog  # search for 'error' in '/var/log/syslog', ignoring case
  • Common Use Cases:

    • Search for a string in a file:

      grep "hello" file.txt
    • Search recursively in directories:

      grep -r "pattern" /path/to/dir
    • Count matching lines:

      grep -c "pattern" file.txt

expansion.md

Expansion - Shell expansion enables the use of variables, wildcards, and other features in commands.

  • Types of Expansion:
    • Brace Expansion: {} Generate arbitrary strings.

      echo {A..E}  # outputs: A B C D E
    • Parameter Expansion: $ Used to expand variables.

      echo $USER  # outputs the current username
    • Command Substitution: ` ` or $() Executes commands and uses the result.

      echo "Today is $(date)"
    • Arithmetic Expansion: $(( )) Perform arithmetic operations.

      echo $((5 + 3))  # outputs: 8
    • Tilde Expansion: ~ Expands to the home directory.

      cd ~  # changes to the home directory
    • Pathname Expansion: *, ? Wildcards for matching filenames.

      ls *.txt  # lists all .txt files

pattern-matching.md

Pattern Matching - File and directory name matching using patterns.

  • Wildcards:

    • * : Matches any number of characters.

      ls *.jpg  # matches all .jpg files
    • ? : Matches a single character.

      ls file?.txt  # matches file1.txt, file2.txt, etc.
    • [ ] : Matches any one of the enclosed characters.

      ls file[12].txt  # matches file1.txt or file2.txt
  • Example:

    ls *.log  # list all .log files in the directory
  • Advanced Patterns:

    • Bracket Expressions: [a-z], [!abc], [[:digit:]]
    • Extended Globbing: ?(pattern), *(pattern), +(pattern), @(pattern), !(pattern)

redirect-pipelines.md

Redirect & Pipelines - Managing

input/output in Linux commands.

  • Redirection Operators:

    • > : Redirect standard output.

      echo "Hello" > file.txt  # write 'Hello' to 'file.txt'
    • >> : Append standard output.

      echo "World" >> file.txt  # append 'World' to 'file.txt'
    • < : Redirect standard input.

      sort < file.txt  # sort lines from 'file.txt'
    • 2> : Redirect standard error.

      ls /nonexistent 2> error.log  # redirect errors to 'error.log'
  • Pipelines (|):

    • Connect multiple commands.
      cat file.txt | grep "pattern" | sort  # chain commands with pipes
  • Examples:

    ls -l > filelist.txt  # save the output to 'filelist.txt'
    grep "error" syslog | less  # view matching lines with 'less'

environment-variables.md

Environment Variables - Affect process behavior in Linux.

  • Common Environment Variables:

    • $HOME : User's home directory.
    • $PATH : Directories to search for executable files.
    • $USER : Current logged-in user.
    • $SHELL : Default shell for the user.
    • $EDITOR : Default text editor.
  • Setting Environment Variables:

    export VAR=value  # set 'VAR' to 'value'
  • Displaying Environment Variables:

    echo $PATH  # display PATH variable
  • Using Variables:

    export GREETING="Hello"
    echo $GREETING  # outputs: Hello

useradd.md

useradd - Create new user accounts.

  • Options:

    • -m : Create the user's home directory.
    • -G : Add user to additional groups.
    • -s : Specify user's login shell.
    • -c : Add a comment about the user.
  • Example:

    useradd -m -s /bin/bash -G sudo newuser  # create a user with a home directory and add to 'sudo' group
  • Common Use Cases:

    • Create a User:

      useradd -m username
    • Add a User to a Group:

      useradd -G groupname username

Directory Structure

Here is the updated directory structure for the cheat sheet project, including the new sections:

.
├── README.md
├── basic-commands
│   ├── cd.md
│   ├── cp.md
│   ├── find.md
│   ├── grep.md
│   ├── head.md
│   ├── ls.md
│   ├── mv.md
│   ├── rm.md
│   ├── chmod.md
│   ├── chown.md
│   ├── tail.md
│   ├── wc.md
│   ├── date.md
│   ├── file.md
│   ├── ln.md
│   └── ...
├── file-compression
│   ├── tar.md
│   └── ...
├── system-info
│   ├── du.md
│   ├── ifconfig.md
│   ├── ip.md
│   ├── kill.md
│   ├── ps.md
│   ├── top.md
│   ├── uname.md
│   └── ...
├── text-processing
│   ├── grep.md
│   ├── sed.md
│   └── ...
├── advanced
│   ├── man.md
│   ├── history.md
│   ├── expansion.md
│   ├── pattern-matching.md
│   ├── redirect-pipelines.md
│   ├── environment-variables.md
│   └── ...
└── user-management
    ├── useradd.md
    ├── usermod.md
    ├── userdel.md
    ├── passwd.md
    ├── groupadd.md
    ├── groupdel.md
    └── ...

This structure provides a comprehensive guide to Linux commands and topics, categorized for easy navigation and deeper learning. Each markdown file should include detailed explanations, options, examples, and common use cases to help users master Linux commands effectively.