Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Last active March 17, 2022 19:26
Show Gist options
  • Save djeikyb/7d00d827e01c60d164205b2b658a491a to your computer and use it in GitHub Desktop.
Save djeikyb/7d00d827e01c60d164205b2b658a491a to your computer and use it in GitHub Desktop.
find and delete dotnet's bin/ and obj/ dirs adjacent to csproj files

find and delete bin/ and obj/ dirs adjacent to csproj files

find . -iname '*.csproj' -type f -print0 | xargs -0 -I{} sh -c 'rm -r $(dirname {})/{bin,obj}'
  1. find files with the csproj suffix
  2. output using null termination to help safely handle file names
  3. xargs option -0 signals the input lines are null terminated
  4. xargs option -p could prompt before executing
  5. xargs option -I designates the placeholder string
  6. run in a new shell process, because i want to use shell expansion techniques AFTER interpolation occurs
  7. dirname gets the absolute path that contains the csproj
  8. then shell expansion creates a command like rm -r /path/bin /path/obj

clean bin obj

old version:

find . -iname 'bin' -type d -o -iname 'obj' -type d -print0 | xargs -0 -I{} rm -r {}

This isn't reliable. for starters, I think if one of bin/ or obj/ was missing, the other wouldn't get picked for deletion. Also, the find command's syntax is incredibly difficult to understand. The operator binding and general order of operations is opaque. There's syntax for grouping, but it adds a lot of visual noise and itself has behaviour that is difficult to predict.

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