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.shgives execute permission to the owner.
- Example:
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.txtfinds all lines containing the word “error”.
- Example:
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
- Navigation: Use
pwdandlsto understand your current context. - File Management: Create, move, or delete files with
touch,mv,cp, andrm. - File Editing: Use
nanoorvimto edit files directly in the terminal.
Interview angle
- “How do you find what’s consuming disk or CPU on a server?” -
df -hthendu -sh *descending into the largest directory for disk;toporhtopfor CPU, andps aux --sort=-%memfor memory. For a file deleted but still held open,lsof | grep deletedexplains why space didn’t return. - “How do you inspect a running process?” -
ps, thenlsof -pfor its open files and sockets,stracefor syscalls, and/proc/<pid>/for everything else.ss -tulpnmaps 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.