1

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!

oguz ismail
  • 1
  • 16
  • 47
  • 69
Nigel Wash
  • 107
  • 7
  • Look for these topics: Processing command-line arguments (*positional parameters*); Looping over files (`for f in *`, *for loops* and *wildcards*); Variables and arithmetics; Testing for files and directories (the `test` built-in command). You'll know what to do next. – iBug Mar 23 '21 at 06:47
  • You should look up the [*find(1)* command](https://man7.org/linux/man-pages/man1/find.1.html). You should be able to combine the `-type`, `-empty` and `-maxdepth` flags to achieve what you want. – Simon Doppler Mar 23 '21 at 06:51

2 Answers2

2

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
oguz ismail
  • 1
  • 16
  • 47
  • 69
1

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

jacouh
  • 8,473
  • 5
  • 32
  • 43