Find total size of uncommitted or untracked files in git

I adapted the answer of edmondscommerce by adding a simple awk statement which sums the output of the for loop and prints the sum (divided by 1024*1024 to convert to Mb)

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr  | awk ' {tot = tot+$1; print } END{ printf("%.2fMb\n",tot/(1024*1024)) }' 

Note that --porcelain prints pathnames relative to the root of the git repos. So, if you do this in a subdirectory the du statement will not be able to find the files..

(whoppa; my first answer in SoF, may the force be with it)


I've used a modified version of this, because I had files with spaces in them which made it crash. I was also unsure about the size calculations and removed a useless head:

git status --porcelain | sed 's/^...//;s/^"//;s/"$//' | while read path; do
    du -bs "$path" ;
done | sort -n | awk ' {tot = tot+$1; print } END { printf("%.2fMB\n",tot/(1024*1024)) }'

I prefer to use while as it's slightly safer than for: it can still do nasty things with files that have newlines in them so I wish there was a to pass null-separate files yet still be able to grep for the status, but I couldn't find a nice way for that.


I think I have answered my own question:

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr;  echo "TOTAL:"; du -cs .

However I'm open to any better ideas or useful tricks. My current output is 13GB :)


The above command is basically there, it gives me the total line by line from git status but doesn't give me the total sum. I'm currently getting the total of all files at the end which is not correct. I tried some use of bc but couldn't get it to work

Tags:

Linux

Git

Bash