Bash: Iteration through directories acts weird Bash: Iteration through directories acts weird unix unix

Bash: Iteration through directories acts weird


The problem is that pathname expansion (expansion of '*') happens in the stat invocation, not in the loop declaration, because the path in the loop is quoted, but the path in stat invocation is not.

I.e. only one iteration of the loop is happening, with dir having value of /home/$USER/sessions/out/*/. That is then being expanded in stat invocation, supplying all the paths matching /home/$USER/sessions/out/*/log to stat.

The solution is to unquote the path in the loop declaration and quote it in the stat invocation.

Like this:

#!/bin/bashfor dir in /home/$USER/sessions/out/*/; do    size=$(stat -c %s "$dir/log")    if [ 1 -ne 0 ]; then        echo $size.    fidone


Your whole program can be simplified down to just one line

stat -c %s /home/$USER/sessions/out/*/log


Not quite sure why you have a test condition of 1 != 0, which always evaluates to true in there. But, the following loop will do it for you:

for LOGFILE in `find $LOGPATH -type f -iname 'log'`do   echo `stat -c %s $LOGFILE`'.'done

I used the variable LOGPATH where you would use '/home/$USER/sessions/out.' If this is in a script file, it would be wise to use a variable like that, at the top of the script, so that it could easily be changed later if need be.

The '-type f' is there in case there are directories with the name log, that you might not want to parse. If that is not the case, then you can omit that.

I used the find command, because you have much more finely grained control over it. You can set search depth, file type, file size, and so on. To get the period at the end, simply single quote a period right at the end of the echo statement.

If you needed the filename, and then the size, just change the echo statement, like so:

echo "$LOGFILE" `stat -c %s $LOGFILE`'.'