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
- Command Line Shortcuts for Productivity
- Terminal and command line shortcuts help speed up workflow and improve productivity.
- Many shortcuts are the same on Linux and macOS because both are Unix-like systems.
- Windows/PowerShell has some different behavior.
- Up Arrow / Down Arrow
Up Arrowcycles backward through command history.Down Arrowcycles forward through command history.- Useful for reusing previously run commands quickly.
- Tab Auto-Complete
Tabcan complete a command or suggestion automatically.- This saves typing time and reduces mistakes.
- Different shells generate suggestions differently:
zsh: suggestions are influenced by recent command history.PowerShell: suggestions are based on available commands, variables, and arguments.
- Clear the Screen
- In Unix-like terminals (
*nix),Control + Lclears the screen. - This gives you a fresh prompt and removes visual clutter.
- In PowerShell, you usually use:
cls
- PowerShell can also bind this action to a shortcut like
Control + L.
- In Unix-like terminals (
- Interrupt a Running Command
Control + Cstops the current command/process.- This terminates execution and returns you to a new prompt.
- In PowerShell,
Control + Ccan also mean copy when text is selected, so behavior depends on context.
- Send a Process to the Background
- In Unix-like terminals,
Control + Zplaces the current process into the background. - This lets you return to the command line and continue other work.
- To bring the process back to the foreground, use:
fg
- In Unix-like terminals,
- Run the Last Command Again
- Typing
!!and pressing Enter reruns the last executed command. - This is faster than scrolling back through history when you only need the most recent command.
- Typing
- Platform Note
- Some shortcuts such as
Control + Zandfgare Unix-like terminal features. - They do not have a direct PowerShell equivalent in this lesson.
- Some shortcuts such as
- Main Takeaway
- These shortcuts are basic but powerful tools for working faster in the terminal.
- Learning them is a good starting point for becoming more efficient with the command line.
- Questions and Answers
- Which key combination is used to clear the screen in
*nixbased terminals?Control + L
- What happens when you press
Control + Zin a*nixbased terminal?- It places the current process in the background.
- Which command can be used to quickly run the last executed command in a
*nixbased terminal?!!
- Which key combination is used to clear the screen in
What is bash and some of its basic commands
- Bash Overview
Bashstands forBourne Again SHell.- It is one of the most common shells in Unix-like systems.
- Learning Bash helps you navigate and manage files in the terminal.
- pwd
pwdmeansprint working directory.- It shows the current directory your terminal is pointing to.
- The working directory is important because many commands act on the current location.
- cd
cdmeanschange directory.- It lets you move from one directory to another.
- You can use:
- Absolute path:
- Starts with
/
- Starts with
- Relative path:
- No leading
/
- No leading
- Parent directory:
..means move up one level
- Absolute path:
- ls
lslists the contents of the current directory.- It helps you check which files and folders exist.
- Common flags:
ls -a- Shows hidden files
ls -l- Shows details like permissions and file information
- Viewing File Contents
catdisplays the full content of a file in the terminal.lesslets you view file contents one screen at a time.- These are useful when you want to inspect a file quickly.
- mkdir
mkdircreates a new directory (folder).- Example:
mkdir notes
- touch
touchcreates a new file.- Example:
touch readme.md
- This creates a new Markdown file named
readme.md.
- mv
mvmoves or renames a file or directory.- It takes:
- Old name/path
- New name/path
- Example:
mv readme.md readthis.md
- This renames
readme.mdtoreadthis.md.
- rm
rmremoves (deletes) a file.rm -rremoves a directory recursively.rm -fforces removal if the file is protected.- Be careful because deleted files are usually not easy to recover.
- cp
cpcopies a file or directory.- Unlike
mv, it keeps the original file. - To copy a directory, use:
cp -r
- Example:
cp readme.md backup.md
- echo
echoprints text to the terminal.- It is similar to
print()orconsole.log(). - Example:
echo "Hello"
- It can also write text into files:
>creates or overwrites a file>>appends text to a file
- echo with Redirection
- Example:
echo "Naomi was here." > readme.md- Creates or overwrites
readme.mdwith that text
- Creates or overwrites
- Example:
echo "Another line" >> readme.md- Adds the text to the end of the file
- Example:
- man
manshows the manual page for a command.- It is useful when you forget how a command works.
- Example:
man ls
- Other Commands Mentioned
head- Shows the beginning of a file
tail- Shows the end of a file
chown- Changes file owner
chmod- Changes file permissions
- Main Takeaway
- Bash provides commands for:
- Navigating directories
- Listing files
- Creating files and folders
- Renaming, moving, copying, and deleting items
- Viewing and writing file contents
- You do not need to memorize every command immediately.
- For unfamiliar commands, use the manual page with
man.
- Bash provides commands for:
findto find things or view a file tree. ### Command options and flags- Options / Flags Overview
- Options or flags are special arguments passed to a command to change how the command behaves.
- The terms
optionsandflagsare often used interchangeably. Flagsis usually used more specifically for options that act like an on/off switch.
- How Options Look
- Options are usually prefixed with one or two hyphens
-. - This helps distinguish them from normal arguments.
- Options are usually prefixed with one or two hyphens
- Long Form Options
- Long form options use two hyphens:
--option
- Examples:
--version- Prints the current version of the application
--help- Shows usage instructions for the command
- Long form options use two hyphens:
- Examples of Long Form
ls --version- Shows the version of
ls
- Shows the version of
ls --help- Shows how to use
ls
- Shows how to use
- Short Form Options
- Short form options use one hyphen:
-a
- They are usually a single letter.
- Example:
ls -a- Lists all files, including hidden files like
.env
- Lists all files, including hidden files like
- Short form options use one hyphen:
- Combining Short Options
- Short options can often be chained together.
- Example:
ls -ahs
- This is a shorter form of:
ls --all --human-readable --size
- Options That Require a Value
- Some options need an extra value.
- That value changes how the option works.
- Long Form with a Value
- Long form usually uses an equals sign
=. - Example:
ls --color=never
- Meaning:
- Set the
coloroption tonever
- Set the
- Long form usually uses an equals sign
- Short Form with a Value
- Short form usually separates the option and value with a space.
- Example:
ls -w 50
- Meaning:
- Set width to
50
- Set width to
- Equivalent Short and Long Forms
- Short form:
ls -w 50
- Long form:
ls --width=50
- Both set the width of the
lsoutput.
- Short form:
- Important Difference
- Short form usually looks like:
-w 50
- Long form usually looks like:
--width=50
- If you mix these up, the shell may treat the value as a normal positional argument instead of an option value.
- Short form usually looks like:
- How to Check the Correct Syntax
- If you are unsure how to use an option, use:
--help
- Example:
ls --help
- This usually shows the correct syntax and available options.
- If you are unsure how to use an option, use:
- Main Takeaway
- Options/flags modify how a command behaves.
- Long form uses
--optionand often--option=value. - Short form uses
-oand often-o value. - 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 typingcls(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; usefgto 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/
- Rename:
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).
- Use
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
manfollowed 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.
- Example:
- 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.
- Example:
- Long form (two hyphens):
--help: You can always use a command with this flag to understand the available options for any command.