Hello everybody. I am attempting to write a simple script using a for loop that counts the number of directories, the number of simple files. and the number of symbolic links in the current directory, and then prints this information. The output should look something like...

directories: 12
files: 20
links: 3

Here is what i have...

#!/bin/bash

linkCount=0
directoryCount=0
fileCount=0

for file in *

do
    if [ -d $file]
    then
        directoryCount=$((directoryCount + 1))
    elif [ -h $file]
    then
        linkCount=$((linkCount + 1))
    elif [ -f $file]
    then
        fileCount=$((fileCount + 1))

echo "there are " + linkCount + " links."
echo "there are " + directoryCount + " directories."
echo "there are " + fileCount + " files."

for some reason it isnt doing what i want it to, can somebody please point me in the right direction?

Well, for one, you haven't closed the if statement with "fi" nor closed the for statement with "done". Also, you need to check for links first or a link to a directory will probably be counted as a directory.

If you are willing to walk the structure three times, it is easy to use find to do it. For instance find $topdir -depth 1 -type l -print gets all the links in $topdir

Edited 5 Years Ago by griswolf: n/a

Also line 7: don't use variable names that sound like proper commands.. by default make it a habit to prefix/suffix "my" or something else to all your variables..
"file" is a command.

This article has been dead for over six months. Start a new discussion instead.