Or a better way would be to pass the path to the directory and then directly use ls with the passed in path. That way, there's no need to change the working directory by cd-ing into the passed-in directory.
e.g.
Modifying Robs code slightly:
Bash:
#!/usr/bin/env bash
#function to display usage info
usage()
{
echo
echo "Usage: $0 <path>"
echo
echo -e "Example: $0 /home/"
echo
exit 1
}
# Ensure there is exactly one parameter
if [[ $# -ne 1 ]] ; then
echo
echo "Error - requires a single parameter which"
echo "must be a path to a valid directory"
usage
# Ensure the parameter is a valid directory
else if [[ ! -d $1 ]] ; then
echo
echo "Error - $1 is NOT a directory!"
usage
fi
# Get the number of items in the passed in path
documentCount=$(ls $1 | wc -l)
control=10
echo "In directory: $1"
echo "Document Count = $documentCount"
if [ $documentCount -gt $control]
then
echo "File Count > $control"
else
echo "File Count < $control"
fi
Also - using ls will list files and directories in the passed-in path.
So the documentCount is more of an itemCount than a documentCount.
If you want to use ls to determine the number of actual files in a directory - excluding any directories, you would need to use something like this in your script:
Bash:
documentCount=$(ls -FL $1 | \grep -v '/$' | wc -l)
The -F parameter to ls causes ls to add a character to the end of each listing to determine each files type.
e.g.
* at the end means an executable file
/ at the end means a directory
no extra character at the end denotes a normal, non-executable file
@ at the end, denotes a symbolic link
The -L parameter causes ls to dereference any symbolic links.
A symbolic link is like a shortcut - it can point to a file, or a directory.
With the -L parameter-
ls -FL $1
will dereference any sym-links it finds and then append a "/" at the end if the link points to a directory, or "*" if it's an executable file, or nothing if the link points to a normal, non-executable file.
That way if there are any symbolic links that point to directories in the listing - they will be excluded from our count of documents.
After using
ls -FL
we pipe to
grep -v '/$'
to filter-out/remove the listings for directories (items with a / at the end).
The output from that is piped to
wc -l
to yield a count of actual files.
Or alternatively, if you want to exclude symbolic links from the count, you could do this instead:
Bash:
documentCount=$(ls -F | \grep -v '/$' | \grep -v '@$' | wc -l)
Either of those options would yield a more accurate count of the number of files in any given directory.
Or you could just use find:
Bash:
documentCount=$(find $1 -maxdepth 1 -type f | wc -l)
Find automatically excludes symbolic links. And setting -maxdepth to 1 prevents find from recursing into any sub-directories and adding their contents to the count.