3

I am running this find command:

find $HOME * -depth -type d -iname *bowtie* | grep "bowtie*-*1[^2]"

My expected out put is:

/home/user/Documents/scripts/bowtie1.3.0-linux-x86_64
/home/user/Documents/scripts/bowtie-1.3.0-linux-x86_64

But it return this:

/home/user/Documents/scripts/bowtie1.3.0-linux-x86_64
/home/user/Documents/scripts/bowtie-1.3.0-linux-x86_64
Documents/scripts/bowtie1.3.0-linux-x86_64
Documents/scripts/bowtie-1.3.0-linux-x86_64

How can I modify the find command to only show the first two results?

0

2 Answers 2

14

You are running find with several search paths that are the same. You are using $HOME and whatever names * generate. The * obviously expands to some directories that, among other things, include the names you are seeking.

Suggestion:

find "$HOME" -type d -name 'bowtie*1.[!2]*'

The above also eliminates your grep by using the -name test more creatively. You will need to quote any patterns that should not be expanded relative to the current directory before invoking find.

The pattern matches names starting with bowtie and then contains 1.n, where n is not 2. In place of [!2], you could use [13-9] to force the match of an integer other than 2, not just any character other than 2.

2
  • 2
    Note that though it's likely not what the OP intended grep "bowtie*-*1[^2]" matches on lines that contain bowti followed by 0 or more es followed by 0 or more -s followed by1 and any character other than 2. So it would match for instance on the second line of a file path such as $'/home/user/foo\nbowti1/whatever/\nBOWTIE-12'. So what your script does is radically different but is likely what the OP needs. Commented Oct 5, 2021 at 18:43
  • 1
    @StéphaneChazelas Yes, I'm aware that the user may have confused globbing patterns and regular expressions. I made sure to describe the pattern I'm actually using though.
    – Kusalananda
    Commented Oct 5, 2021 at 19:17
9

That is because you are passing two arguments to find:

  • Your $HOME directory
  • * which is expanded by the shell to the full list of files and directories in the current directory which, presumably, is your $HOME.

So find will first search through $HOME and return things like /home/user/foo and then it will search through the list of files and directories you gave it which, since they are expanded by the shell, are paths relative to your current directory and therefore do not have the leading /home/user, and so it returns things like foo.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .