LinuxQuestions.org
Visit Jeremy's Blog.
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 07-16-2004, 11:00 AM   #1
deanbrown3d
Member
 
Registered: Jun 2004
Location: Central NJ, USA
Distribution: Red Hat / FC2
Posts: 66

Rep: Reputation: 15
Can I specify an exact filename in 'locate'?


Or in slocate? I want to seach for example "socket.h" without finding files that contain this name, e.g. "other_socket.h"


thanks muchly

Dean
 
Old 07-16-2004, 11:21 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
You can provide a regular expression to (s)locate. Something like:
Code:
locate -r "^\(.*/\)*socket\.h$"
<edit>
Added the '$' to the expression... forces an end-of-line match... just as insurance.
</edit>

Last edited by Dark_Helmet; 07-16-2004 at 11:28 AM.
 
Old 07-16-2004, 11:22 AM   #3
TheIrish
Member
 
Registered: Oct 2003
Location: ITALY
Distribution: Debian, Ubuntu, Fedora
Posts: 137

Rep: Reputation: 15
or, you may use find
 
Old 07-16-2004, 11:22 AM   #4
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
hmm...I haven't tested this one, and I guess there are better ways to achieve this...but...if you would type

locate ?ocket.?

that shouldn't find other_socket.h (or socket.h_other either), because it would only look for things that include "ocket." and before that there is only one letter and after that there is only one letter. this does, however, find things like "bocket.s", "socket.o" etc., but at least it will get something out for you
 
Old 07-16-2004, 11:26 AM   #5
corbis_demon
Member
 
Registered: Jun 2004
Location: India
Distribution: CLFS
Posts: 515

Rep: Reputation: 36
The search string in slocate can contain any set of characters and it's okay if you specify filename.The usual options are:

slocate [-qi] [-d <path>] [--database=<path>] <search string>

slocate [-i] [-r <regexp>] [--regexp=<regexp>]

slocate [-qv] [-o <file>] [--output=<file>]

slocate [-e <dir1,dir2,...>] [-f <fstype1,...>] <[-l <level>] [-c] <[-U <path>] [-u]>

slocate [-Vh] [--version] [--help]
 
Old 07-17-2004, 03:21 PM   #6
cpanelskindepot
Member
 
Registered: Jun 2004
Posts: 43

Rep: Reputation: 15
Is there any locate alternative?
The thing about locate is it requires us to do an updatedb once a week.
I need something that is less of a hassle.
 
Old 07-17-2004, 03:28 PM   #7
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
Why not just set a cron job to update your database? No hassle at all.
 
