简体   繁体   中英

How to remove unused Images from Xcode Project?

I want to delete all the unused images from a XCode project and in order to do that I am using the following script:

#!/bin/sh
PROJ=`find . -name '*.xib’ -o -name '*.[mh]'`

for png in `find . -name '*.png'`
do
name=`basename $png`
if ! grep -q $name $PROJ; then
rm –Rf "$png"
echo "$png is not referenced"
fi
done

The above script is working fine and deleting all the images from the project that are not referenced in " .xib " however, there is a catch.

Problem

The script is also deleting the images that are referenced in " .m " files. (Images that are getting set programmatically)

Request

Could you please tell me how can I add " .m " with " .xib " files in search.

PROJ=`find . -name '*.xib’ -o -name '*.[mh]'`

First, not you are using rm -Rf to delete a single image. Be careful! This removes recursively and without forcing it, so it can be risky and remove things you don't want. Probably better to just say rm .

Your script is quite well organized and tidy. To make it more robust, it is always good to use quotes in the variables. This way, it will also support names with spaces. That is, if you want to remove a file called "a b.png", and the name is stored in the variable $png , saying rm $png you run rm a b.png , so it will try to remove a and b.png .

After all this introduction, let's focus on the specific problem here.

It looks like you are looking for those files that either end with .xib or m . The find . -name '*.xib' -o -name '*.[mh]' find . -name '*.xib' -o -name '*.[mh]' syntax seems to be fine, but it may be better to use regex in find .

find -type f -regex '.*\.\(xib\|m|h\)'

Finally, you are using a for loop to go through the result of a find . Note you can also say:

while IFS= read -r png;
do
    # things with "$png"
done < <(find ...)

but I won't go and suggest anything else here because I don't really follow the logic on these .xib , .png files. If you can show an example I will update my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM