Reputation: 11
What would be the best means of being able to check whether a specified path (./filename followed by the filepath) is a directory? Ideally I would also be looking to print the list of the files within the directory should they be present. Thanks.
Upvotes: 0
Views: 151
Reputation: 8588
From Check if passed argument is file or directory in BASH.
The following script should do the trick.
#!/bin/bash
PASSED=$1
if [[ -d $PASSED ]]; then
echo "$PASSED is a directory"
elif [[ -f $PASSED ]]; then
echo "$PASSED is a file"
else
echo "$PASSED is not valid"
exit 1
fi
Double square brackets is a bash extension to [ ]
. It doesn't require variables to be quoted, not even if they contain spaces.
Also worth trying: -e
to test if a path exists without testing what type of file it is.
Upvotes: 2