Handy Bash script to perform the most common operations -- Go to and Edit

in #bash5 years ago (edited)

I find that when I'm working on a server via ssh I keep performing two operations over and over: go to a directory, and edit a file.

When I need to perform either of these operations and I can't remember the location of the target directory or file, I usually use mlocate to find either. I then either cd there or type 'vi' -paste- the name of the directory I just copied using the cursor.

I decided it was time to save some time and write a script which will speed up this process. I call it 'seek.sh'. It requires the mlocate binary to be installed (on Centos, use: yum install mlocate).

Here is the script:

===

#!/bin/sh

# seek.sh script by thomas toscani
# version .1 dated 2019-0102

### NOTE:
# If you want to have the script change the current working directory in the CURRENT terminal
# then you will need to set up an alias in .bashrc like this:
# alias myseek="source /path/to/script"
# And then eliminate the 'exec bash' command at the end of this file
# The alias will then source the script and will allow it to change directories

echo

N=0
for i in $(locate $1) ; do

      choicesarray[$N]="$i"
      echo "$N = $i"     #to confirm the entry

  let "N= $N + 1"
done

echo

echo -n "(e)dit or (g)oto:  "
read -n1 operation
echo
if [[ -z "$operation" ]] ; then
        exit 0
fi
echo -n "Which file?: "
read choice
echo

if [[ $operation == "e" ]] ; then
        /bin/vim ${choicesarray[$choice]}
else
        DIR=$(dirname "${choicesarray[$choice]}")
        echo "Opening $DIR in new shell.  Use Control-D when finished.  You are at Shell level: $(echo $SHLVL)"
        cd $DIR
        exec bash
fi
echo
echo

===

You use the script in the following way.

First put 'seek.sh' somewhere in your $PATH.

Next, type 'seek [filename or directory name]' for the file or directory you want to edit or go to.

Seek will use mlocate to find all instances of that name in the file system. It will then present you with an enumerated list of matches.

It then asks you whether you want to (e)dit or (g)oto the file or directory. Press 'e' to edit the file or 'g' to go to directory containing the file. Press either 'e' or 'g' without pressing return.

It will then ask you "Which file?:". Select the file from the list by typing in the index of the file or directory. You'll need to press return this time since there can be a two-digit index.

That's it! Seek will now either use /bin/vim to edit the file, or go to the parent directory of the file using cd.

Important note: As you can see in the comments of the file, bash won't normally let you change directories in the terminal from which you called the script. Any cd command pertains to the environment in which the script is run. So in this case, we spawn a new bash terminal with the new directory.

However, there is a fix for this. You can change the current directory of the current terminal by setting up an alias to the seek.sh script, and have the target for this alias be: "source /path/to/script", where 'source' is literal.

I hope you find this useful and it saves you many keystrokes!

Sort:  

Congratulations @thomastoscani! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You distributed more than 10 upvotes. Your next target is to reach 50 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98923.04
ETH 3381.66
USDT 1.00
SBD 3.09