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?

Recommended Answers

All 3 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.