I’m aware you can test if a file is a directory using:
if(-d $filename)
but how can you test if it’s not a directory?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Have you thought of trying the following?
The result of
-dis, after all, something that can be treated as a boolean value, hence the logical not operator!will work fine.And, if you’re after something more specific than “not a directory”, see here. There are quite a few things that aren’t directories that it sometimes doesn’t make sense to treat as regular files.
Keep in mind that’s assuming you’ve already validated that the filename exists. If you do a
! -don a non-existent filename, it will return true.It’s a philosophical matter as to whether you consider a non-existent thing to be “not a directory” but, if you want to ensure it exists as well, you can use
! -din conjunction with-e, something like: