Nigel Wash
Nigel Wash

Reputation: 107

Counting empty and non-empty files within directories in Bash

Unfortunately I do not have any code as a starting point as I have no idea where to begin. I need to create a bash script named dircheck that allows the user to run the program with a directory path (e.g. ./dircheck ~Documents/unit/backup) and for the script to output to the terminal the following:

name of directory entered has:
x empty files,
x files with data,
x empty directories,
x non-empty directories

As you can see I need to output how many files the directory has that are empty, how many files the directory has that have data, how many directories that are empty, and how many directories that have data.

Any help would be greatly appreciated!

Upvotes: 1

Views: 1198

Answers (2)

jacouh
jacouh

Reputation: 8741

I would propose this as another way with dircheck.sh, by a bash function to check recursively a given directory, using only bash intrinsic syntax (shell glob) - without calling outer command:

#!/bin/bash
#
# check recursively a directory:
#
function checkDirr() {
  for x in $1/*
  do

    #
    # if it's a sub-directory:
    #
    if [ -d $x ]; then
      #echo "$x is a directory."
      ndirs=$((ndirs + 1))
      #
      # glob all entries in an array:
      #
      entries_array=($x/*)
      #
      # check entries array count:
      #
      if [ ${#entries_array[*]} -eq 0 ]; then
        #echo "empty dir"
        nemptydirs=$((nemptydirs + 1))
      else
        #
        # call recursively myself to check non-empty sub-directory:
        #
        checkDirr $x

      fi

    #
    # otherwise it's a file:
    #
    else
      #echo "$x is a file."
      nfiles=$((nfiles + 1))
      #
      # check empty file:
      #
      if [ ! -s $x ]; then
        nemptyfiles=$((nemptyfiles + 1))
      fi
    fi

  done
}

#
# main():
#
#
# initialize globals:
#
ndirs=0
nfiles=0

nemptydirs=0
nemptyfiles=0

#
# use given directory path or the current directory .:
#
dir0=${1:-.}

#
# set shell nullglob option to avoid dir/* string when dir is empty:
#
shopt -s nullglob

#
# now check recursively the directory:
#
checkDirr $dir0

#
# unset shell nullglob option:
#
shopt -u nullglob

#
# send statistics:
#
echo $dir0 has:
echo
echo $nemptyfiles empty files
echo $((nfiles - nemptyfiles)) files with data
echo $nemptydirs empty directories
echo $((ndirs - nemptydirs)) non-empty directories

    

This gives for example

$ dircheck.sh so

so has:

2 empty files

87 files with data

3 empty directories

11 non-empty directories

Upvotes: 1

oguz ismail
oguz ismail

Reputation: 50750

As suggested in comments, GNU find can do this fairly easily.

#!/bin/bash -
echo "$1 has:"
find "$1" -mindepth 1 -type f,d \
  \( -empty -o -printf 'non-' \) -printf 'empty ' \
  \( -type f -printf 'files' -o -printf 'directories' \) \
  -printf '\n' | sort | uniq -c

You can restrict the analysis to the first level entries only by adding -maxdepth 1 to the find invocation. Anyway, its output will look like this:

$ ./foo ./bionic
./bionic has:
      7 empty files
    175 non-empty directories
   2163 non-empty files

Upvotes: 2

Related Questions