I want to know, if my solution of finding a filename or "filetype" in conjunction with an if-statement is good practice? Maybe there's a safer/better way doing stuff like this?
#!/usr/bin/env bash
DIR="/home/user"
SUB_DIR="/Downloads"
if [[ -n "$(find $DIR$SUB_DIR -depth -type f -name '*.jpg' -print0 -quit 2>/dev/null)" ]]; then
echo "Found JPG-File"
elif [[ -n "$(find $DIR$SUB_DIR -depth -type f -name '*.png' -print0 -quit 2>/dev/null)" ]]; then
echo "Found PNG-File"
#... and much more elif statements
fi
-print0
- if there are white spaces in filename
-quit
- first catch and than quit find command
I also use this line of code to exclude sub-folders from search:
if [[ -n "$(find $DIR$SUB_DIR -type d \( -path */RAW* -o -path */VIDEO* \) -prune -o -type f -name '*.jpg' -print0 -quit 2>/dev/null)" ]]; then
echo "Some other stuff"
fi