During creating a project, I create a lot of short programs,
for testing some algorithms or functions. Very often I need these short files for reusing.
But I have more then 20000 files and subdirectories in my home directory!
How can I find out location of some needed files? Easy.
First at all. I never called file like f1, f234 ..., but single_list.c, ostream_iter.cpp, format.pl.
In this case I have a big chance to know about file without opening it. And ...
1. I created usual text file ( in my case it was $HOME/lib/IndexFile ).
2. I created bash script "indexfile" ( source is below ) and put it into ~/bin directory.
3. As all programmers, I am lazy, but if I create a file with useful information,
I add name and description of the file to my "IndexFile" using "indexfile" script.
4. Now indexfile -p qsort and I get list of file(s) which name or description contains "qsort" pattern.
5. indexfile -o filename and I open file(s) using Vim even without thinking where the file is located.
indexfile -h
************************************************************
USAGE: indexfile -[spodsh] pattern or filename
-c filename - create new record in IndexFile.
If file is not inside of current
directory, print full path.
-p pattern - print all suited records.
-o filename - open all suited files by vim editor.
-d pattern - delete a record from IndexFile.
-s - substitute description.
-h - to see this help message.
************************************************************
#!/bin/bash
##########################################
# Script allows to create and manipulate #
# the data from files by "keyword" #
##########################################
BADARG=67
filename="$2"
pattern="$2"
indexfile="$HOME/lib/IndexFile"
wh="^[[1;37m"
en="^[[0m"
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
usage()
{
echo
echo "$wh************************************************************$en";
echo "^[[1;41;37mUSAGE:^[[0m `basename $0` -[cpodsh] pattern or filename" >&2;
echo -e "\t\t$wh-c filename$en - create new record in IndexFile."
echo -e "\t If file is not inside of current"
echo -e "\t directory, print full path."
echo -e "\t\t$wh-p pattern$en - print all suited records."
echo -e "\t\t$wh-o filename$en - open all suited files by vim editor."
echo -e "\t\t$wh-d pattern$en - delete a record from IndexFile."
echo -e "\t\t$wh-s$en - substitute description."
echo -e "\t\t$wh-h$en - to see this help message."
echo "$wh************************************************************$en";
echo
exit $BADARG;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
create_new_record()
{
if [ -f "$filename" ]
then
num_fields=`echo $filename | awk -F/ '{print NF}'`
if [ "$num_fields" -ne "1" ]; then
file_name=$filename;
else
file_name="$PWD/$filename";
fi
echo
grep -w -n "$filename" "$indexfile" >&2;
if [ "$?" -eq "0" ]; then
echo "$wh This FILE exists !$en";
fi
echo "$wh****************************$en";
echo "Enter short$wh description$en";
echo -n " ( 0 - to quit ) : ";
read desc;
if [ "$desc" = "0" ]; then
exit 1;
fi
lis=$(echo $desc | sed "s/\033//g");
description=$(echo $lis | sed "s;[\<>/:^\'\"]; ;g");
echo "$filename:$description" >> $indexfile;
sort $indexfile -o $indexfile;
else
echo "***********************";
echo "Enter correct$wh FILENAME!$en" >&2;
exit 1;
fi
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
print_pattern()
{
if [ -z "$pattern" ]
then
echo
echo "**************************";
echo "Enter$wh PATERN$en for searching";
exit 1;
else
echo
count=`grep -i -s -c "$pattern" "$indexfile"`;
if [ $count -eq "0" ]
then
echo "**************************";
echo "There is no such$wh PATTERN$en";
exit 1;
else
grep -i "$pattern" "$indexfile" | awk -F: '{printf"%s\n\t\t%s\n",$1,$2}'>&2;
fi
fi
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
open_all_files()
{
if [ -z "$pattern" ]
then
echo "**************************";
echo "Enter$wh PATERN$en for searching";
exit 1;
else
files=`cat "$indexfile" | cut -d: -f1 | grep -i "$pattern"`;
if [ "$files" = "" ]
then
echo "**************************";
echo "There is no such$wh FILE$en(s)";
exit 1;
else vim $files;
fi
fi
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
delete_record()
{
if [ -z "$pattern" ]
then
echo "**************************";
echo "Enter$wh PATERN$en for searching";
exit 1;
else
grep -n -i "$pattern" "$indexfile" >&2;
if [ "$?" -eq "1" ]
then
echo "**************************";
echo "There is no such$wh PATTERN$en";
exit 1;
else
echo -n "$wh DELETE!$en (y/n) : ";
read answer;
case $answer in
y|Y ) sed -e "/\<$pattern\>/d" $indexfile > temp.$$;
cp -f temp.$$ "$indexfile"; rm -f temp.$$;;
* ) exit 0;;
esac
fi
fi
}}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
substitute_descript()
}{
echo "**************************";
echo -n "Enter$wh PATERN$en for searching : ";
read patt;
if [ -z "$patt" ]
then
echo "**************************";
echo "Enter$wh PATERN$en for searching";
exit 1;
else
number=`grep -c "$patt" "$indexfile"`
grep "$patt" "$indexfile";
if [ "$?" -eq "1" ]
then
echo "**************************";
echo "There is no such$wh PATTERN$en";
exit 1;
else
echo
echo "If there are more than one line,"
echo "press $wh(1-9)$en to repeat the search."
echo -n "Otherwise - press$wh 0$en ";
read answer;
if [ "$answer" != "0" ]; then
substitute_descript;
fi
if [ "$number" -gt "1" ]; then
echo "You can't substitute multiple line";
substitute_descript;
fi
echo
echo "***************************";
echo -n "Enter$wh LINE$en for substitution : "
read lin
if [ -z "$lin" ]; then
echo "You should print some text";
substitute_descript;
fi
li=$(echo "$lin" | sed "s/\033//g");
line=$(echo "$li" | sed "s/[\<>/:^\'\"]/ /g");
echo "$line";
first=`grep -w "$patt" "$indexfile" | awk -F: '{print $2}'`
sed -e "s;\<$first\>;$line;" $indexfile > temp.$$;
cp -f temp.$$ "$indexfile" >&2;
rm -f temp.$$ >&2;
echo
echo "New record :"
new_record=`grep -w "$line" "$indexfile"` >&2;
echo "$wh$new_record$en";
exit 0;
fi
fi
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
###########################
## MAIN PART ##
###########################
if [ "$#" -lt 1 -o "$#" -gt 2 ]; then
usage;
fi
first_char=`echo $1 | cut -c1`
if [ "$#" -eq "1" -a "$first_char" != "-" ]; then
usage;
fi
while getopts "c:p:o:d:h:s" OPTION
do
case $OPTION in
c ) create_new_record;;
p ) print_pattern;;
o ) open_all_files;;
d ) delete_record;;
s ) substitute_descript;;
h ) usage;;
* ) usage;;
esac
done
echo "$wh Done$en";
exit 0;
Now I use "datafile" program. It is C-language code that retrives information from MySQL table.
Program "indexgui.pl" is Perl script and works in same way but using GUI.