Home arrow Guides arrow Find files from command line in FreeBSD
Find files from command line in FreeBSD PDF
Thursday, 22 March 2012
Here is a small tutorial regarding how to find files from FreeBSD's shell.

Find a file by using an exact name

  find ./ -name info.txt

  ./ will search the current directory and subdirectories (If you want to search on all mounted file systems use / instead).

A typical find command will look like this (when you do not know the full name of your filename):

  find ./ -name "*part_of_filename*"

Where part_of_filename is a part of filename you are searching for (if you don't know the full/exact name of the file name you are searching). Don't forget to enclose part_of_filneme with * and then with " chars.

Faster searches

Let's say we want to search all txt files for ones that contains linux pattern in file name. This would be our search:

  find ./ -name '*.txt' | grep linux

still there's a faster way:

  find ./ -name '*.txt' -exec grep -li 'linux' {} \;

and a faster way:

  find ./ -name '*.txt' | xargs grep -li 'linux'

Using find and xargs could create problems if you have white spaces in filenames. To fix that problem we will use ASCII NUL character (instead of regular space character) to separate file names:

  find ./ -print0 -name "*" | xargs -0 grep -rli 'linuxl'

(previous command uses find -print0 ... |  xargs -0 ... to use ASCII NUL character)

Search in content of files

To find a file that contains a string/pattern (string_to_find in our case) use this command:

  find /path -exec grep -l “string_to_find” {} \;

There's also a faster search using grep:

  grep -Flr --include "*" 'mini_httpd' ./                         # search all files for mini_httpd pattern

If we want to search only text files for that pattern:

  grep -Flr --include "*.txt" 'mini_httpd' ./

Find all files that were modified today

To do that we use find command with -mtime parameter:

  find ./ -mtime 0

 where: 0 is for today, 1 is for yesterday and so on (will search for file modified only that day and not a range of days).

If we want to see all .jpg files that were modified today we could use:

  find ./ -name "*.jpg" -mtime 1

./ tells find to search in current directory and subdirectories. If we want to search in a different path just replace ./ with your own path.

If you want to search all files modified three days ago and too days ago and today use:

  find ./ -mtime -3

For more info regarding -mtime run man find and look for -atime option to see supported time units.

Last Updated ( Thursday, 22 March 2012 )
< Prev   Next >

Other BSD Systems





Best BSD firewall?