Find a file the lazy way with this script | Opensource.com

Find a file the lazy way with this script

Can't remember which file you downloaded? Try the lf script for the easy way out.

Filing cabinet for organization
x

Subscribe now

Get the highlights in your inbox every week.

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
Digital hand surrounding by objects, bike, light bulb, graphs

These six Bash scripts will make your life easier when you're working with Git repositories.
Person using a laptop

Try this script the next time you give a presentation to prevent making typos in front of a live audience.

Topics

About the author

Klaatu - Klaatu is a Unix geek and podcaster for Hacker Public Radio and GNU World Order.