UpdateDB and Locate Scripts

From OESF

Revision as of 18:06, 24 November 2006 by Phil284 (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

I wrote this because porting the full version of GNU findutils with locate and updatedb didn't make since. On a normal Unix box it takes a lot of space and uses a lot of resources to make the DB. I decided to just make a Text DB and wrap egrep to get what I wanted. Works well I think and does what I need while full filling the 'tiny' is better motto of the Z.

Both of these scripts are written in bourne shell and uses busyboxes 'find' (which is greatly lacking but suffices) and '/bin/egrep' from the Z rom.


  • updatedb.sh --

Creates the text database and config file to point at it needed by locate.sh.


  • locate.sh --

Reads the config file created by updatedb and then wraps egrep to search the database for the string or substring needed. All occurences are displayed.


Install:


Copy 'updatedb.sh' and 'locate.sh' into your path. Name them what you like or drop the .sh if it is annoying. 'updatedb.sh' will create two files. One is a file called 'locate_conf' in the directory /var/log. The other file is the locatedb which is put on internal RAM by default but can be placed on CF or SD cards with a command line option.

Both files are created everytime 'updatedb.sh' is run. You can run updatedb from cron to keep your files database up to date.



Usage:


1) First run updatedb.sh

Usage: updatedb.sh [[-rcsh]

       -r = store DB on internal RAM. (Default)
       -c = store DB on CF Card.
       -s = store DB on SD Card.
       e.g.

updatedb.sh -s

           updatedb.sh :: Using SD card for DB Storage.
           updatedb.sh :: Removing old Locate Database.
           updatedb.sh :: --> /mnt/card/locatedb_dir/locate.db
           updatedb.sh :: Creating new Locate Database
           updatedb.sh :: --> /mnt/card/locatedb_dir/locate.db
           updatedb.sh :: Created config file (./locate.conf).
           updatedb.sh :: Done.

2) Now you can run locate.sh

       Usage: locate.sh <string>
       e.g.
            locate.sh test
            /home/root/usr/bin/test
            /home/!QtPalmtop/apps/Jeode/primtest.desktop
            /home/!QtPalmtop/bin/runprimtest
            /home/!QtPalmtop/pics/primtest_icon.png
            /usr/bin.rom/test
            /usr/mnt.rom/card/python-src/test.py
            /usr/!QtPalmtop.rom/bin/runprimtest
            /usr/!QtPalmtop.rom/pics/primtest_icon.png

BEGIN updatedb.sh
#!/bin/sh
# updatedb.sh - create database of path/files for
#               use with locate.sh.
#
# $Id: updatedb.sh,v 1.2 2002/03/17 02:44:36 jmurff Exp unknown $
# $Author: jmurff $
#
# $Log: updatedb.sh,v $
# Revision 1.2  2002/03/17 02:44:36  jmurff
# Moved locate.conf to /var/log.
#
# Revision 1.1  2002/03/17 00:13:20  jmurff
# Initial revision
#
#
#======================================
ldir_name=locatedb_dir
locate_conf="/var/log/locate.conf"
myname=`basename $0`
lpath="/home/root/$ldir_name"    # Main RAM Card Default
echo " "
# select output file via command option
while getopts rcs option
do
  case "$option"
  in
     r)
       lpath="/home/root/$ldir_name"    # Main RAM Card
       echo "$myname :: Using internal RAM for DB Storage."
       ;;
     c)
       lpath="/mnt/cf/$ldir_name"       # CF Card
       echo "$myname :: Using CF Card for DB storage."
       ;;
     s)
       lpath="/mnt/card/$ldir_name"     # SD/MMC Card
       echo "$myname :: Using SD card for DB Storage."
       ;;
     \?)
       echo "Usage: $myname  [[-rcsh]"
       echo "	-r = store DB on internal RAM."
       echo "	-c = store DB on CF Card."
       echo "	-s = store DB on SD Card."
       echo " "
       exit 1
       ;;
  esac
done
if [[ ! -d $lpath ]; then
  echo "$myname :: Making directory for Locate Database."
  echo "$myname :: --> $lpath"
  mkdir $lpath
  if [[ $? -ne 0 ]; then
     echo "$myname :: Error creating $lpath"
     echo " "
     exit 1
  fi
fi
locate_db="$lpath/locate.db"
if [[ -e $locate_db ]; then
  echo "$myname :: Removing old Locate Database."
  echo "$myname :: --> $locate_db"
  rm -rf $locate_db
  if [[ $? -ne 0 ]; then
     echo "$myname :: Error removing $locate_db"
     echo " "
     exit 1
  fi
fi
# Create Database for Locate
echo "$myname :: Creating new Locate Database"
echo "$myname :: --> $locate_db"
# Busybox find is really brain dead...
# Find works but gives error sometimes on /procs...
find / -print > $locate_db 2>/dev/null
# If you are worried about space use this command uinstead of the
# one above to keep the file GZIP-ed (Requires change in locate.sh
#find / -print|gzip  > $locate_db 2>/dev/null
# If you have a good find use this ::
# find / -path '/proc' -prune -o -print > $locate_db 2>/dev/null
#if [[ $? -ne 0 ]; then
#     echo "$myname :: Error creating $locate_db"
#     echo " "
#     exit 1
#fi
# Make file needed for locate.
if [[ -e $locate_conf ]; then
  echo "$myname :: Removing old locate config file."
  echo "$myname :; --> $locate_conf"
  rm -rf $locate_conf
  if [[ $? -ne 0 ]; then
     echo "$myname :: Error removing $locate_conf"
     echo " "
     exit 1
  fi
fi
echo locate_db=$locate_db > $locate_conf
echo "$myname :: Created config file ($locate_conf)."
echo "$myname :: Done."
echo " "
exit 0

END updatedb.sh


BEGIN locate.sh
#!/bin/sh
# locate.sh - used to read database and find
#             occurance of files based on a
#             search string.
# $Id: locate.sh,v 1.2 2002/03/17 02:44:36 jmurff Exp unknown $
# $Author: jmurff $
#
# $Log: locate.sh,v $
# Revision 1.2  2002/03/17 02:44:36  jmurff
# Moved locate.conf to /var/log.
#
# Revision 1.1  2002/03/17 00:13:20  jmurff
# Initial revision
#
#
#--------------------------------------
locate_conf="/var/log/locate.conf"
myname=`basename $0`
if [[ -e $locate_conf ] ; then
   . $locate_conf
else
   echo " "
   echo "$myname :: Can't find my configuration file:"
   echo "$myname :: '$locate_conf'."
   echo "$myname :: Make sure you ran/run 'updatedb.sh'"
   echo " "
   exit 1
 fi
# DO the easy part...
# If you used GZIP in UpdateDB use ZCAT here.
#zcat $locate_db|egrep -i $1
egrep $1 $locate_db
exit $?

END locate.sh

Personal tools