-1

Does anyone know if find command do full disk scan if I apply -mtime +30?

I am worrying that when number of directory grow, it will become deadlock whenever I run find, and trying to find a way to limit number of directory it search, but not sure even define mtime would still search for every directory.

3 Answers 3

4

mtime does not decide where in the filesystem hierarchy you want to search, it is just a test that is applied on the files found to check for their modification time (if greater than 30 minutes) with the current time as the reference.

To control where to search look at the path you are searching (recursively or not). For example, you can limit the (recursive) search only to a certain directory, on a certain filesystem by -xdev, and so on.

Check man find.

1
  • I think you misunderstand what I am asking: find /data -empty -mtime +30 <- does the "search for mtime" job belongs to find or find will ask file system for that? If mtime is handle by find itself, it will need to scan all directory, no matter if mtime is +30 or not, then generate result and filter it, this will cause full disk scan and I am trying to find a way to avoid it. Aug 30, 2016 at 5:30
1

find will scan a directory tree (this is not necessarily a full disk).

By default, find will examine directories to return every file in the hierarchy.

TESTS (such as -mtime) do not modify which files are returned. Unless combined with some ACTION (like -prune or -quit), the mod times of the files won't affect the search space.

The various OPTIONS, TESTS, and ACTIONS are outlined in the man page.

I am worrying that when number of directory grow, it will become deadlock whenever I run find, and trying to find a way to limit number of directory it search

I'm not sure why "deadlock" is a worry. As the files increase, the amount of work find has to do increases as well. But it should always complete.

Unless you have some information about which files in your hierarchy may or may not match, neither find nor the filesystem can help. The only way to print every possible match is to examine every possible file.

Now if you have some information that can limit which ones are possible, you might be able to add in some actions that reduce the work that is done.

4
  • So putting some argument or criteria on find command should help to reduce workload, correct? My worry is that once the number of directories hit certain amount, then find will take days or weeks to search, which at the end of course will be complete, but it will take forever to do that. Aug 30, 2016 at 5:47
  • Yes, that's correct. I occasionally have to run find on hierarchies of more than 200M files (days to execute). Rarely can I limit the space usefully and instead have to just put up with the time such operations take. But when possible, pruning can help speed things up.
    – BowlOfRed
    Aug 30, 2016 at 5:51
  • so from your real experience, find still take days to complete on your 200M files repos? I think I should purge empty directory every night instead of every month. Looks like it will still take ages to complete. Aug 30, 2016 at 5:53
  • @ThomasG.Lau, yes. Although they are on external NFS appliances, not local.
    – BowlOfRed
    Aug 30, 2016 at 5:55
0

What you are looking for is the option -maxdepth. You can limit the number of directories the find command will scan with this option. For example the below command will recurse into only two directories below your current directory

find . -maxdepth 2 -iname '*.txt'

Change it to -maxdepth 1 if you want to scan only the current directory or increment that value if you want to recurse into more directories

From the man page of find:

-maxdepth levels Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

-mindepth levels Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.

It does not matter what value you pass to +mtime, the find will scan the entire disk only if you issue the find command from / and if you use '.' as the first option or if the first option to the find command is / and you have no -maxdepth or -mindepth values mentioned.

For example:

sree@seiko:/ : pwd
/
sree@seiko:/ : find . -iname "*.txt"
sree@seiko:/ :
1
  • not exactly what I am asking, maxdepth is one of the parameter you could use it. I am asking those parameter would help to reduce work from find, or find would still search everything at the first place, then filter result when display it. Aug 30, 2016 at 5:50

You must log in to answer this question.

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