backend / linux bash / 01_bash_commands.md

Basic Bash Commands

3 interview angles 3 min read source

Basic Bash Commands

Bash (Bourne Again Shell) is a command-line interface used for interacting with the operating system. Below are some fundamental Bash commands to get started:


1. Navigating the File System

  • pwd: Prints the current working directory.
  • ls: Lists files and directories in the current directory.
    • ls -l: Lists details like file permissions, owner, size, and date modified.
    • ls -a: Lists all files, including hidden files.
  • cd <directory>: Changes the current directory to the specified one.
    • cd ..: Moves up one directory.

2. Working with Files and Directories

  • touch <file>: Creates an empty file.
  • mkdir <directory>: Creates a new directory.
  • cp <source> <destination>: Copies files or directories.
  • mv <source> <destination>: Moves or renames files or directories.
  • rm <file>: Deletes a file.
    • rm -r <directory>: Deletes a directory and its contents recursively.

3. Viewing and Editing Files

  • cat <file>: Displays the contents of a file.
  • less <file>: Views file content one screen at a time.
  • nano <file>: Opens the file in the Nano text editor for editing.

4. Managing Processes

  • ps: Displays a list of currently running processes.
  • kill <process-id>: Terminates a process by its ID.
  • top: Displays real-time information about system processes.

5. File Permissions

  • chmod <permissions> <file>: Changes the permissions of a file or directory.
    • Example: chmod 755 script.sh gives execute permission to the owner.
  • chown <owner>:<group> <file>: Changes the owner and group of a file or directory.

6. Searching and Finding

  • find <directory> -name <filename>: Searches for files in a directory.
  • grep <pattern> <file>: Searches for a specific pattern in a file.
    • Example: grep "error" logs.txt finds all lines containing the word “error”.

7. Networking

  • ping <hostname>: Checks the connectivity to a host.
  • curl <url>: Fetches data from a URL.

8. Archiving and Compression

  • tar -cvf archive.tar <files>: Creates an archive from files.
  • tar -xvf archive.tar: Extracts files from an archive.
  • gzip <file>: Compresses a file.
  • gunzip <file>: Decompresses a file.

9. Miscellaneous

  • echo <text>: Displays text on the terminal.
  • history: Displays the list of previously executed commands.
  • clear: Clears the terminal screen.

Daily Workflow Tips

  1. Navigation: Use pwd and ls to understand your current context.
  2. File Management: Create, move, or delete files with touch, mv, cp, and rm.
  3. File Editing: Use nano or vim to edit files directly in the terminal.


Interview angle

  • “How do you find what’s consuming disk or CPU on a server?” - df -h then du -sh * descending into the largest directory for disk; top or htop for CPU, and ps aux --sort=-%mem for memory. For a file deleted but still held open, lsof | grep deleted explains why space didn’t return.
  • “How do you inspect a running process?” - ps, then lsof -p for its open files and sockets, strace for syscalls, and /proc/<pid>/ for everything else. ss -tulpn maps listening ports to processes.
  • “What should every non-trivial script start with?” - set -euo pipefail: exit on error, fail on unset variables, and propagate failures through pipes. Without it a script continues confidently after a failed command.