Operations 10 min read

Boost Your Linux Productivity: 9 Essential Bash Command-Line Tricks

This article compiles nine practical Bash techniques—from inserting text at the top of files and appending multiline blocks, to recursive search‑replace, temporary Vim files, secure downloads, Bashmarks, column extraction, word skipping, and custom command packages—each illustrated with ready‑to‑use code snippets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Boost Your Linux Productivity: 9 Essential Bash Command-Line Tricks

The author shares a collection of Bash command‑line tricks aimed at maximizing efficiency for Linux users, providing ready‑to‑use functions and code snippets that can be added to a .bashrc file.

Technique 1: Add Text to the Top of a File

Use sed to insert a line at the beginning of a file:

sed -i '1s/^/line to insert
/' path/to/file/you/want/to/change.txt

Technique 2: Insert Multiple Lines into a Configuration File

Employ a here‑document to append a block of text:

cat >> path/to/file/append-to.txt <<"EOF"
export PATH=$HOME/jdk1.8.0_31/bin:$PATH
export JAVA_HOME=$HOME/jdk1.8.0_31/
EOF

Technique 3: Recursive Search and Replace Across a Directory

Combine find with sed to replace text in all matching files:

# OSX version
find . -type f -name '*.txt' -exec sed -i '' s/this/that/g {} +

A reusable function added to .bashrc:

function sr {
    find . -type f -exec sed -i '' s/$1/$2/g {} +
}

Usage example:

sr wrong_word correct_word

Technique 4: Open a Temporary File in Vim/Dropbox

Two functions generate a random filename with openssl and open it in GVim:

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 }

Technique 5: Download Files with Curl (Follow Redirects, Ignore SSL Errors)

Simple commands for downloading a page or a file:

curl -Lks <some-url>
curl -OLks <some-url-to/a/file.tar.gz>

Technique 6: Bashmarks for Directory Bookmarking

Functions to save, jump to, and list directory bookmarks:

# Save current directory
function s { cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1; mv ~/.sdirs1 ~/.sdirs; echo "export DIR_$1=$PWD" >> ~/.sdirs; }
# Jump to bookmark
function g { source ~/.sdirs; cd $(eval echo \$(echo \$DIR_$1)); }
# List bookmarks
function l { source ~/.sdirs; env | grep "^DIR_" | cut -c5- | grep "^.*="; }

Technique 7: Extract a Column from Formatted Output (Awk Example)

Define a reusable function to print a chosen column: function col { awk -v col=$1 '{print $col}'; } Example usage with git status -s:

git status -s | col 2

Technique 8: Skip the First X Words Using a Custom Function

A function that removes the first x fields from input:

function skip { n=$(( $1 + 1 )); cut -d' ' -f$n-; }

Combined with other commands, e.g., to delete Docker images:

docker rmi $(docker images | col 3 | xargs | skip 1)

Technique 9: Create Your Own Command Package

A versatile dur function that supports cloning, moving, tracking branches, copying SSH keys, listing functions, showing definitions, and displaying help:

function dur {
  case $1 in
    cl|clone) git clone [email protected]:nicolapaolucci/$2.git;;
    mv|move) git remote add bitbucket [email protected]:nicolapaolucci/$(basename $(pwd)).git && git push --all bitbucket;;
    tr|trackall) for remote in $(git branch -r | grep -v master); do git checkout --track $remote; done;;
    k|key) ssh $2 "mkdir -p .ssh && cat >> .ssh/authorized_keys" < ~/.ssh/id_rsa.pub;;
    f|fun) typeset -F | col 3 | grep -v _ | xargs | fold -sw 60;;
    d|def) typeset -f $2;;
    h|help|*) echo "[dur]dn shell automation tools"; echo "commands available:"; echo " [cl]one, [mv|move]"; echo " [f]fun lists all bash functions defined in .bashrc"; echo " [def] <fun> shows definition of function defined in .bashrc"; echo " [k]ey <host> copies ssh key to target host"; echo " [tr]ackall, [h]elp";;
  esac
}

Feel free to try these snippets, adapt them to your own .bashrc, and share any improvements.

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.

Automationproductivitycommand-lineBashShell scripting
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.