Thread: simple bash program using find

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Canada
    Posts
    1

    Question simple bash program using find

    Im working on a small project and Ive looked, what seems like everywhere, for an answer. My question is Im trying to create a bash shell that will take in two variables and search for files that contain both of the listed arguments. Just for the records, im quite new to linux, so if im making rediculously stupid mistakes thats why
    one of the million things ive tried :
    Code:
    find . -name "*$1*" -print | xargs find . -name "*$2*
    I have looked for ways to include both, but I just cant seem to get it to work.
    Code:
    find . \( -name "*$1*" -a -name "*$2*"\)
    but even that doesnt work, im also not to sure if thats how you do AND. I saw it on a website for OR, but they did not describe how to use it properly, and I havent been able to find adequate resources on either.

    Any help would be greatly appreciated.
    Thanks in advance,
    Brett

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > search for files that contain both of the listed arguments.
    So if you're searching for 'foo' and 'bar', then you would match a file called
    foobar.c
    But not files called
    foo.txt
    bar.dat

    Code:
    find . -type f | grep $1 | grep $2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    How about:
    Code:
    find -name *$1* | grep $2
    That works for directorys as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  3. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM