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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-25-2012, 12:35 PM
|
#1
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Rep:
|
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.
|
|
|
08-25-2012, 12:40 PM
|
#2
|
Member
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128
Rep:
|
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
Code:
read input
find /home/manoj -type f -regex "^.+${input}$" -print
|
Last edited by byannoni; 08-25-2012 at 01:07 PM.
|
|
|
08-25-2012, 01:01 PM
|
#3
|
Member
Registered: Jun 2012
Location: Missouri, USA
Posts: 236
Rep:
|
@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
|
|
|
08-25-2012, 02:18 PM
|
#4
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
08-25-2012, 02:26 PM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by rosehosting.com
@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
|
|
|
08-25-2012, 02:34 PM
|
#6
|
Member
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128
Rep:
|
Quote:
Originally Posted by David the H.
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.
|
|
|
08-25-2012, 02:34 PM
|
#7
|
Member
Registered: Jun 2012
Location: Missouri, USA
Posts: 236
Rep:
|
Quote:
Originally Posted by David the H.
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.
|
|
|
08-27-2012, 09:23 AM
|
#8
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Original Poster
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 04:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|