I am trying to check if an argument is a directory or a file. I want to put a / after each directory name and a * after every executable file. I know ls uses -F to get this information but I can't figure this out in my script.
Here is my code:
echo -n "Please enter Directory name you wish to search: "
read dir
for filename in "/home/me/Desktop/$dir"/*
do
if (-F $filename)
then
echo $filename
fi
done
First check the provides argument is the directory or not using the if statement using the -d option for the first argument using the $1 parameter. If it is true then print the message that the provided argument is the directory. If the argument is not the directory then check it for the file.
In order to check if a directory exists in Bash, you have to use the “-d” option and specify the directory name to be checked.
Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.
[ -f "$filename" ]
is true for files, [ -d "$dirname" ]
is true for directories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With