4

How do I find a file on my harddisk that starts with io_file?

I tried:

grep -r io_file*` 

and

find -name io_file*

find did not return anything whereas grep seems to take ages without any results.

What am I doing wrong?

3
  • 1
    grep and find are not intended for the same thing.
    – Tom
    Aug 11, 2009 at 19:53
  • 1
    (not the same Tom as the poster)
    – Tom
    Aug 11, 2009 at 19:54
  • With all due respect to the true find wizardry shown in the other answers, an alternative is to install ack. This is a Perl tool that has a simpler interface and encompasses both find's and grep's major functionalities.
    – Yuval F
    Aug 11, 2009 at 20:52

8 Answers 8

11

find / -name 'io_file*'

The first parameter will specify where the search should start. / means your entire hard drive. To search in only the current directory (and subdirectories), use: find .

The search string must be quoted if it contains a shell metacharacter, such as an asterisk. Otherwise, it will be parsed by the shell and never seen by find.

5
  • 1
    Not entirely true. The search string will only be expanded if there is a match, otherwise it is left unexpanded. Try "echo thisisnotafile*", for example.
    – JesperE
    Aug 11, 2009 at 19:48
  • 1
    Jesper: is that shell-dependent behavior? The terminal I'm working from uses tcsh, and quoting makes a difference even for filenames which aren't expandable.
    – John Millikin
    Aug 11, 2009 at 19:58
  • Thanks for that. And how can I say that I wanna look in the current directory AS WELL AS all subdirectories?
    – Tom
    Aug 11, 2009 at 20:00
  • Just change the '/' for '.' (without quotes) (Again, not the poster)
    – Tom
    Aug 11, 2009 at 20:16
  • @John Millikin: indeed, bash and tcsh have a different behavior. Aug 11, 2009 at 23:09
7
find / -name 'io_file*' -type f 2>/dev/null



   /: Start looking from root directory

   -type f: Only search for regular files

    2>/dev/null: Redirect errors to /dev/null. (handy if you are not root and
                 not interested in all the "access denied" messages)

Keep in mind that grep and find do different things.

grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

find - search for files in a directory hierarchy

3
  • -name pattern Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.
    – Tom
    Aug 11, 2009 at 19:51
  • thats from the find man page
    – Tom
    Aug 11, 2009 at 19:51
  • @jcolebrand nope
    – Tom
    Sep 4, 2011 at 17:59
4

IMO, using find is slow for an entire harddrive search (it will also show you a lot of permission errors when accessing files you do not own). If possible, use locate:

locate -b 'io_file*'

You'll probably have to re-index if the file is newer than 24h (normally it sets a daily cronjob):

sudo updatedb
2
find / -name 'io_file*'
2

The command

# set -x

will print commands after shell expansion so you can see what is going on

# find . -name letter.*
+ find . -name letter.rtf

...and now you can see that the shell is expanding the argument to name into letter.rtf, as my current driectory contains a file called letter.rtf that matches the pattern.

Quoting the pattern (double quotes work in this case also) will stop the shell expansion

# find . -name 'letter.*'
+ find . -name 'letter.*'

To turn it off, use

# set +x

You can also use -x on a shebang line to see what shell scripts are doing which can greatly aid debugging.

#!/bin/bash -x
0

The shell expands the wildcards and passes the result to grep, so you need to either escape the * or use quotes:

find / -name io_file\*
find / -name 'io_file*'
find / -name "io_file*"

All three of these do the same thing on my Linux box.

0

find -name io_file* will search in the current directory and below only. If you want to search the entire harddrive, you need to specify the path: find / -name io_file*. Same thing goes with grep -r; it will only search in the current directory and below.

The find utility is very handy, but it may be a little counterintuitive to use. My recommendation is that you read the find man-page (man find), it is well worth the invested time.

0

Because you didn't give grep a file, it is operating on standard input and waiting for you to type something. It's the wrong command anyway. As for find you need a directory to start from and have to put io_file* in quotation marks, otherwise it gets expanded by the shell:

find / -name "io_file*"

You can also use locate "io_file*" which is faster but may be inaccurate.

You must log in to answer this question.