Skip to content

Instantly share code, notes, and snippets.

@alexsorokoletov
Last active October 1, 2019 12:13
Show Gist options
  • Save alexsorokoletov/3aa685c0d731d8f8eea44fc6cf0f6872 to your computer and use it in GitHub Desktop.
Save alexsorokoletov/3aa685c0d731d8f8eea44fc6cf0f6872 to your computer and use it in GitHub Desktop.
Clear bin/obj folders for Xamarin projects, as well as other temporary Xamarin files. Developer lifesaver
#!/bin/bash
# based on http://stackoverflow.com/questions/755382/i-want-to-delete-all-bin-and-obj-folders-to-force-all-projects-to-rebuild-everyt
find . -iname "bin" -type d | xargs rm -rf
find . -iname "obj" -type d | xargs rm -rf
# clear VS4Mac temporary downloads
echo ~/Library/Caches/VisualStudio/7.0/TempDownload/
for f in ~/Library/Caches/VisualStudio/7.0/TempDownload/* ; do
sudo rm -rf $f
done
#optionally you can run folowing to clear NuGet cache: nuget locals all -clear
@martijn00
Copy link

This doesn't work if you have a lot of files in a directory. I've adjusted it to this to work:

for d in  /Users/username/Documents/*/ ; do
    echo "$d"
    cd "$d"
    find . -iname "bin" | xargs rm -rf
    find . -iname "obj" | xargs rm -rf
    cd .
done

@alexsorokoletov
Copy link
Author

@martijn00 thanks, I like the idea of putting in path instead of opening folder you want to clear in terminal every time.
You can use pushd and popd instead of cd, btw.

@alexsorokoletov
Copy link
Author

@martijn00 I've updated the snippet to include your suggestion.
Now checking only folders.

@scriptam
Copy link

scriptam commented Apr 7, 2018

Nice. But it won't work on folders with spaces in the name. Consider the following replacement:

find . -iname "bin" -type d -o -iname "obj" -type d | tr '\n' '\0' | xargs -0 rm -rf

This combines two separate find commands into one (using the "or" operator on find)
It also prevents xargs from failing (xargs treats path names with spaces as separate arguments)

This alternative is terser, and works fine on the Mac.

@alexsorokoletov
Copy link
Author

Very nice, @scriptam!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment