I have an alias

alias binobj="find . -iname "bin" -o -iname "obj" | xargs rm -rf"

That I use when my disk is almost full. This deletes the folders "bin" and "obj", recursively searching from the current folder.

I would like to know how I can know the total sum of all deleted files. I have looked into placing an echo in between the 2 commands through a pipe, but then the xargs won't receive the files to delete.

I am on a mac, but I'm not shy of adding packages. I just don't want to put this in another file (this is my .bash_profile), as this one syncs with all my other macs.


You'll want to be very careful doing a recursive find for bin and obj... for example, you might accidentally pop off important files or directories (e.g: .../.hg/store/data/obj) which would not be a good idea.

Where at all possible, you want to be as precise as you can be. If you're trying to remove directories, then specify -type d so that you don't accidentally match a file / symlink / etc...

Additionally, this is large enough that it would benefit being a set of functions for readability / maintainability.

This set of functions (and finally a pipeline that can be aliased / put into a function) will remove all bin and obj directories that are next to a file called makefile (all case insensitive). The total bytes will be written to stderr using the shy numfmt to get output such as "9.1MB"

function getMakefiles {
    find . -type f -iname makefile

function getBinObjDirsNear {
    while read NEAR; do
        find $(dirname ${NEAR}) -type d -maxdepth 1 \( -iname bin -o -iname obj \)

function calcTotal {
    while read ENT; do
        TOTAL=$(( TOTAL + $(du -bc ${ENT} | tail -n 1 | cut -f 1) ))
        echo ${ENT}
    numfmt --to=iec --suffix=B --format="%f" ${TOTAL} >&2

getMakefiles | getBinObjDirsNear | calcTotal | xargs -d '\n' rm -rf
  • It is a really good idea to check for a makefile next to the obj bin folders to ensure nothing can be broken. I have executed the command before, but it's probably a good idea to stop doing that. I can't use makefile to check for though, but I can use the solution & project files where the folders will be next to. – user1009013 May 18 '17 at 14:06

I would simply compute the size before removing the folders :

alias binobj="du -sc bin/ obj/ | grep total ; find . -iname "bin" -o -iname "obj" | xargs rm -rf"

The command will prompt you the added size in bytes of the bin + obj directories from the current directory; then execute to delete.

Edit: According to comment : the bin and obj should be found recursively :

Without putting much time to find a elegant solution, I would log the size of each directory before removing it, and compute them after the cleaning is complete. A dirty one liner could be :

find . -iname "bin" -o -iname "obj" | xargs -I % sh -c "du -sb % >> /tmp/delreport.log; rm -rf %"; awk '{sum+=$1} END {print sum}' /tmp/delreport.log && rm /tmp/delreport.log

  • | xargs -I % sh -c "..." : allows you to put multiple commands with the \% parameter
  • du -sb : will log you the size of the repository which will be removed
  • awk '{sum+=$1} END {print sum}' : sum up all the size in the first column of the report
  • I think it's nearly good, but the du is not recursive, which is important as I sometimes run it from my projects folder to clean up all my projects. – user1009013 May 18 '17 at 7:41
  • @user1009013 I added a some information according to you need of recursively finding the directories – Esteban May 18 '17 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.