How do I find all files in Xcode that are NOT a member of a target

I don't know of a way to script it, but for a file you can view a list of all targets with the associated targets checked, and you can check or uncheck targets from that list.

Open the Utilities pane and select the File Inspector. Then select a file and the related targets will be in the Target Membership area of the File Inspector.

You can even select multiple files and the targets that are associated to some but not all the selected files will be marked with '-'. If you check a target then all the selected files will be added to that target.


Assuming your MyProject.xcodeproj is in your working directory, and all your sources are in or below your working directory, you could use a hack* like this to look for .m files that are not part of any target:

find . -name "*.m" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep "{} in Sources" MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1"

Once you remove the files from the project, you can remove them -- and other objc source files that are no longer referenced from your project -- from your git repo using this similar script:

find . -name "*.[hm]" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep {} MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1" | cut -c3- | xargs -I '%' find . -name '%' -exec git rm '{}' \;

* hack in the sense that it depends on implementation details of the Xcode file format.


I built a tool which lets you extract what files are in targets

https://github.com/ConfusedVorlon/ProjectFileTool

You could compare this with a file search for .m files within your project directory.