Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
1  
grep and find are not intended for the same thing. –  Tom Aug 11 '09 at 19:53
1  
(not the same Tom as the poster) –  Tom Aug 11 '09 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 '09 at 20:52
add comment

migrated from stackoverflow.com Aug 11 '09 at 21:54

This question came from our site for professional and enthusiast programmers.

8 Answers

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.

share|improve this answer
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 '09 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 '09 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 '09 at 20:00
    
Just change the '/' for '.' (without quotes) (Again, not the poster) –  Tom Aug 11 '09 at 20:16
    
@John Millikin: indeed, bash and tcsh have a different behavior. –  Bastien Léonard Aug 11 '09 at 23:09
add comment
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

share|improve this answer
    
-name pattern Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell. –  Tom Aug 11 '09 at 19:51
    
thats from the find man page –  Tom Aug 11 '09 at 19:51
    
@jcolebrand nope –  Tom Sep 4 '11 at 17:59
add comment

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
share|improve this answer
add comment
find / -name 'io_file*'
share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.