keepcalculator

06 Numbers and Powers

Handy Command Line Tips for Everyday Users

Card No. 06·52012 · Updated · Standard formula, shown below

This article covers practical command line tips that make everyday terminal work faster and less error-prone, from navigation and file management to process control and customization. You’ll learn concrete commands you can type right away, along with the reasoning behind them, so you can adapt them to your own workflow.

Why the Command Line Still Matters

Graphical interfaces are great for discovery, but the command line excels at speed, automation, and precision. A single command can rename hundreds of files, find text across thousands of documents, or repeat a task with slightly different parameters. Once you learn a few core patterns, you can combine them like building blocks to solve problems that would take many clicks in a GUI. Moreover, the command line is universally present on Unix-like systems (Linux, macOS) and available on Windows through PowerShell or Windows Subsystem for Linux, making it a transferable skill.

Navigation and File Management Shortcuts

Moving around and managing files are the most frequent tasks. Here are the tips that save the most keystrokes:

  • Use cd - to go back to the previous directory. Instead of typing a long path again, this toggles between your current and last directory. It’s especially handy when you’re alternating between two folders.
  • Leverage !! to repeat the last command. Type !! and press Enter to run it again. Add a prefix like sudo !! to rerun the previous command with elevated privileges (on Unix-like systems).
  • Create multiple directories at once with mkdir -p. The -p flag creates parent directories as needed and doesn’t error if they already exist. For example, mkdir -p projects/2025/reports creates all three levels in one go.
  • Move or rename files with mv and get a confirmation. Use mv -i to be prompted before overwriting an existing file. This prevents accidental data loss, especially when using wildcards.
  • Copy directories recursively with cp -r. To copy a folder and its contents, you must specify -r (recursive). Without it, cp will skip directories.

Searching and Finding Things Quickly

Finding files and text is a daily need. The command line offers powerful tools that are often faster than GUI search.

  • Use find to locate files by name, type, or date. For example, find . -name "*.pdf" lists all PDF files in the current directory and subdirectories. Add -mtime -7 to find files modified in the last 7 days.
  • Search inside files with grep. grep -r "error" . recursively searches all files in the current directory for the string “error”. Add -i for case-insensitive, -n to show line numbers, and --color to highlight matches.
  • Use history to recall past commands. Type history to see a numbered list. Then use !number to rerun a specific command. You can also press Ctrl+R to search interactively through your history.
  • Combine find with -exec to act on results. For instance, find . -name "*.tmp" -delete removes all temporary files. Be careful with -delete; test with -print first.

Process and System Monitoring

Knowing what’s running and how to control it is crucial for troubleshooting and resource management.

  • List processes with ps or top. ps aux shows all running processes with details like CPU and memory usage. top (or htop if installed) gives a real-time, interactive view.
  • Kill a process by PID or name. Use kill 1234 to terminate process ID 1234. If it doesn’t respond, try kill -9 1234 (force kill). To kill by name, use pkill firefox.
  • Run a command in the background with &. Append & to a command to run it in the background and get your prompt back. For example, python server.py &. Use jobs to list background jobs and fg to bring one to the foreground.
  • Use Ctrl+Z to suspend a foreground process. Then use bg to resume it in the background or fg to resume in the foreground. This is handy when you accidentally start a long-running task and need your shell back.

Text Processing and Piping

The true power of the command line comes from chaining commands with pipes (|). This lets you transform and analyze text without opening a file.

  • Count words, lines, and characters with wc. wc -l file.txt counts lines. Combine with cat or grep to get counts of filtered output.
  • Sort and remove duplicates with sort and uniq. sort file.txt | uniq -c gives a count of each unique line. This is useful for analyzing logs.
  • Extract columns with awk. For example, awk '{print $1}' data.txt prints the first field of each line. awk is a full programming language, but even basic usage is powerful.
  • Replace text with sed. sed 's/old/new/g' file.txt replaces all occurrences of “old” with “new” and prints the result to stdout (doesn’t modify the file unless you use -i).

Customizing Your Shell

Your shell is your main interface; customizing it can save time and reduce typos.

  • Create aliases for long or frequent commands. In your .bashrc or .zshrc, add lines like alias ll='ls -alF' or alias gs='git status'. After saving, run source ~/.bashrc to apply changes.
  • Set a useful prompt. The PS1 variable controls your prompt. For example, export PS1='\u@\h:\w\$ ' shows username, hostname, and current directory. Many prefer to include the git branch when in a repository.
  • Use command-line history expansion. Besides !!, you can use !$ to refer to the last argument of the previous command. For instance, mkdir newfolder then cd !$ moves into that folder.
  • Install a shell like Zsh with Oh My Zsh (on Unix-like systems). This provides themes, plugins, and better autocompletion. It’s a popular choice, but not required; Bash is perfectly capable.

Common Pitfalls and How to Avoid Them

Even experienced users make mistakes. Here are the most common and how to prevent them.

  • Using rm without caution. There is no trash bin in the command line; deleted files are gone. Always double-check paths, use -i for confirmation, and consider using a safer alternative like trash if available.
  • Running commands as root when not needed. Avoid sudo for daily tasks. A typo in a root command can damage the system. Use sudo only for specific administrative actions.
  • Forgetting to quote paths with spaces. Always enclose paths containing spaces in quotes, e.g., cd "My Documents". Otherwise, the shell splits them into multiple arguments.
  • Mixing up relative and absolute paths. A relative path is relative to your current directory. Use pwd to see where you are. Absolute paths start with / (or C:\ on Windows).
  • Not reading error messages. The command line usually tells you exactly what went wrong. Read the message carefully; it often includes the correct syntax or a hint.

Frequently Asked Questions

How do I undo a command line mistake?

If you accidentally delete a file with rm, there is no built-in undo. Your best bet is to restore from a backup or version control. For commands that modify files (like cp or mv), using the -i flag (interactive) prompts you before overwriting, which can prevent mistakes. Also, consider using trash instead of rm if available, as it moves files to a trash folder.

What is the difference between a shell and a terminal?

A terminal is the graphical window that displays text and accepts input. The shell is the program that runs inside the terminal, interpreting your commands. Common shells include Bash, Zsh, and Fish. On Windows, PowerShell is a shell, and the Terminal app is the graphical host.

How do I learn more about a command?

Use the built-in manual pages. Type man command (e.g., man ls) to see the manual. For a quick summary of options, use command --help or command -h. Online resources like Stack Overflow and official documentation are also invaluable.

Why does my command say “command not found”?

This means the shell cannot find an executable with that name in your PATH. Check the spelling, ensure the software is installed, or provide the full path (e.g., ./script.sh for a script in the current directory). You can also add a directory to your PATH in your shell configuration file.

Can I use command line on Windows?

Yes. Windows has Command Prompt (cmd) and PowerShell. PowerShell is more powerful and uses a different syntax (e.g., Get-ChildItem instead of ls). You can also install Windows Subsystem for Linux (WSL) to run a full Linux environment, including Bash, directly on Windows.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →