Command Line and Working with Bash

This contents are mainly from freeCodeCamp tutorial.

What is terminal and what is command line

  • The command line is a basic text input interface which allows a user to enter “commands”,
  • A terminal is a special application that offers a command line interface to perform system-level commands beyond the basic read/write operations.
  • A shell is the software that wraps the command line and interprets your inputs as commands, returning the output.

Command Line Shortcuts

  1. Command Line Shortcuts for Productivity
    1. Terminal and command line shortcuts help speed up workflow and improve productivity.
    2. Many shortcuts are the same on Linux and macOS because both are Unix-like systems.
    3. Windows/PowerShell has some different behavior.
  2. Up Arrow / Down Arrow
    1. Up Arrow cycles backward through command history.
    2. Down Arrow cycles forward through command history.
    3. Useful for reusing previously run commands quickly.
  3. Tab Auto-Complete
    1. Tab can complete a command or suggestion automatically.
    2. This saves typing time and reduces mistakes.
    3. Different shells generate suggestions differently:
      1. zsh : suggestions are influenced by recent command history.
      2. PowerShell : suggestions are based on available commands, variables, and arguments.
  4. Clear the Screen
    1. In Unix-like terminals ( *nix ), Control + L clears the screen.
    2. This gives you a fresh prompt and removes visual clutter.
    3. In PowerShell, you usually use:
      1. cls
    4. PowerShell can also bind this action to a shortcut like Control + L .
  5. Interrupt a Running Command
    1. Control + C stops the current command/process.
    2. This terminates execution and returns you to a new prompt.
    3. In PowerShell, Control + C can also mean copy when text is selected, so behavior depends on context.
  6. Send a Process to the Background
    1. In Unix-like terminals, Control + Z places the current process into the background.
    2. This lets you return to the command line and continue other work.
    3. To bring the process back to the foreground, use:
      1. fg
  7. Run the Last Command Again
    1. Typing !! and pressing Enter reruns the last executed command.
    2. This is faster than scrolling back through history when you only need the most recent command.
  8. Platform Note
    1. Some shortcuts such as Control + Z and fg are Unix-like terminal features.
    2. They do not have a direct PowerShell equivalent in this lesson.
  9. Main Takeaway
    1. These shortcuts are basic but powerful tools for working faster in the terminal.
    2. Learning them is a good starting point for becoming more efficient with the command line.
  10. Questions and Answers
    1. Which key combination is used to clear the screen in *nix based terminals?
      1. Control + L
    2. What happens when you press Control + Z in a *nix based terminal?
      1. It places the current process in the background.
    3. Which command can be used to quickly run the last executed command in a *nix based terminal?
      1. !!

What is bash and some of its basic commands

  • Bash Overview
    1. Bash stands for Bourne Again SHell .
    2. It is one of the most common shells in Unix-like systems.
    3. Learning Bash helps you navigate and manage files in the terminal.
  1. pwd
    1. pwd means print working directory .
    2. It shows the current directory your terminal is pointing to.
    3. The working directory is important because many commands act on the current location.
  2. cd
    1. cd means change directory .
    2. It lets you move from one directory to another.
    3. You can use:
      1. Absolute path:
        1. Starts with /
      2. Relative path:
        1. No leading /
      3. Parent directory:
        1. .. means move up one level
  3. ls
    1. ls lists the contents of the current directory.
    2. It helps you check which files and folders exist.
    3. Common flags:
      1. ls -a
        1. Shows hidden files
      2. ls -l
        1. Shows details like permissions and file information
  4. Viewing File Contents
    1. cat displays the full content of a file in the terminal.
    2. less lets you view file contents one screen at a time.
    3. These are useful when you want to inspect a file quickly.
  5. mkdir
    1. mkdir creates a new directory (folder).
    2. Example:
      1. mkdir notes
  6. touch
    1. touch creates a new file.
    2. Example:
      1. touch readme.md
    3. This creates a new Markdown file named readme.md .
  7. mv
    1. mv moves or renames a file or directory.
    2. It takes:
      1. Old name/path
      2. New name/path
    3. Example:
      1. mv readme.md readthis.md
    4. This renames readme.md to readthis.md .
  8. rm
    1. rm removes (deletes) a file.
    2. rm -r removes a directory recursively.
    3. rm -f forces removal if the file is protected.
    4. Be careful because deleted files are usually not easy to recover.
  9. cp
    1. cp copies a file or directory.
    2. Unlike mv , it keeps the original file.
    3. To copy a directory, use:
      1. cp -r
    4. Example:
      1. cp readme.md backup.md
  10. echo
    1. echo prints text to the terminal.
    2. It is similar to print() or console.log() .
    3. Example:
      1. echo "Hello"
    4. It can also write text into files:
      1. > creates or overwrites a file
      2. >> appends text to a file
  11. echo with Redirection
    1. Example:
      1. echo "Naomi was here." > readme.md
        1. Creates or overwrites readme.md with that text
    2. Example:
      1. echo "Another line" >> readme.md
        1. Adds the text to the end of the file
  12. man
    1. man shows the manual page for a command.
    2. It is useful when you forget how a command works.
    3. Example:
      1. man ls
  13. Other Commands Mentioned
    1. head
      1. Shows the beginning of a file
    2. tail
      1. Shows the end of a file
    3. chown
      1. Changes file owner
    4. chmod
      1. Changes file permissions
  14. Main Takeaway
    1. Bash provides commands for:
      1. Navigating directories
      2. Listing files
      3. Creating files and folders
      4. Renaming, moving, copying, and deleting items
      5. Viewing and writing file contents
    2. You do not need to memorize every command immediately.
    3. For unfamiliar commands, use the manual page with man .
  15. find to find things or view a file tree. ### Command options and flags
  16. Options / Flags Overview
    1. Options or flags are special arguments passed to a command to change how the command behaves.
    2. The terms options and flags are often used interchangeably.
    3. Flags is usually used more specifically for options that act like an on/off switch.
  17. How Options Look
    1. Options are usually prefixed with one or two hyphens - .
    2. This helps distinguish them from normal arguments.
  18. Long Form Options
    1. Long form options use two hyphens:
      1. --option
    2. Examples:
      1. --version
        1. Prints the current version of the application
      2. --help
        1. Shows usage instructions for the command
  19. Examples of Long Form
    1. ls --version
      1. Shows the version of ls
    2. ls --help
      1. Shows how to use ls
  20. Short Form Options
    1. Short form options use one hyphen:
      1. -a
    2. They are usually a single letter.
    3. Example:
      1. ls -a
        1. Lists all files, including hidden files like .env
  21. Combining Short Options
    1. Short options can often be chained together.
    2. Example:
      1. ls -ahs
    3. This is a shorter form of:
      1. ls --all --human-readable --size
  22. Options That Require a Value
    1. Some options need an extra value.
    2. That value changes how the option works.
  23. Long Form with a Value
    1. Long form usually uses an equals sign = .
    2. Example:
      1. ls --color=never
    3. Meaning:
      1. Set the color option to never
  24. Short Form with a Value
    1. Short form usually separates the option and value with a space.
    2. Example:
      1. ls -w 50
    3. Meaning:
      1. Set width to 50
  25. Equivalent Short and Long Forms
    1. Short form:
      1. ls -w 50
    2. Long form:
      1. ls --width=50
    3. Both set the width of the ls output.
  26. Important Difference
    1. Short form usually looks like:
      1. -w 50
    2. Long form usually looks like:
      1. --width=50
    3. If you mix these up, the shell may treat the value as a normal positional argument instead of an option value.
  27. How to Check the Correct Syntax
    1. If you are unsure how to use an option, use:
      1. --help
    2. Example:
      1. ls --help
    3. This usually shows the correct syntax and available options.
  28. Main Takeaway
    1. Options/flags modify how a command behaves.
    2. Long form uses --option and often --option=value .
    3. Short form uses -o and often -o value .
    4. Short flags can often be combined together.

