MacOS – How to search Mac for files that begin or end with a space “ ” or period “.”

filefindermacmacos

I just need to know how to search (within Finder vs Terminal, if possible) for all file names within a specific folder, which begin or end with a space or a period.

For example:
" file.jpg" (begins with a space)
"file .jpg" (ends with a space)
"file..jpg" (ends with a period

Additionally, finding a file name with the characters * or " in it. Searching for this in file name using Finder produces thousands of results which don't actually have * or " in it.
Example: "fil*e.jpg" or "fil"e.jpg"

Best Answer

With Finder as the active application, choose Find from the File menu. In the Searching "This Mac" window that brings up, select Kind is Document, then use + to add another condition with Name as the first item. If you choose begins with as the second item, you can find files whose names begin with a particular character, like space. The ends with condition won't find files with the names in your examples unless the filename suffix ends with the character of interest, but you can find a particular character, like a space, as the last character before the suffix if you choose contains and follow that character with .. For your file..jpg example you would use .. for the search text. This approach will find literal * characters in filenames, if you choose contains and use * for the search text.

Finding such files with the find command (in Terminal) is straightforward if you quote special characters appropriately. For example,

find Downloads -name ' *' -ls

reports all files in my Downloads directory which begin with a space,

find Downloads -name '*\**' -ls

reports all files in my Downloads directory with a * in its name,

find Downloads -name '*\.\.*' -ls

reports all files in my Downloads directory with two consecutive . characters in its name, and

find Downloads -name '* .*' -ls

reports all files with a space character immediately before the suffix. Finally,

find Downloads -name ' *' -ls -o -name '*\**' -ls -o -name '*\.\.*' -ls -o -name '* .*' -ls

reports all of those files in my Downloads directory. You can anchor the search elsewhere by replacing Downloads on the command line, and you can use other options to restrict the search further (-type f would ignore directories and symlinks).