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]"
script dir1 dir2
as input? Then (if dir1 has no subdirs) of course only dir1 will show up asMAIN_DIR="$1"
... at least that is what I read from your question. – FelixJN Aug 11 '15 at 07:34ls
. The short answer is to replaceshopt -s nullglob
withset -f
. – G-Man Says 'Reinstate Monica' Oct 16 '15 at 04:20