[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1410
  • Last Modified:

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).
0
london_mccourt
Asked:
london_mccourt
2 Solutions
 
amit_gCommented:
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
 
TintinCommented:
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now