Question is in the title been trying to do this. I want to do it with a for loop. This is what I've come up with so far which does not work.for home in /home/ {.,/}*; do echo $home; doneI would like to do this non-recursively if possible.
How do I write a script to count the total number of files, and directories in my home directory then display it?
shell;shell script;directory;home
There are many higher level commands that will almost do what you want to do, but this is a good demonstration of how to do the same thing in a shell script.  This should work on any system that has /bin/sh.  It's not dependent on any other commands.You can save this script as any filename, and then type sh ./whatever_you_named_it to execute it.I've split this into two sections, Listing the Files, and Counting the Files.  Listing the files recursively is the most complicated, but once we have a list, it's easy to count them.Listing the FilesThis script recursively lists all of the files and directories under your home directory or a directory it receives as a parameter.  With no parameters, it defaults to the home directory.#!/bin/shif test $# -eq 0then    startdir=$HOMEelse    startdir=$1fifor f in $start/*          # step through all files in the starting directorydo     echo $f               # print the file name     if test -d $f         # is the file a directory? (-d)     then        sh $0 $f         # yes call this script with the dir as arg     fidoneLet's discuss each step:If no parameters were passed to the script, then use $HOME as the starting directory, otherwise use the first parameter. ($# contains the number of parameters passed to this script and we use the shell's built-in test command to find out if it's zero):if test $# -eq 0then        startdir=$HOMEelse        startdir=$1fiIterate over files in your home directory.  $HOME/* expands to all of the non-hidden files and directories at the top level of your home directory:for f in $HOME/*doPrint the file name.  I think you know echo:     echo $f               # print the file nameCall the shell built-in test command with the -d option. test returns true if the argument, $f, is a directory. This is most often written as [ -d $f ] for brevity, but it really is a command. [Search for test expr in the sh man page]:    if test -d $f         # is the file a directory? (-d)    thenThis next statement is where we recur. We start a new copy of the currently running script and pass a the current $f directory name to it. The $0 expands to the filename of the current shell script. Rather than type some filename here, we use $0 so this script will work even if it's name is changed. For example, if the script was called rlist, then you could also write sh rlist $f, but if the script was renamed, the script would no longer function correctly, because it would be calling a non-existent script, or the wrong script. [See Special Parameters in the sh man page]:        sh $0 $f         # yes call this script with the dir as argTerminates the if statement and the for loop.     fidoneCounting the FilesThe script so far lists the files but does not count them.  If you want to get a count of all the files and directories execute this:sh ./scriptname | wc -lwc -l is the word count command, and with the -l option counts only the lines.  The above pipeline prints a number which is the number of files under your home directory.