7

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"

3 Answers 3

9

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).

4
  • How do you search for hidden folders that begin with . (period) e.g. .venv ?
    – JerodG
    Commented Oct 15, 2020 at 15:00
  • A command like find Downloads -name '\.*' -ls (with a suitable substitution for Downloads should work, in Terminal. Commented Oct 15, 2020 at 16:16
  • Finder does not seem to show names which begin with . in its searches, whether or not they are folders, at least with macOS 10.12.6 (I haven't tried newer versions). Commented Oct 15, 2020 at 16:19
  • 1
    If you only want to show folders, a command like find Downloads -type d -name '.*' -ls should work. Commented Oct 15, 2020 at 16:20
0

To illuminate and expand on Eirik's answer a bit:

The instructions he's provided you are essentially the same as you would use in the terminal, but with a few details hidden.

My guess is that you're seeking to clean-up file names, so this is something the terminal is really, very good for: precisely, in fact, why it's retained in any modern operating system at all.

If you're interested, let me know and I'll see about adding further details on what to type to (a) find all files matching your characteristics and (b) correcting their names (while keeping back-ups in case of concern) in a command you can paste into the terminal. :)

0

Despite the fact that this is an old question, this is still a current issue.

I was getting to a similar question from the Linux side, migrating Apple servers or replacing Netatalk as a file service by Samba on Linux. As there was nothing around solving all naming issues, I ended up writing a Python script to do my bidding. Although I tested this only on Linux, it should work on any POSIX system providing Python 3.

You can find the code here on my git: macSanitize

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .