LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 05:45 PM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

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, 06:52 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,884

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
use find
Code:
 man find
 
Old 09-04-2016, 07:37 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,268

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
Use wildcard but remove the quotes in the test
Code:
SEARCHFILE="pi-2-black_status_*_*.txt"
if [ -f $SEARCHFILE ]
...
 
Old 09-06-2016, 06:22 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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, 11:33 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
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 11:34 AM.
 
Old 09-07-2016, 02:04 PM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,881

Rep: Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250
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 09:01 PM
Bash script to fgrep a large file. With list as source for searching. the_file Linux - Newbie 14 01-24-2011 04:42 PM
Searching tips for bash script hardening norbert74 Linux - Security 6 02-09-2010 01:26 PM
searching within a file using bash matt007 Linux - Software 6 07-23-2009 10:35 AM
bash script for database searching using crontab saurya_s Linux - Software 5 01-22-2004 09:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration