How to iterate over a directory for files without including sub-directories in Bash
1
vote
1
answer
125
views
I need to parse through a directory that contains files and subdirectories however I am unable to use the find command. The for loop is supposed to go through each file and compare it to another directory but I cannot get it to pass over the subdirectories.
difference=0
for item in $1
do
if [[ -d $item ]]
then
continue
fi
if [ ! -f "$2/$(basename $item)" ]
then
echo "$2/$(basename $item) is missing"
difference=1
fi
if [ diff $item "$2/$(basename $item)" ]
then
echo "$1/$(basename $item) is different"
difference=1
fi
done
Using the code above it does skip over the subdirectories but then also doesn't compare the files. I know that if I omit the first if statement with the continue and put:
for item in $(find $1 -type f)
It works however I am unable to use that function. Any help is greatly appreciated. Furthermore I also have the other directory comparing to this one since its the same code I didn't write it down.
Asked by IMPNick
(11 rep)
Feb 13, 2023, 01:26 AM
Last activity: Feb 13, 2023, 02:28 AM
Last activity: Feb 13, 2023, 02:28 AM