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.
-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:
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)
Answered By – David C. Rankin