Optimize for happiness

Life is easier with simple scripting. Grateful for everyone's encouragement and support!

View this project on GitHub

Search, search and search again!

2023.06.16   •   4 min read

I sometimes need to check everywhere for, let’s say, remnants of a deleted program (all sorts of preference files and anything else). It’s time to simplify the whole system search with a quick shell script.

Jumping right to it, here is a script to search everywhere:

To use it, basically place it anywhere you like and in the Terminal first let it be an executable (+x) and then use it.

$ chmod +x find-all.sh
    # The above command makes the script executable, 
    # so the following command can be run:
$ ./find-all.sh something

Searching everywhere for "*something*" (case-insensitive) ...

    # found files get listed here
$

To this script, it does not matter if you capitalized anything, it will find it. Letter case does not matter.

If you find it useful and want to make it more convenient, you could move the script into /usr/local/bin/ directory.

For an even easier use, I renamed it to just find-all (from find-all.sh) - just a little shorter. Now I can happen to be in any directory on the computer and still search everywhere for files and directories that match the symbols I give it.

Search somewhere in particular

A similar search script is below. For when we don’t need to search everywhere. Let’s specify where to look:

$ find-here.sh ~ index.ht

Searching in '/Users/<my-user>' for "*index.ht*" (case-insensitive) ...

# a bunch of files matching that search string
$

Installation is identical as the script above. Place it anywhere, but a convenient and consistent location is to keep such scripts in /usr/local/bin/.

What if I want to search for something inside the files?

By now, you get the idea for these simple scripts and can easily modify this command into a script, or refer to it here:

$ grep -lir verity /usr/local/bin/*
/usr/local/bin/find-all.sh
/usr/local/bin/find-here.sh
$
  • -l will print the filename where the text is found (in this example, we looked for "verity" and found the two scripts from above - inside those scripts there is the line that I wrote them so this found my name)
  • -i means ignore case
  • -r will go recursively into subdirectories

I could not resist, here is a quick script doing just that (find-inside.sh):

Bonus point: learn about a command option, specifically

What I have found particularly helpful is to sort through command manuals on shell but look up a particular piece.

For example, remind me, what does -a option mean in rsync? Let’s show the rsync help and highlight the -a:

$ rsync -h | grep --color -e '-a'
    xattrs, optional secluded-args, iconv, no prealloc, stop-at, crtimes
--archive, -a            archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
--append                 append data onto shorter files
--append-verify          --append w/old data in file checksum
--acls, -A               preserve ACLs (implies --perms)
--atimes, -U             preserve access (use) times
...

Try it, your output will look better than I can paste here. Every place where -a is found will be printed in red. That makes it so much easier to find! At a glance, I see that the answer is in the results line 2, -a means archive mode which is equivalent to -rlptgoD.

  • grep -e means we are grabbing an expression
  • --color adds the useful red highlight for every expression match