Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to search for a specific file type(.jpeg) in a given path very efficient manner.

I am presently having 2 options

  1. ls- R | grep .jpeg
  2. use 'find'

which one is better one? is there any better approach then this?

share|improve this question

migrated from Dec 15 '09 at 11:52

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

I would use find /somewhere/on/disk -name "*.jpeg" over ls.

There is also the locate *.jpeg option which makes use of a database. However, this database is only updated at regular intervals (generally from a cron job) so some file might not have been indexed yet at the time you're invoking the locate command hence producing a less accurate result. You can force the creation or update of this database by invoking the updatedb command.

About find vs ls performance, ls will sort the output which takes time while you don't necessarily need it. Also, ls will output a lot of information which needs to be filtered by grep afterwards.


share|improve this answer
+1 for mentioning locate, which is awesomely fast when it works. – Carl Smotricz Dec 15 '09 at 11:52
Executing ls -f will not sort the output making the list command much faster on big directories. I am not saying that it will be faster than find though... I don't know. – mrucci Feb 13 '10 at 10:20

I've always been a fan of find, there's so much you can do with it:

find /starting/path -type f -name "*.jpeg"

So if I wanted to start under my home directory:

find /home/john -type f -name "*.jpeg"

You may want to look for .jpg and .jpeg (3 letter extensions from the DOS days) as files may be named either way:

find . -type f \( -name "*.jpg" -o -name "*.jpeg" \)
share|improve this answer

Depends on what you want as a result. ls -R won't tell you which directory it found the file in, while find will.

Also, ls -R generates more output, which is subsequently filtered, while find searches specifically for what you wanted. Without having done measurements, I would guess that find is generally more efficient.

share|improve this answer

I usually use

      find . -name *.jpg -print {} \;
share|improve this answer
Why bother with the -print? – Nathan Fellman Dec 15 '09 at 20:22
@NathanFellman: Why not bother with the -print? – Tom Wijsman Jul 16 '12 at 1:55
@Tom: It's more to type, and more to remember. – Nathan Fellman Jul 16 '12 at 6:10

You might also want to match the files in .JPG and .JPEG in capital letters. With find, you could add -name '*.JPG' -name '*.JPEG', or you could pipe find to a grep (yes, it's a bit ugly):

find . -type f | grep "\.[Jj][Pp][Ee]\?[Gg]$"

This will match:

  • foo.jpg
  • bar.jpeg
  • foo.JPG
  • bar.JPEG
share|improve this answer
Use iname to match case insensitively: "find . -type f -iname *.jpg" For extra credit, use "man find" to learn how to do OR. – Randy Orrison Jun 24 '11 at 6:38
@Randy: good to know. – ℝaphink Jun 24 '11 at 10:14

find ./ -name *.jpg -print

share|improve this answer
This won't work. Have you tried it? – innaM Dec 15 '09 at 11:49
@Manni: Yes I have used this command under slackware Linux without any problems. – t0mm13b Dec 15 '09 at 11:51
@Manni, are you saying it won't work because the '*' should be quoted? Maybe tommieb75's shell handles that ok. – Nathan Fellman Dec 15 '09 at 11:57
Yes, Nathan, that's exactly what I was talking about. What strange kind of shell would handle this ok? – innaM Dec 15 '09 at 12:08
@Manni: Under Bash it runs no problems! :) – t0mm13b Dec 17 '09 at 15:21

Install "locate" or "slocate"

Run "updatedb" and "locate \*.jpeg"

updatedb builds a index database for the filenames in your system.

locate will find the file from the database instead of scanning directory by directory.

It is faster that find if you are do searching a lot after each database build. You might want to setup a cron job to run the updatedb command.

This is kind of like Google Desktop Search, but only for searching filename.

share|improve this answer

Find is definitely the best tool for this job.
The output of ls -R will be difficult to parse (the file names are interleaved with the directory names). Besides with ls you will have hard time dealing with names with spaces, whilst find . -name '*.jpeg' -print0 will generate a list separated by null-characters. xargs, grep and others have option to read this kind of input. This means all weird names are preserved.

share|improve this answer

Other than the obvious difference with the pipe, both versions can be seen as equal. The implementation (ls.c vs. find.c taken from busybox) looks quite the same when searching for files. You can also shorten the first one:

ls -R *.jpeg
share|improve this answer

You must log in to answer this question.

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