DEV Community

Douglas R Andreani
Douglas R Andreani

Posted on • Updated on

File search (and more) with `find`

Being able to use the command line is a productivity booster, all system operations that can be done with a mouse and keyboard on fancy GUI applications are doable ove CLI.

The find command is impressive by its versatility, for example, you can lookup for a file that you "lost" on your hard-drive.

find / -name mylostfile

by steps, the first argument to the file program is the initial path for search (recursevely, "/" for root):

the -name parameter specifies the file name that you are looking for.

BONUS: you can use wildcards in the -name parameters

find / -name "*.jpg"

This basic functionality, find already is worth a ton, but just looking for a file is just the tip of this Titanic killer iceberg.

The exec parameter

Here is where find shines, you can execute any command on the list of the files that you found, just replace the file name with {}

find . -name "*.mp4" -exec ffmpeg -i {} -r 30 {.}.avi \;

again step by step:

-name "*.mp4" search for any file with the mp4 extension

-exec ffmpeg -i {} -r 30 {.}.avi \; call ffpmeg to perform a operation, keeping a 30fps on a video file and converting it to avi.

{} current file.

{.} current file without extension.

PS:: the semi-colon in the end of the command is uterly important, you can find more details at: file man file

ffmpeg details: ffmpeg


Do you like using the terminal? Share your experiences with find in the comments and we can learn togheter!

Top comments (1)

Collapse
 
bdmorin profile image
Brian • Edited

Have you looked at ripgrep?
github.com/BurntSushi/ripgrep

# time find ./ -type f -exec fgrep -HIi "perl" {} \; > /dev/null
5.57s user 8.48s system 82% cpu 17.034 total

Same command:
# time rg perl > /dev/null
0.08s user 0.17s system 587% cpu 0.043 total


Similar searchers:
# time ack perl > /dev/null  
0.45s user 0.21s system 99% cpu 0.664 total

# time ag perl > /dev/null
0.10s user 0.33s system 308% cpu 0.137 total

ripgrep is amazing.