LinuxQuestions.org
Review your favorite Linux distribution.
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 08-25-2012, 01:35 PM   #1
manojb
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Rep: Reputation: Disabled
Lightbulb Search based on similar numbers as file names


Hi friends,

I am having trouble in using find command. The situation is as follows:

I have 2 different files. Filenames of two different files are as follows:

file12
file123

file12 is present in /home/manoj/test1 folder and file123 in /home/manoj/test2 folder.

Main issue is - how to search for file12 or file123 from home path ?
My search is based on input string with integers.

For example, if I search with file name like 12, it should return file12 and if i provide my input string as 123, it should return file123.

Please suggest.
 
Old 08-25-2012, 01:40 PM   #2
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
Code:
find /home/manoj -type f -regex 'file\d\d\d?' -print
Edit:
Just realized you want an input string:
Code:
read input
find /home/manoj -type f -regex "file${input}" -print

Edit2:
Sorry, forgot the anchors. This is better (see post #3):
Quote:
Originally Posted by rosehosting.com View Post
Code:
read input
find /home/manoj -type f -regex "^.+${input}$" -print

Last edited by byannoni; 08-25-2012 at 02:07 PM.
 
Old 08-25-2012, 02:01 PM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
@byannoni, your find command is not working as expected here (centos 6.2 and gentoo). It's weird why is like that but the following should work fine:

Code:
read input
find /home/manoj -type f -regex "^.+${input}$" -print
HTH
 
Old 08-25-2012, 03:18 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
find is a tricky command with lots of options, and it takes some time to learn. Start with these links:

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Note that the -name option uses globbing (not exactly the shell globbing in the link, but broadly the same), and only tries to match the filename itself, not the path in front of it. The -regex option used above, OTOH, uses more powerful regular expressions, and attempts to match the entire input filepath.

Assuming the filenames are exactly as posted, with the number occurring at the end of the name, globbing matches could look like this:
Code:
find /home/manoj -type f -name "*[^0-9]12" -print
find /home/manoj -type f -name "*[^0-9]123" -print
The [^0-9] ensures that the expression matches the number exactly, and won't also match "file 212", and similar.

find has several other matching features. Read the man page for them all.
 
1 members found this post helpful.
Old 08-25-2012, 03:26 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Quote:
Originally Posted by rosehosting.com View Post
@byannoni, your find command is not working as expected here (centos 6.2 and gentoo). It's weird why is like that but the following should work fine:

Code:
read input
find /home/manoj -type f -regex "^.+${input}$" -print
HTH
How exactly is it "not working as expected"? How can we tell you what's wrong without knowing what happened?

You do realize that the read command is there to ask for user input first, right? So you type in "12" after running the first command, and that's inserted into the find command for searching.

Note also that the regex here really should have a number limiter too. Try this:

Code:
read -p "Enter the number to search for: " input
find /home/manoj -type f -regex "^.+[^0-9]${input}$" -print
 
Old 08-25-2012, 03:34 PM   #6
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
Quote:
Originally Posted by David the H. View Post
How exactly is it "not working as expected"? How can we tell you what's wrong without knowing what happened?
The regex will also match names like:
  • badfile12
  • file123bad
  • dontwantthisfile123
because it is not anchored to the beginning and end of the name.
 
Old 08-25-2012, 03:34 PM   #7
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
Quote:
Originally Posted by David the H. View Post
How exactly is it "not working as expected"? How can we tell you what's wrong without knowing what happened?

You do realize that the read command is there to ask for user input first, right? So you type in "12" after running the first command, and that's inserted into the find command for searching.

Note also that the regex here really should have a number limiter too. Try this:

Code:
read -p "Enter the number to search for: " input
find /home/manoj -type f -regex "^.+[^0-9]${input}$" -print
oh, yes I do know what 'read' is . by 'not working as expected' I meant that it is not working at all the find command given by @byannoni.

yes, I agree that it is even more precise with the number delimiter between them.
 
Old 08-27-2012, 10:23 AM   #8
manojb
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
I found this working perfectly:

find /home/manoj -type f -name "*[^0-9]12" -print
find /home/manoj -type f -name "*[^0-9]123" -print

--
Thanks a lot
 
  


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
Browser UI, search file names, download hits? catkin Linux - Software 2 07-10-2012 05:19 PM
How to add leading zeros to numbers for file names rootaccess Linux - General 20 06-16-2012 08:35 PM
Need a script to find/replace numbers with names in 1 file using another as the guide kmkocot Programming 4 07-03-2009 04:30 AM
Trying to create directories based on file names JackieBrown Linux - General 3 05-15-2009 10:13 PM
Command to find similar file names in a directory Ottoguy Linux - Newbie 4 02-02-2006 06:42 AM


All times are GMT -5. The time now is 10:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration