LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script for searching for a file using wildcards (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-for-searching-for-a-file-using-wildcards-4175588722/)

Entropy1024 09-04-2016 05:45 PM

Bash script for searching for a file using wildcards
 
I want to search for the existence of a file in a certain folder and execute commands if it finds it. The code below sorta works, but the question marks are not treated as wildcards. If I replaced the question marks with the exact file to mach such as:
pi-2-black_status_160904_2340.txt
It would find it fine. However I need to search using the wildcards.
How may I achieve this please?

Code:

cd /home/tim/Documents/pi-2-black/status/
SEARCHFILE="pi-2-black_status_??????_????.txt"
if [ -f "$SEARCHFILE" ]
then
echo "Found"
else
echo "Not found"
fi


Keith Hedger 09-04-2016 06:52 PM

use find
Code:

man find

keefaz 09-04-2016 07:37 PM

Use wildcard but remove the quotes in the test
Code:

SEARCHFILE="pi-2-black_status_*_*.txt"
if [ -f $SEARCHFILE ]
...


Habitual 09-06-2016 06:22 AM

Tim:
If you know where it is, or expected to be
what is
Code:

cd /home/tim/Documents/pi-2-black/status/
all about?
Code:

if [ -f /home/jj/LQRocks  ]; then echo 'Yes, LQ Does Rock' ; else echo "Sucks to be you"; fi
Wildcards likely not supported here. See first question.

BW-userx 09-07-2016 11:33 AM

why not just open up a term and type
Code:

find  (path to where you want to look) / (being everything that is mounted) -type f -name pi-2-black_status_*_*.txt
then wait for your results.

do not forget if you're looking on the system side you have to be root or use sudo then the find ... (command)

pan64 09-07-2016 02:04 PM

running ls -lR <rootdir> with a little post-processing (for example in perl or awk) will run much faster. But probably not important at all.


All times are GMT -5. The time now is 06:50 AM.