LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-04-2016, 04:45 PM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 14 & 15
Posts: 80

Rep: Reputation: Disabled
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
 
Old 09-04-2016, 05:52 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,364

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
use find
Code:
 man find
 
Old 09-04-2016, 06:37 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Use wildcard but remove the quotes in the test
Code:
SEARCHFILE="pi-2-black_status_*_*.txt"
if [ -f $SEARCHFILE ]
...
 
Old 09-06-2016, 05:22 AM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,097
Blog Entries: 11

Rep: Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215
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.
 
Old 09-07-2016, 10:33 AM   #5
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,950

Rep: Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684
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)

Last edited by BW-userx; 09-07-2016 at 10:34 AM.
 
Old 09-07-2016, 01:04 PM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,985

Rep: Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619
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.
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help for simple bash script - searching strings lpallard Linux - Software 21 10-09-2011 08:01 PM
Bash script to fgrep a large file. With list as source for searching. the_file Linux - Newbie 14 01-24-2011 03:42 PM
Searching tips for bash script hardening norbert74 Linux - Security 6 02-09-2010 12:26 PM
searching within a file using bash matt007 Linux - Software 6 07-23-2009 09:35 AM
bash script for database searching using crontab saurya_s Linux - Software 5 01-22-2004 08:53 AM


All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration