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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityHow 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?
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
.
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
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
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
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.
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.
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.