There are lots of issues! Let’s take this part which is "working":
name=$( file -b $i )
if [ name == "directory" ]
This assigns the output of the file command to the variable called
name, but doesn't use it; instead, it runs the
[ command with 3 parameters:
== is a bash extension.
If this was corrected to use
$name rather than
name you would again get a
too many arguments problem for many cases. This is because
file returns multiple word results like
ASCII text. So after the command has run you get
if [ ASCII text == directory ]
and now it is obvious that the command is missing some grouping.
if [ "$(file -b -- "$i")" = "directory" ]
is probably what you want:
= rather than
== for portability, and quoting the result of command substitution which you almost always want to do.