Reputation: 15930
Iterating a for loop, how do I make sure it ignores outputting directories?
for filename in /home/user/*
do
echo $filename
done;
Upvotes: 2
Views: 5589
Reputation: 140477
As in my previous answer you really want to use find
. What you're trying to do on 7-10 of lines scripting can just be done with this:
find /home/user -type f -printf "%f\n"
Upvotes: 4
Reputation: 27233
You can use -d
operator to check whether $filename
refers to a directory:
for filename in /home/user/*
do
if [ ! -d "${filename}" ]
then
echo $filename
fi
done;
See test manpage for details and other available operators.
You can also use the find command:
find /home/user -not -type d -maxdepth 1
Upvotes: 2
Reputation: 17234
find command is more suitable for what you want.
find /home/user -type f
Upvotes: 1
Reputation: 14034
You can use a conditional statement inside the loop. Something like this (untested):
if [ ! -d "$filename" ]
then
// do stuff
fi
-d
is true if it's a directory, and that's inverted with !
. So it will succeed if it does exist and isn't a directory.
Upvotes: 0
Reputation: 56129
for filename in /home/user/*
do
if [ ! -d "$filename" ]; then
echo $filename
fi
done
Or, use the find
command:
find /home/user ! -type d -maxdepth 1
Upvotes: 7
Reputation: 77175
while IFS='$\n' read -r filename
do echo "$filename"
done < <(find /home/user/ -type f -maxdepth 1)
Upvotes: 0