[SOLVED] Search based on similar numbers as file names
Linux - NewbieThis 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.
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.
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.
@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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.