I have a folder containing all my PHP projects, I'd like a command that would output the folder size of each project but only for the *.php files inside it. Ideally the output would be something like this:

project1 7,0M
project2 13,3M

Assuming you have a folder that contains only project folders, you could run the following in that folder:

for proj in *; do
  phpfiles=$(find $proj -iname '*.php')
  size=$(du -ch $phpfiles | tail -n 1 | cut -d\t -f1)
  echo $proj $size

Explanation: we iterate over all project folders. In each folder, we find all *.php files. We compute the total size of these files, and output the project folder's name and the computed size.

In the first line I simply used * to iterate over all project folders. That's assuming your current working directory is a folder that contains only project folders. However, you could replace this with something more accurate, e.g., for proj in /home/user/proj1 /var/www/proj2 /opt/proj3, to iterate only over specific folders.

| improve this answer | |
  • Ok, thanks. Can't we exclude files directly? – COil Aug 19 '14 at 9:39
  • Not sure exactly what you mean. The command find $proj -iname '*.php' finds all files in folder $proj that end in .php - easy and straightforward. What do you want to exclude exactly? If there are certain PHP files that you do not wish to take into account, say a file exclude.php, you could use for instance find $proj -iname '*.php' -a ! -name 'exclude.php' meaning "find all files in folder $proj whose case-insensitive name ends in .php AND that are NOT named exclude.php". Refer to man find for more infos. I'll happily help, if you can tell me more precisely what you need ;) – Malte Skoruppa Aug 19 '14 at 10:06
  • I mean exclude files in the main directory that contains all projects. (which is your remark with *) But it would be cool to list the project folders dynamically. – COil Aug 19 '14 at 10:21
  • 1
    Ah, ok. Well for that we would need to come up with criteria that identify a project folder. For starters, you could use for proj in $(find . -mindepth 1 -maxdepth 1 -type d) to iterate only over the directories (-type d) that are exactly one level below the current working directory (-mindepth 1 -maxdepth 1), i.e., all folders in the current folder. To additionally exclude the folders not_this_folder and nor_this_folder below the current directory, you could use for proj in $(find . -mindepth 1 -maxdepth 1 -type d ! -name not_this_folder ! -name nor_this_folder). – Malte Skoruppa Aug 19 '14 at 10:34

No problem:

find /var/www/site/ -type f -name "*.php" -exec du -shc {} + | tail -1 | awk '{print $1}'

You can script this to make it show them all:


cd /var/www/
for i in $(find ./* -maxdepth 0 -type d)
    prjSz=$(find $i -type f -name "*.php" -exec du -shc {} + | tail -1 | awk '{print $1}')
    echo "${i:2}: $prjSz"
| improve this answer | |

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.