Home > Mobile >  find a file by the suffix substring of true absolute path
find a file by the suffix substring of true absolute path

Time:11-02

I met this question in my interview today.

For example, the file 1.txt, which I want, is in /data/users/xxx/u/a/b/c/1.txt. Right now, I only remember that the absolute path of the file I want has the suffix a/b/c/1.txt. There can be a lot of different 1.txt in my system.

Which shell command should I use to find the one with this pattern.

CodePudding user response:

The -wholename option to find allows you to specify pathname components along with a glob to locate files in your filesystem. You should try and tailor the start directory as much as possible, but you can start with / if you really have no idea where the file could be located.

In that case, you could do:

find / -type f -wholename "*a/b/c/1.txt"

You can also use locate (mlocate) which uses -wholename matching by default. An equivalent search with locate would be:

locate "*/a/b/c/1.txt"

The caveat is that updatedb must have been run since the file was created for it to be in the mlocate.db. Otherwise, the file won't be found with locate. (also note, locate is not installed by default in by all Linux distros)

  • Related