9 Powerful Bash Tricks to Boost Your Command‑Line Productivity
This article shares nine practical Bash techniques—including inserting text at the top of a file, appending multiple lines, recursive search‑replace, creating temporary Vim files, advanced curl usage, Bashmarks, column extraction with awk, skipping words, and building custom command packages—each illustrated with ready‑to‑use code snippets.
I enjoy exploring the Bash environment and often encounter repetitive problems that require the same solutions; to avoid rewriting these solutions each time, I created a set of reusable functions and added them to my .bashrc file.
Tip 1 – Insert a line at the top of a file
Use sed to prepend a line:
sed -i '1s/^/line to insert
/' path/to/file/you/want/to/change.txtTip 2 – Append multiple lines to a configuration file
Employ a here‑document (EOF) to insert a block of text:
cat >> path/to/file/to/append-to.txt << "EOF"
export PATH=$HOME/jdk1.8.0_31/bin:$PATHexport JAVA_HOME=$HOME/jdk1.8.0_31/
EOFTip 3 – Recursive search and replace across a directory
Combine find with sed (OS X syntax shown):
# OSX version
find . -type f -name '*.txt' -exec sed -i '' s/this/that/g {} +A reusable Bash function can simplify this:
function sr {
find . -type f -exec sed -i '' s/$1/$2/g {} +
}Usage example:
sr wrong_word correct_wordTip 4 – Open a temporary file in Vim/Dropbox
Create temporary files with random names using openssl:
function sc {
gvim ~/Dropbox/$(openssl rand -base64 10 | tr -dc 'a-zA-Z').txt
}
function scratch {
gvim ~/Dropbox/$(openssl rand -base64 10 | tr -dc 'a-zA-Z').txt
}Running sc or scratch opens a new Vim window with a random‑named file.
Tip 5 – Download files with curl handling redirects and SSL issues
Fetch a page and follow redirects, ignoring SSL errors: curl -Lks <some-url> Download a file (e.g., a tarball) similarly:
curl -OLks <some-url/to/a/file.tar.gz>Tip 6 – Bashmarks for quick directory navigation
Use bashmarks to bookmark frequently visited directories and jump back to them with a simple command.
Tip 7 – Extract a specific column from formatted output (awk)
Example extracting the second column from git status -s output:
# Sample output of git status -s command:
M .bashrc
?? .vim/bundle/extempore/
# Remove status code and keep file names
git status -s | awk '{print $2}'A reusable function:
function col {
awk -v col=$1 '{print $col}'
}Usage:
git status -s | col 2Tip 8 – Skip the first X words using xargs
Define a helper to drop the first N fields:
function skip {
n=$(( $1 + 1 ))
cut -d' ' -f${n}-
}Example processing docker images output to obtain only image IDs:
docker images | col 3 | xargs | skip 1Tip 9 – Build your own command package
You can create custom Bash commands; for instance, a shortcut to copy an SSH key to any server: dur key user@somehost Finally, try adding these functions to your own .bashrc or create a new one, and feel free to share additional tips in the comments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
