[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Validating files and directories
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: Validating files and directories |
Date: |
Sat, 13 Nov 2021 10:06:01 +0100 |
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
>
> But have noticed that I can use -e to see if there's something by that name,
> instead of separately testing -f and -d.
> Yet I am getting confused between using -a and -e.
Why would you be confused between -a and -e? You don't use -a here and
you never ever need to use it.
See https://unix.stackexchange.com/questions/147728
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.
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.
- Netiquette, (continued)
- Netiquette, irenezerafa, 2021/11/17
- Re: Re: Netiquette, Thomas Paulsen, 2021/11/18
- Re: Re: Netiquette, Greg Wooledge, 2021/11/18
- Re: Re: Re: Netiquette, Thomas Paulsen, 2021/11/18
- Netiquette, irenezerafa, 2021/11/20
- Re: Netiquette, Chet Ramey, 2021/11/18
- Re: Netiquette, Lawrence Velázquez, 2021/11/13
- Netiquette, irenezerafa, 2021/11/13
- Re: Validating files and directories, Chris F.A. Johnson, 2021/11/13
- Validating files and directories, irenezerafa, 2021/11/13
Re: Validating files and directories,
Andreas Kusalananda Kähäri <=