Bash Terminal Commands - WittyWriter

Bash Terminal Commands

📘 Key Concepts and Definitions

🧮 Command Structure

Bash commands typically follow this pattern:

command [options] [arguments]

Example: grep -i "error" /var/log/syslog

🛠️ Core Commands Reference

File and Directory Navigation

CommandDescription
lsLists directory contents. (Use ls -la for detailed, hidden files).
cd [dir]Changes the current directory. (cd .. moves up one level).
pwdPrints the current working directory.

File and Directory Management

CommandDescription
touch [file]Creates a new, empty file.
mkdir [dir]Creates a new directory.
cp [source] [dest]Copies files or directories. (Use cp -r for directories).
mv [source] [dest]Moves or renames files or directories.
rm [file]Removes a file. (Use rm -r for directories; use with caution!).

Viewing and Searching Files

CommandDescription
cat [file]Prints the entire content of a file to the screen.
less [file]Views file content one page at a time (allows scrolling).
head / tail [file]Views the first / last 10 lines of a file. (Use tail -f to follow a file live).
grep [pattern] [file]Searches for a pattern within a file.
find [path] -name [pattern]Searches for files and directories in a directory hierarchy.

System and Process Management

CommandDescription
ps auxLists currently running processes.
kill [pid]Sends a signal to terminate a process by its ID.
df -hDisplays disk space usage in a human-readable format.
chmodChanges the permissions of a file or directory.

🧭 Common Workflows with Piping

Piping (|) is the key to combining simple tools to perform complex tasks.

  1. Find large files: Find all files in the current directory, get their details, sort by size, and show the top 5.
    find . -type f -exec ls -lh {} + | sort -rh -k 5 | head -n 5
  2. Search command history: Find a command you ran previously that involved copying a file.
    history | grep "cp"
  3. Count files in a directory: List all files (one per line) and then count the lines.
    ls -1 | wc -l

⌨️ Productivity Tips & Shortcuts

📊 Piping and Redirection Explained

OperatorNameExampleDescription
|Pipels -l | lessSends the output of ls -l as the input to less.
>Redirect Outputls > file_list.txtWrites the output of ls to a file, overwriting it if it exists.
>>Append Outputecho "Error log" >> app.logAppends the output to the end of a file.
<Redirect Inputsort < names.txtSends the content of names.txt as input to the sort command.

🧪 Practical One-Liner Examples

Find and Delete All .tmp files

find . -name "*.tmp" -type f -delete

Monitor a Log File for Errors in Real-Time

tail -f /var/log/nginx/access.log | grep " 404 "

Create a Quick Backup of a File

cp config.yaml{,.bak}
# Expands to: cp config.yaml config.yaml.bak

🧹 Troubleshooting Common Issues

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more