LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-12-2017, 11:05 AM   #1
pico77
LQ Newbie
 
Registered: Oct 2017
Location: Southern Italy
Distribution: ubuntu 14.04
Posts: 3

Rep: Reputation: Disabled
Complex file search via command line


Dear Users

I have pretty basic usage of the common 'find' and 'locate' commands which I prefer over other tools for searching files/folders.

I know there are indexing software like recoll or beagle, but they put out too many results. I name files/dirs in a very structured way: the typical file/directory name in my system is

2017_10_12_abc_def_ghi

it starts with an important date (usually the one I started/finished to work on the file), "abc", "def", "ghi" are descriptors/keywords that tell me about the content according to some hierarchy of mine. Let's say that any string separated by underscores is a Keyword/descriptor in my naming scheme.

The problem is that I'd like to construct a script (maybe based on find/locate) that does the following: in a given path it looks recursively to find all files/dirs whose names descriptors contain a list of possible strings

For example the following
Code:
command_name   path_name    "ab"  "hi"
should look recursively into 'path_name' and search for all files/dirs that have keywords containing at least one of the two srings (that is "ab" or "hi"). So for instance such a command should give

2017_10_12_abc_def_ghi

as a result, but not

2017_10_12_cba

I tried to construct such a thing with find/locate commands, but I might be too basic to come up which something working. Wha it is most difficult to me is that the list of possible keaywords over which to look should be of variable length.

Does anybody have some idea if this is possible to do?

Regards
Pierre
 
Old 10-12-2017, 01:43 PM   #2
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
second code block here:
http://mywiki.wooledge.org/UsingFind...based_on_names

Quote:
Wha it is most difficult to me is that the list of possible keaywords over which to look should be of variable length.
I don't understand that.
 
Old 10-12-2017, 01:50 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,398

Rep: Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172
https://github.com/junegunn/fzf
 
Old 10-12-2017, 02:57 PM   #4
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 387

Rep: Reputation: 108Reputation: 108
Quote:
Does anybody have some idea if this is possible to do?
With perl it is quite simple to do. You need an actual text processing scripting language such as perl for this. Perl has its own find function which will find all filenames recursively from a starting point and call a function that you specify for each file name. You would use this function as a filter. You would pass a list of strings to match as an argument to the script which will be used by this filter function. If the current filename matched one of these strings you would add it to an output list. When the find function was finished scanning you would output this list.

The script you write will accept a directory name as the first argument and a variable number of strings to be used as filters. The directory will be the starting point for the find function while the strings will all be added to a global variable list which would be referenced by the filter function.

http://perldoc.perl.org/File/Find.html

You will have to learn some perl to do this but with an actual scripting language this is quite straightforward.
 
Old 10-13-2017, 01:27 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,400
Blog Entries: 9

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
and why is
Code:
find /path -name '*ab*hi*'
not sufficient?
 
Old 10-13-2017, 01:53 PM   #6
pico77
LQ Newbie
 
Registered: Oct 2017
Location: Southern Italy
Distribution: ubuntu 14.04
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
this is very interesting. I Installed it via github, but I need to understand how to use it.

thanks for this.

P
 
Old 10-13-2017, 01:56 PM   #7
pico77
LQ Newbie
 
Registered: Oct 2017
Location: Southern Italy
Distribution: ubuntu 14.04
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
and why is
Code:
find /path -name '*ab*hi*'
not sufficient?
yes, in principle yes, but the point is that this would find relevant items only if the order of the keaywords is correct. In other words

Code:
find /path -name '*ab*hi*'
will only find items where 'ab' comes before 'hi'.

Thanks anyway
P
 
Old 10-13-2017, 02:16 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,400
Blog Entries: 9

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
ok, maybe like this:
Code:
find /path -name '*ab*' -a -name '*hi*'
 
Old 10-17-2017, 10:43 PM   #9
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 387

Rep: Reputation: 108Reputation: 108
The OP was looking for some sort of script to avoid typing a complex command hundreds of times. The following bash script builds a variable length find command adding -o -name for each parameter past 2.

Code:
#!/bin/bash

FIND="find $1 -name '*$2*'"

for item in "${@:3}" ; do
  FIND="$FIND -o -name '*$item*'"
done

#echo $FIND
eval $FIND
With echo $FIND un-commented for debugging.

Code:
$ ./search.sh /home/pico ab hi mr ty
find /home/pico -name '*ab*' -o -name '*hi*' -o -name '*mr*' -o -name '*ty*'
This will greatly cut down on the keystrokes required.

Last edited by tofino_surfer; 10-17-2017 at 10:46 PM.
 
Old 10-17-2017, 10:57 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,479

Rep: Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095
I like locate with regex - much kinder on the machine. Especially my poor old spinning disk.
Code:
locate --regex "$PWD.*(ab|hi)"

Last edited by syg00; 10-18-2017 at 12:56 AM. Reason: closing quotes
 
1 members found this post helpful.
Old 10-18-2017, 04:00 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
Yes, I think you won't find better than that locate command with --regex. But you can also use ls -lR <dir> | grep pattern instead of find, it will be much faster.
 
Old 10-18-2017, 11:35 AM   #12
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,400
Blog Entries: 9

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
^ doesn't locate find only files that have been indexed in its database?
 
Old 10-18-2017, 06:27 PM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,479

Rep: Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095Reputation: 3095
Sure.
Some some distros run updatedb daily, so that might suffice for most folks. Me, I run it when I feel the need - usually less than once a week.
 
  


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
ssh complex command line [at least for me... :)] vjunior1981 Linux - Newbie 7 09-17-2016 11:55 AM
How to search all sub-directories for a file or command line in a file in Ubuntu? tearsforhari Linux - Newbie 3 05-11-2015 05:25 PM
[SOLVED] Command line search in file for < date jdgr Linux - General 6 04-11-2013 09:35 AM
Download 'complex' links via command line alphaniner Linux - General 3 02-06-2010 03:44 PM
LXer: A utility for sending complex email messages from the command line LXer Syndicated Linux News 0 06-06-2008 10:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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