Old 07-17-2004, 09:07 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
I got to thinking that having a version of locate find a specific filename rather than matching text within the path/filename might be pretty useful. So I wrote a quick bash function to do it using the regular expression method I posted earlier. I dunno if anybody really wants it, but I figured I'd throw it up anyway.
Code:
function flocate
{
  if [ $# -gt 1 ] ; then
    display_divider=1
  else
    display_divider=0
  fi

  current_argument=0
  total_arguments=$#
  while [ ${current_argument} -lt ${total_arguments} ] ; do
    current_file=$1
    if [ "${display_divider}" = "1" ] ; then
      echo "----------------------------------------"
      echo "Matches for ${current_file}"
      echo "----------------------------------------"
    fi

    filename_re="^\(.*/\)*$( echo ${current_file} | sed s%\\.%\\\\.%g )$"
    locate -r "${filename_re}"
    shift
    (( current_argument = current_argument + 1 ))
  done
}
Slap that into your ~/.bash_profile and you're good to go.

I named it flocate as an abbreviation for "file locate" but that's just personal preference. It can take more than one filename, and will print a divider if multiple filenames are given. Just like the name, if the divider gets on your nerves, feel free to change it or rip it out completely.

Here's an example of the output:
Code:
[not_me@SpaceBall1 not_me]$ flocate socket.h general_info.txt
----------------------------------------
Matches for socket.h
----------------------------------------
/home/not_me/temp/ucspi-tcp-0.88/socket.h
/usr/lib/bcc/include/sys/socket.h
/usr/include/sys/socket.h
/usr/include/asm/socket.h
/usr/include/linux/socket.h
/usr/include/bits/socket.h
/usr/local/include/libguile/socket.h
/usr/local/src/build/mozilla/xpinstall/wizard/libxpnet/GUSI/include/sys/socket.h
/usr/src/linux-2.4.18-14/include/abi/util/socket.h
/usr/src/linux-2.4.18-14/include/asm-i386/socket.h
/usr/src/linux-2.4.18-14/include/linux/socket.h
----------------------------------------
Matches for general_info.txt
----------------------------------------
/home/not_me/documents/misc/general_info.txt
[not_me@SpaceBall1 not_me]$
 
Old 07-17-2004, 09:56 PM   #9
cpanelskindepot
Member
 
Registered: Jun 2004
Posts: 43

Rep: Reputation: 15
I have no idea what that was but it sure looks cool! LOL

Good job!
 
Old 07-18-2004, 09:03 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
In studying the function provided by dark_helmet, I entered this:
Code:
$ current_file=stdio.h
$ echo "${current_file} | sed s/\\./\\\\./g"
stdio.h | sed s/\./\\./g
$ echo ${current_file} | sed s/\\./\\\\./g
stdio\.h
Keeping track on how bash sees lines of code is something that still trips me up.

I added the function to my ~/.bash_profile. I am impressed D_H!
 
Old 07-18-2004, 11:27 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Thanks!

I usually go to a command line and experiment with commands that need backslashes. They invariably turn into backslash conventions. So, as much as I would like to say I coded that function from a thorough understanding of escape character interpretations... I can't because it still confuses me too at times.

And for anyone curious to know why I replace each . with \. - it's to prevent locate from misinterpreting the regular expression. A normal period in an RE is interpreted like a ? on the command line: any single character can match. Putting a backslash in front forces an exact match for the period.

Anyhoo, this serves as yet another example of why comments is a good thing. Now I just need to work on the laziness part.
 
Old 05-23-2013, 11:31 PM   #12
sunlitlaz
LQ Newbie
 
Registered: Mar 2012
Location: Southwest, Virginia
Distribution: Debian
Posts: 2

Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by Dark_Helmet View Post
I got to thinking that having a version of locate find a specific filename rather than matching text within the path/filename might be pretty useful. So I wrote a quick bash function to do it using the regular expression method I posted earlier. I dunno if anybody really wants it, but I figured I'd throw it up anyway.
Dark_Helmet, I have to say that this function you wrote has been extremely useful as I go about my server management duties. I just wanted to make sure that you knew that you wrote a kicking function, and it is in regular use every day, so thank you!! I plan to post a writeup about it on my tech support blog (giving you full credit of course), and I bet lots of other people will be happy to use it too.

Thanks again - Laz
 
Old 05-24-2013, 12:25 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You do realise this thread is from 2004??
D_H is probably around, but you might be being a tad optimistic here; why not msg him instead
 
Old 05-25-2013, 02:31 PM   #14
sunlitlaz
LQ Newbie
 
Registered: Mar 2012
Location: Southwest, Virginia
Distribution: Debian
Posts: 2

Rep: Reputation: Disabled
Question

Quote:
Originally Posted by chrism01 View Post
You do realise this thread is from 2004??
D_H is probably around, but you might be being a tad optimistic here; why not msg him instead
I posted the thank you here, because I wanted to thank him publicly. Therefore, anyone that happened by this thread could see it. People do find this thread still using a search engine, that's how I found it. I surely didn't first read this almost ten years ago!

So now, aside from saying thanks to Dark_Helmet and generally trying to be nice to him and the community, anyone that happens by this thread and is interested in using his function can see that someone else found it safe and useful. This hopefully will encourage them to try it out, and maybe (hopefully) even, generate some interest in this site so they come back and read more or maybe even join us, who knows?

In other news, why do you care? What made you write up a condescending post, talking to me like I am some kind of moron that doesn't know what day or year it is? Is that how you make yourself feel important? All I did was say thank you to someone, where is the harm in that?
 
Old 05-25-2013, 03:11 PM   #15
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by sunlitlaz View Post
What made you write up a condescending post, talking to me like I am some kind of moron that doesn't know what day or year it is? Is that how you make yourself feel important? All I did was say thank you to someone, where is the harm in that?
Like there is no harm in saying thanks (also see http://www.linuxquestions.org/questi....php?p=1053288) there is no harm in a fellow LQ member pointing out, in a friendly way I might add, to a fellow LQ member that a thread is kind of old. Nothing condescending there. BTW last time D_H logged in was about a year ago.
 
  


Reply


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
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 05:33 AM
Exact HD-to-HD copy samtheviking Fedora 7 02-25-2005 06:32 PM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 11:32 PM
what exact does DNS and NFS do. Do I actually need them? babyboss Linux - Networking 1 09-21-2004 09:32 PM
grep [exact phrase] chrisfirestar Linux - General 2 02-09-2004 05:30 AM

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

All times are GMT -5. The time now is 03:54 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