I want to search for a specific file type(.jpeg) in a given path very efficient manner.
I am presently having 2 options
ls- R | grep .jpeg
which one is better one? is there any better approach then this?
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 community
I would use
find /somewhere/on/disk -name "*.jpeg" over
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
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
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" \)
Depends on what you want as a result.
ls -R won't tell you which directory it found the file in, while
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.
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.
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.