LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash - quickest way to do multiple searches for files (http://www.linuxquestions.org/questions/programming-9/bash-quickest-way-to-do-multiple-searches-for-files-802248/)

librano 04-15-2010 01:57 PM

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.

Sergei Steshenko 04-15-2010 02:57 PM

Quote:

Originally Posted by librano (Post 3936770)
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.

unSpawn 04-15-2010 03:02 PM

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'.

Tinkster 04-15-2010 03:42 PM

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 ...

librano 04-15-2010 03:43 PM

Wow! thanks for the updatedb tip. That is awesome!

librano 04-15-2010 03:51 PM

Quote:

Originally Posted by Tinkster (Post 3936872)
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?


All times are GMT -5. The time now is 11:53 AM.