CHEAT SHEET ABOUT LINUX COMMANDS

Cheet sheet

Checking the Type of a Command

  • Use type to see if a command is built-in or external:

     
    type cd type cal
  • Add -a to show all possible locations for a command:

     
    type -a echo

Finding Where a Command Is Located

  • which looks through your PATH to find where a command is:

     
    which ls which cal
  • whereis gives you the path to the binary, source, and man pages:

     
    whereis ls
  • locate searches a pre-built database of files on your system:

     
    locate gshadow

Creating Aliases and Functions

  • Set a quick alias:

     
    alias today="date" today
  • Define a simple function:

     
    my_func() { command1 command2 }

    Example:

     
    show_home() { ls -l /home echo "Done" } show_home

Combining Commands

  • ; runs multiple commands in sequence, regardless of success:

     
    cal 1 2030 ; cal 2 2030 ; cal 3 2030
  • | (pipe) passes the output of one command to another:

     
    cal 1 2030 | cal 2 2030
  • && runs the second command only if the first one works:

     
    ls /etc/ppp/ && echo "Success"
  • || runs the second command only if the first one fails:

     
    ls /etc/ppp/ || echo "ls failed"

📚 Getting Help and Documentation

  • man opens the manual for a command:

     
    man ls
  • whatis gives a short description and man section:

     
    whatis ls
  • info opens a more detailed help system:

     
    info ls
  • --help gives a brief overview of how to use a command:

     
    cat --help
  • README files (docs):

     
    ls /usr/share/doc

Navigation and Wildcards

  • pwd: Shows your current directory.

  • Wildcards:

    • * = any number of characters

    • ? = exactly one character

    • [ab] = any character listed between brackets

    Examples:

     
    echo /etc/s* # Starts with "s" echo /etc/t??????? # Starts with "t" + 7 characters echo /etc/[gu]* # Starts with "g" or "u"

Working with Files

  • Copy with verbose output:

     
    cp -v file1 file2
  • Create an archive using tar:

     
    tar -cf archive.tar file.png

Viewing File Content

  • Use less to scroll through large files (H for help).

  • head shows the top lines of a file:

     
    head /etc/sysctl.conf
  • tail shows the bottom lines:

     
    tail -5 /etc/sysctl.conf

Text Processing Tools

  • sort: Orders lines alphabetically:

     
    sort /etc/sysctl.conf
  • wc: Counts lines, words, and characters:

     
    wc /etc/sysctl.conf
  • cut: Extracts fields from a file using a delimiter:

     
    cut -d: -f1,5-7 mypasswd
  • grep: Filters lines based on a matching pattern:

     
    grep bash /etc/passwd grep 'r..f' red.txt # Matches words with r, any 2 chars, then f
     

Leave a Comment

Visió general de la privadesa

Aquest lloc web utilitza galetes per poder oferir-te la millor experiència d'usuari possible. La informació de les galetes s'emmagatzema al teu navegador i realitza funcions com reconèixer-te quan tornes al nostre lloc web i ajudar el nostre equip a entendre quines seccions del lloc web trobes més interessants i útils.