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- use
'find'
which one is better one? is there any better approach then this?
|
|
|
I would use There is also the About Reference: |
|||||
|
|
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. Also, ls -R generates more output, which is subsequently filtered, while |
|||
|
|
|
I usually use
|
|||||||
|
|
|
You might also want to match the files in .JPG and .JPEG in capital letters. With find, you could add
This will match:
|
|||||||
|
|
|
|||||||||||
|
|
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. |
|||
|
|
|
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:
|
||||
|
|