ls -l -l is long list format which shows the detail information of the file such as go back two folders with cd ../..

Summary

Terminal, Shell, and Command Line Basics

  • Command line: A text interface where users type commands.
  • Terminal: The application that provides access to the command line.
  • Terminal emulator: Adds extra features to a terminal.
  • Shell: Interprets the commands entered into the terminal (e.g., Bash).
  • PowerShell / Command Prompt / Microsoft Terminal: Options for accessing the command line on Windows.
  • Terminal (macOS): Built-in option on macOS, with third-party alternatives like iTerm or Ghostty.
  • Terminal (Linux): Options vary by distribution, with many third-party emulators like kitty.
  • Terminology: Though “terminal,” “shell,” and “command line” are often used interchangeably, they have specific meanings.

Command Line Shortcuts

  • Up/Down arrows: Cycle through previous/next commands in history.
  • Tab: Autocomplete commands.
  • ** Control+L ** (Linux/macOS) or typing cls (Windows): Clear the terminal screen.
  • ** Control+C **: Interrupt a running command (also used for copying in PowerShell if text is selected).
  • ** Control+Z ** (Linux/macOS only): Suspend a task to the background; use fg to resume it.
  • ** !! **: Instantly rerun the last executed command.

Bash Basics

  • Bash (Bourne Again Shell): Widely used Unix-like shell. Key commands:
    • pwd: Show the current directory.
    • cd: Change directories.
      • .. refers to the parent directory (one level up).
      • . refers to the current directory.
    • ls: List files and folders.
      • -a: Show all files, including hidden files.
      • -l: Show detailed information about files.
    • less: View file contents one page at a time with navigation options, including scrolling backward and searching.
    • more: Display file contents one screen at a time, with limited backward scrolling and basic navigation.
    • cat: Show the entire file content at once without scrolling or navigation, useful for smaller files.
    • mkdir: Create a new directory.
    • rmdir: Remove an empty directory.
    • touch: Create a new file.
    • mv: Move or rename files.
      • Rename: mv oldname.txt newname.txt
      • Move: mv filename.txt /path/to/target/
    • cp: Copy files.
      • -r: Recursively copy directories and their contents.
    • rm: Delete files.
      • -r: Recursively delete directories and their contents.
    • echo: Display a line of text or a variable’s value.
      • Use > to overwrite the existing content in a file. (e.g., echo "text" > file.txt)
      • Use >> to append output to a file without overwriting existing content (e.g., echo "text" >> file.txt).
    • exit: Exit the terminal session.
    • clear: Clear the terminal screen.
    • find: Search for files and directories.
      • -name: Search for files by name pattern (e.g., find . -name "*.txt").
    • Use man followed by a command (e.g., man ls) to access detailed manual/help pages.

Command Options and Flags

  • Options or flags: modify a command’s behavior and are usually prefixed with hyphens:
    • Long form (two hyphens):
      • Example: --help, --version
      • Values are attached using an equals sign, e.g., --width=50.
    • Short form (one hyphen):
      • Example: -a, -l
      • Values are passed with a space, e.g., -w 50.
      • Multiple short options can be chained together, e.g., ls -alh.
  • --help: You can always use a command with this flag to understand the available options for any command.