1

I have a strange issue here using bash. In the following script (this is a simplified version) the find command should list all folders but it ignores some folders:

/Home Made videos
/Home made videos [1080p]

It will show only "/Home Made videos" and the count will be 1.

If I run the find command on the command line (outside script), it returns all files. Can someone say why and how to avoid it?

IFS=$'\n'
MAIN_DIR="$1"
DirArray=( $(find "$MAIN_DIR" -type d) ) 
tLenDir=${#DirArray[@]} 
for (( j=0; j<${tLenDir}; j++ ));
do
  echo "Folder: ${DirArray[$j]}"
done

Actually it was part of a bigger script (wrongly cut), the following code has the problem:

function main() {
    OLDIFS=$IFS
    IFS=$'\n'
    shopt -s nullglob
    MAIN_DIR="$1"
    [ "$MAIN_DIR" == "" ] && MAIN_DIR="."
    DirArray=( $(find "$MAIN_DIR" -type d) ) 
    tLenDir=${#DirArray[@]} 
    echo "Found ${tLenDir}"
    for (( j=0; j<${tLenDir}; j++ ));
    do
      if [[ "${DirArray[$j]}" != "." ]] && [[ "${DirArray[$j]}" != ".." ]] && [[ "${DirArray[$j]}" != "" ]] ; then
        echo "Folder: ${DirArray[$j]}"
      fi
    done
    IFS=$OLDIFS
    shopt -u nullglob
}

main "$1"

My bash is 4.3.39, the idea was (a) finding the folders then (b) for each one find the files. I managed to solve the problem changing to one find:

fileArray=($(find "$DIR" -type f -name '*.mkv' -o -name '*.mp4' -o -name '*.avi' ))

But if someone knows what was wrong with the old script I would like to know.

Sample:

"FolderA"
- "Folder B"
- "Folder [1080p]"
Thomas Dickey
  • 76,765
Alex
  • 11

0 Answers0