Here's a scenario: Whenever I need some source code or a bundle of art assets or a game from the internet, I download it to my ~/Downloads directory, navigate to the folder, and promptly realize I forgot the file name. It's not that I don't remember what I downloaded; it's the proliferation of file types that throws me off. Was it a tarball or a ZIP file? What was the version number? Have I downloaded a copy before?
Or maybe I know I created a file, but days later I just can't remember the full file name. Maybe I remember a string in the file name, but not the exact arrangement of words.
In short, there are too many variables for me to confidently issue a command without listing the contents of a directory and grepping for some substring of the filename.
To make this process easy, I keep a command I call lf in my ~/bin directory. It's a simple frontend to the popular find or locate command, but with less typing and far less functionality.
For example, to find a file located in the current directory that contains the string foo in the file name:
$ lf foo
/home/klaatu/foo.txt
/home/klaatu/goodfood.list
/home/klaatu/tomfoolery.jpg
To find a file located in another directory with the string foo in the file name:
$ lf --path ~/path/to/dir foo
It's purely a lazy tool, and as I am very lazy, it's one I use frequently.
The script
#!/bin/sh
# lazy find
# GNU All-Permissive License
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
## help function
function helpu {
echo " "
echo "Fuzzy search for filename."
echo "$0 [--match-case|--path] filename"
echo " "
exit
}
## set variables
MATCH="-iname"
SEARCH="."
## parse options
while [ True ]; do
if [ "$1" = "--help" -o "$1" = "-h" ]; then
helpu
elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then
MATCH="-name"
shift 1
elif [ "$1" = "--path" -o "$1" = "-p" ]; then
SEARCH="${2}"
shift 2
else
break
fi
done
## sanitize input filenames
## create array, retain spaces
ARG=( "${@}" )
set -e
## catch obvious input error
if [ "X$ARG" = "X" ]; then
helpu
fi
## perform search
for query in ${ARG[*]}; do
/usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*"
done
6 Comments