6

I'm writing a script to get all files within a folder(including sub-folder):

#!/bin/bash
function loop() {
files=`ls -1Fd $1`
echo "$files" |
while IFS= read -r file; do
    if [[ "$file" == */ ]]; then
        loop "$file*"
    else
        echo "$file"
    fi
done
}
loop "$PWD/*"

I tried to test the script in this way:

#create folders and files
mkdir test\ folder && mkdir test\ folder/test\ subfolder && touch test\ folder/test\ subfolder/test\ file && cd test\ folder

#execute the script
~/path_to_the_script/test.sh

But it doesn't work, here is the error:

ls: cannot access /home/user/Documents/test: No such file or directory
ls: cannot access folder/*: No such file or directory

How to modify the script to achieve it?

Searene
  • 4,550

2 Answers2

6

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"
terdon
  • 104,119
4

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 ;)

H. Freeze
  • 531