1

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?

0

9 Answers 9

6

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.

Reference:

2
  • +1 for mentioning locate, which is awesomely fast when it works. Dec 15, 2009 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, 2010 at 10:20
3

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" \)
2

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.

1

I usually use

      find . -name *.jpg -print {} \;
3
  • Why bother with the -print? Dec 15, 2009 at 20:22
  • @NathanFellman: Why not bother with the -print? Jul 16, 2012 at 1:55
  • @Tom: It's more to type, and more to remember. Jul 16, 2012 at 6:10
1

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
2
  • 1
    Use iname to match case insensitively: "find . -type f -iname *.jpg" For extra credit, use "man find" to learn how to do OR. Jun 24, 2011 at 6:38
  • @Randy: good to know.
    – raphink
    Jun 24, 2011 at 10:14
1

find ./ -name *.jpg -print

7
  • This won't work. Have you tried it?
    – innaM
    Dec 15, 2009 at 11:49
  • @Manni: Yes I have used this command under slackware Linux without any problems.
    – t0mm13b
    Dec 15, 2009 at 11:51
  • @Manni, are you saying it won't work because the '*' should be quoted? Maybe tommieb75's shell handles that ok. Dec 15, 2009 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, 2009 at 12:08
  • @Manni: Under Bash it runs no problems! :)
    – t0mm13b
    Dec 17, 2009 at 15:21
0

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.

0

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.

0

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

You must log in to answer this question.

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