Fundamentals 10 min read

Ten Bash Productivity Tips for Command-Line Efficiency

This article presents ten practical Bash tips that boost command‑line productivity, covering inserting text at the top of files, appending multi‑line blocks, recursive search‑and‑replace, creating temporary Vim files, using curl for downloads, managing bookmarks, extracting columns with awk, trimming output, and building custom command packages.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Ten Bash Productivity Tips for Command-Line Efficiency

I enjoy exploring the Bash environment, and after repeatedly solving the same problems I created a set of reusable functions and added them to my .bashrc to improve command‑line efficiency.

Tip 1 – Add a line to the top of a file

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

Tip 2 – Insert multiple lines into a configuration file

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

Tip 3 – Recursive search and replace in a directory

Using find together with sed you can replace text across many files:

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

I wrapped this into a function:

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

Usage: sr wrong_word correct_word Tip 4 – Open a temporary file in Vim/Dropbox

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 randomly‑named temporary file.

Tip 5 – Download files with curl handling redirects and SSL issues

curl -Lks <some-url>          # download page to terminal
curl -OLks <some-url/to/a/file.tar.gz>  # download a file

Tip 6 – Bashmarks for directory shortcuts

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

Tip 7 – Extract a column from formatted output (awk)

function col {
  awk -v col=$1 '{print $col}'
}

Example: git status -s | col 2 prints only the file names.

Tip 8 – Skip the first N words of a line

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

Used together with other commands, e.g., docker images | col 3 | xargs | skip 1 to remove the header.

Tip 9 – Create a custom command package

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

These functions illustrate how Bash can be extended to automate common development tasks.

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.

AutomationproductivityBashscriptscommand-line
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

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.