List the amount of space occupied by a file type in the terminal

Posted on

Question :

I suspect that the PCs I use have many .RData files, used by the R program to save data sets. I want to do a cleanup on these files, but without going into directory by directory, from computer to computer. In addition, I use computers with at least 3 different operating systems: macOS, Ubuntu, and CentOS.

I already searched the internet and the command

find . -iname '*RData' -print0 | du -ch --files0-from=-

find the files you want, and calculate the space occupied by each of them. However, it only works on Ubuntu and CentOS. When I try to run macOS, I get the following message:

du: illegal option -- f
usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m | -g] [-x] 
[-I mask] [file ...]

What makes sense, because unlike du of Ubuntu and CentOS, du of macOS does not have the --files0-from flag.

How can I solve this problem in macOS? I suppose it’s not through du , but is there any way to do this via terminal? I am using bash 4.4.12 (1) -release.


Answer :

Solution with utilities find + du + awk :

$ find . -iname "*RData" | du | awk '{bytes+=$1} END{print bytes}'


find . -name "*RData" -exec du -c {} +


Leave a Reply

Your email address will not be published. Required fields are marked *