Bash Commands Cheat Sheet โœจ

ยท

2 min read

Basic Commands

Displaying Output:

  • Echo a calculation result:

      num1=4
      num2=5
      echo $((num1+num2))   # Prints: 9
      echo 'end'            # Prints: end
    

    ๐Ÿ”— Tip: echo is great for printing text or results in the terminal!


Get the Current Directory:

  • Print working directory:

      pwd
    

    ๐Ÿ” Displays the current location in the filesystem.

Check Current User:

  • Who am I?

      whoami
    

    ๐Ÿ‘ค Shows the username of the logged-in user.


Listing Directory Contents ๐Ÿ”

See All Files and Folders:

  • Basic listing:

      ls
    
  • Show hidden files and folders:

      ls -a
    
  • Show permissions of contents:

      ls -la
    

    โšก Pro Tip: Permissions include read (r), write (w), and execute (x).


File and Folder Operations ๐Ÿ“

Create Files:

  • Using touch command:

      touch script.js script1.js script2.js script3.js
    

Create Folders:

  • Using mkdir command:

      mkdir src
    

Copy Files:

  • Copy a file into a folder:

      cp fileNameToCopy folderNameToPaste
    

Move Files:

  • Move a file to another folder:

      mv fileNameToCopy folderNameToPaste
    

Rename Files:

  • Move and rename a file:

      mv fileNameToMoveOrRename newFileName
    

Delete Files and Folders:

  • Delete a file:

      rm fileName
    
  • Delete an empty folder:

      rmdir folderAddress
    
  • Delete a folder with contents:

      rm -r folderAddress  # -r means recursive
    

    โšก Pro Tip: Use with caution as this permanently deletes files!


For Loops in Bash ๐Ÿ”„

Create Multiple Files:

  • 100 JavaScript files:

      for i in {1..100}; do touch "app$i.js"; done
    

Delete Multiple Files:

  • Remove those 100 files:

      for i in {1..100}; do rm "app$i.js"; done
    

Viewing and Editing Files ๐Ÿ”ง

View File Data:

  • Using cat command:

      cat fileName
    

    โœจ Note: cat means concatenate and is used to display file content.

Edit File Data:

  • Using nano editor:

      nano fileName
    
  • Using vim editor:

      vim fileName
    

    โšก Pro Tip: To save in vim, press ESC then type :wq to write and quit.


Summary

This cheat sheet gives you a quick and interactive guide to essential bash commands. From managing files ๐Ÿ“‚ to looping ๐Ÿ”„ and editing content ๐Ÿ”ง, bash scripting makes automating tasks fun and efficient!

Happy Coding ๐Ÿš€

ย