LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-15-2010, 01:57 PM   #1
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Rep: Reputation: 31
Bash - quickest way to do multiple searches for files


Hi all,

I am looking for the quickest way to do multiple searches for different files.

Here is the scenario: I want to search for various icons one by one in a script. Preference is for the gnome icon theme.

Code:
#!/bin/bash

# icons to search for
icon1="firefox"
icon2="thunderbird"
icon3="gimp"

# create list of all icons
iconList=`find /usr/share/icons/gnome/ /usr/share/icons/`

searchFunction () {
  echo "$iconList" | grep -m 1 -i $1
}

searchFunction $icon1
searchFunction $icon2
searchFunction $icon3
The echo piped into grep part seems to take a long time. Is there a quicker way of doing this?

I do not want to use slocate, sqlite or the like since that will require admin privileges to update the database and/or extra dependencies.
 
Old 04-15-2010, 02:57 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by librano View Post
Hi all,

I am looking for the quickest way to do multiple searches for different files.

Here is the scenario: I want to search for various icons one by one in a script. Preference is for the gnome icon theme.

Code:
#!/bin/bash

# icons to search for
icon1="firefox"
icon2="thunderbird"
icon3="gimp"

# create list of all icons
iconList=`find /usr/share/icons/gnome/ /usr/share/icons/`

searchFunction () {
  echo "$iconList" | grep -m 1 -i $1
}

searchFunction $icon1
searchFunction $icon2
searchFunction $icon3
The echo piped into grep part seems to take a long time. Is there a quicker way of doing this?

I do not want to use slocate, sqlite or the like since that will require admin privileges to update the database and/or extra dependencies.
Consider using 'egrep' and regular expression - for your case look in the manual for '|' without the quotes.
 
Old 04-15-2010, 03:02 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,112
Blog Entries: 54

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
BTW using 'locate' shouldn't be a problem either as unprivileged user: just create your own database with 'updatedb -U /usr/share/icons -o /dev/shm/icons.db --require-visibility no' then search with 'locate --database /dev/shm/icons.db -i opera'.
 
1 members found this post helpful.
Old 04-15-2010, 03:42 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,971
Blog Entries: 11

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
Or
Code:
find /usr/share/icons/ -regextype posix-extended -regex ".*(firefox|mozilla|gimp).*"
...

As /usr/share/icons/gnome is a subdir of /usr/share/icons/ there's no point
in searching it separately ...
 
Old 04-15-2010, 03:43 PM   #5
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Original Poster
Rep: Reputation: 31
Wow! thanks for the updatedb tip. That is awesome!
 
Old 04-15-2010, 03:51 PM   #6
librano
Member
 
Registered: Jul 2004
Location: Here, there and everywhere.
Distribution: Arch+KDE, Linux Mint Fluxbox CE
Posts: 163

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Tinkster View Post
Or
Code:
find /usr/share/icons/ -regextype posix-extended -regex ".*(firefox|mozilla|gimp).*"
...

As /usr/share/icons/gnome is a subdir of /usr/share/icons/ there's no point
in searching it separately ...
The reason I had the gnome directory in the find command is so that it lists the preferred theme at the top... and grep stops at the first match... which will be from the gnome folder if it exists.

Also, I didn't want to repeatedly run find every time I search for an icon... hence creating the list. There is a performance advantage in doing this, right?
 
  


Reply

Tags
bash, fast, find, grep, script, search, speed


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 08:30 AM
how do i use bash to perform an operation on multiple files Brynn Linux - Newbie 6 07-24-2006 06:02 AM
Multiple searches (sorry if this is a long thread) genderbender Programming 2 01-30-2005 06:03 PM
Multiple searches? genderbender Programming 7 01-30-2005 04:20 PM


All times are GMT -5. The time now is 07:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration