loop through all files in a folder [duplicate]

Solution 1:

First of all, don't parse ls. Now, the reason your script fails is because you are passing "$PWD/*". Because that's qupted, it will be expanded before it's passed to your function to /path/to/dir/* and since there is no file named * in your PWD, it fails.

However, even if it worked, that would have gotten you into an infinite loop.

What you are looking for is:

#!/bin/bash

function loop() {
    ## Do nothing if * doesn't match anything.
    ## This is needed for empty directories, otherwise
    ## "foo/*" expands to the literal string "foo/*"/
    shopt -s nullglob

    for file in $1
    do
    ## If $file is a directory
    if [ -d "$file" ]
    then
            echo "looping for $file"
        loop "$file/*"
    else
            echo "$file"
    fi
    done
}
loop "$PWD/*"

That, however, will fail if your PWD contains any whitespace characters. A safer way is:

#!/bin/bash

function loop() {
    ## Do nothing if * doesn't match anything.
    ## This is needed for empty directories, otherwise
    ## "foo/*" expands to the literal string "foo/*"/
    shopt -s nullglob

    ## Make ** recurse into subdirectories
    shopt -s globstar

    for file in "$@"/**
    do
    ## If $file is a file
    if [ -f "$file" ]
    then
        echo "$file"
    fi
    done
}
loop "$PWD"

Solution 2:

Why are you going so much out of way, just keep it simple

#!/bin/bash
function loop() { 
    for i in "$1"/*
    do
        if [ -d "$i" ]; then
            loop "$i"
        elif [ -e "$i" ]; then
            echo $i
        else
            echo "$i"" - Folder Empty"
        fi
    done
}
loop "$PWD"

Hope that helps ;)