*NIX utilities

1) You are in a directory lets say "/home/xyz" and you want to find some file lets say "abc.txt" and open it using vi. We normally do

# find /home/xyz -name abc.txt
# vi /abc.txt

Simply you can do :
# find /home/xyz -name abc.txt -exec vi {} +

You can make an alias of above command and it in your profile file.

But if you have more then one abc.txt file and you want to open only one of them, you can use the following script.

# vi fvi

################################################################
#small script that finds the file given at command line and vi it
#needs improvement
if [ $# -ne 1 ]
then
echo "Usage: $0 "
exit 1
fi
result=`find . -name $1`
file=`echo $result | awk '{print $1}'`
if [ -n "$file" ]
then
vi $file
else
echo "$file not found"
fi
################################################################

How to use it
1) Copy the script to some folder where everybody can access it, like "/usr/bin/"
2) chmod 755 /usr/bin/fvi

Let say you want to find and vi abc.txt from /home/xyz
# cd /home/xyz
# fvi abc.txt

No comments:

Books I like

  • Inside C++ Object Model
  • Unix Network Programming - Stevens
  • Professional-c++-programmer-to-programmer - wrox
  • Beautiful-code-leading-programmers-explain-how-they-think-theory-in-practice