KSH: Determine if a path is a file or a directory

I'm writing a shell script that does a little searching through a file system, however I need to be able to check whether what is found is either a file or a directory.

Its for the KSH (AIX/Solaris/LINUX compatible).
london_mccourtAsked:
Who is Participating?
 
amit_gConnect With a Mentor Commented:
filepath=/path/to/file
if [ -f $filepath ]
then
   echo "It is a file"
elif [ -d $filepath ]
then
   echo "It is a directory"
fi
0
 
sjm_eeCommented:
... might not be either ...
0
 
TintinConnect With a Mentor Commented:
Are you using find?

If so you can search for files with

find / -type f

and directories with

find / -type d

Otherwise, use amit_g's solution, with the following addition check

filepath=/path/to/file

if [ -f $filepath ]
then
   echo "It is a file"
elif [ -d $filepath ]
then
   echo "It is a directory"
else
   echo "Neither a file or a directory"
fi
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.