help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Validating files and directories


From: irenezerafa
Subject: Validating files and directories
Date: Sat, 13 Nov 2021 16:42:11 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Saturday, November 13th, 2021 at 9:06 AM, Andreas Kusalananda Kähäri 
<andreas.kahari@abc.se> wrote:

> On Sat, Nov 13, 2021 at 12:22:18AM +0000, irenezerafa via wrote:
>
> > I am using the following commands to validate a file or directory.
> > if [[ ! -f "$fl" && ! -d "$fl" ]]; then
> > printf '%s\n' "$fl: File or Directory does not exist"
> > fi

> Note that you can't replace your -f and -d tests with -e if you also
> care about files that are not regular files or directories (e.g.
> sockets, block device files, fifos etc.) A file that -e says exists may
> be neither a regular file nor a directory.
>
> You could possibly say
>
> if [[ -e "$fl" ]]; then
>
> if [[ ! -f "$fl" ]] && [[ ! -d "$fl" ]]; then
>
> printf '%s exists but is neither directory nor regular\n' "$fl"
>
> else
>
> printf '%s exists and is directory or regular\n' "$fl"
>
> fi
>
> else
>
> printf '%s does not exist\n' "$fl"
>
> fi
>
> The -e test is not a file-type test, while both -f and -d are.

I only need to check for regular files and names of actual directories, not 
anything else.
This suggests that I should continue to use the -f and -d constructs.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]