Operations 9 min read

Boost Your Productivity with Essential Linux Shell Tricks

This article presents a collection of practical Linux shell techniques—including brace expansion, directory shortcuts, command history shortcuts, and useful built‑in variables—to streamline everyday command‑line tasks, reduce typing, and automate repetitive operations for faster, more efficient workflow.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Boost Your Productivity with Essential Linux Shell Tricks

Linux shells offer many concise features that can dramatically improve productivity. Below are several practical tricks, each illustrated with clear examples and ready‑to‑copy commands.

Brace Expansion for Similar Filenames

Enclose comma‑separated strings in braces to generate multiple filenames automatically. Note that spaces are not allowed inside the braces.

$ echo {one,two,three}file
onefile twofile threefile
$ echo {one,two,three}{1,2,3}
one1 one2 one3 two1 two2 two3 three1 three2 three3

This is especially handy for extending arguments of commands such as cp, mv, or rm:

$ cp /very/long/path/file{,.bak}
# creates a backup file named file.bak
$ rm file{1,3,5}.txt
# removes file1.txt file3.txt file5.txt
$ mv *.{c,cpp} src/
# moves all .c and .cpp files into the src directory

Directory Shortcuts

Use cd - to return to the previous directory, and set the CDPATH variable to search a list of directories automatically when a relative cd fails.

$ pwd
/very/long/path
$ cd
/home/labuladong
$ cd -
/very/long/path

Example of configuring CDPATH:

$ export CDPATH='~:/var/log'
$ cd mysql   # searches ~ and /var/log, finds /var/log/mysql
$ pwd
/var/log/mysql

Repeating Commands Efficiently

The !! shortcut repeats the previous command, while Ctrl+R performs a reverse incremental search of the command history (bash only). For more precise retrieval, combine history with grep and execute a command by its history number using !<em>n</em>.

$ sudo !!
# repeats the previous command with sudo
$ history | grep 'config'
 7434  git config --global --unset https.proxy
$ !7434
git config --global --unset https.proxy

A convenient wrapper function can be added to .bashrc or .zshrc:

his(){
    history | grep "$@"
}
# Usage: his 'some_keyword'

Other Handy Shortcuts

Automatic "yes" input : pipe the output of yes to a command that expects interactive confirmation.

$ yes | apt install xxx
# automatically answers "y" to all prompts

Special variables : $? – exit status of the last command (0 for success, non‑zero for error). Useful in scripts to make decisions based on command outcomes. $$ – PID of the current shell process. Handy for creating unique temporary filenames.

Example using $? to conditionally add a footer to Markdown files only when a keyword is absent:

#!/bin/bash
filename=$1
# Check if the file already contains "下一篇"
 tail $filename | grep '下一篇'
 [ $? -ne 0 ] && { echo "Add footer"; }

These techniques together can save a lot of time when working in the Linux command line.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxproductivitycommand-lineTips
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.