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.
104 readers like this.
Filing cabinet for organization

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

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

# 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 " "

## set variables


## parse options

while [ True ]; do
if [ "$1" = "--help" -o "$1" = "-h" ]; then
elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then
    shift 1
elif [ "$1" = "--path" -o "$1" = "-p" ]; then
    shift 2

## sanitize input filenames
## create array, retain spaces

ARG=( "${@}" ) 
set -e

## catch obvious input error

if [ "X$ARG" = "X" ]; then

## perform search

for query in ${ARG[*]}; do
    /usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*"
What to read next
User profile image.
Klaatu is a Unix geek and podcaster for Hacker Public Radio and GNU World Order.


When I've forgotten the name of the file I've downloaded I do

cd Downloads
ls -rt

It is generally the file at bottom of the list

great stuff!

Maybe more color can be added with a grep:

/usr/bin/find "${PATH}" "${MATCH}" "*${ARG}*" | /usr/bin/grep --color $ARG

This highlight the term searched...


I don't know why people use 'find' so much. There are always better ways most of the time. For that simple search, you can use your shell glob. $ ls *foo*
If you enable starglob (shopt -s starglob) and want to search sub dirs, $ ls **/*foo* or hidden subdirs $ ls .**/*foo*

I find this script useful:
- It saves keystrokes
- it search in subfolders
- it shows the path where the file was find

In reply to by castaway (not verified)

I'd advise against using PATH as a variable name in any script, as that already is an internal shell variable.

By overriding PATH you are forced to use commands' full path in your script, e.g.: '/usr/bin/find' instead fo simply 'find'.


Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.