0

Is there any way in a Linux terminal to refer to a file in a very deep directory structure without specifying the exactly path?

For example, if you know you have a file called qstr.js somewhere 3-4 directories deep (e.g. qtools\modules\version001\tests\qstr.js), is there a way to type something like:

cp qs*

and then have it search and find this file so that you can just TAB the full path and file name?

Git has something like this which allows you to refer to working directory files that are deep in a directory.

2
  • what would you expect to happen if there was a sibling directory anywhere along the way that also contained a file named qs2.dat?
    – Jeff Schaller
    Commented May 4, 2020 at 15:13
  • Similar to when you start typing a file and there are multiple files that start with those letters, i.e. they are all displayed for you to choose from. But often I find myself in the situation where I know the name of a file which is unique, but it is just burried in a directory structure and so difficult to reference. Commented May 5, 2020 at 7:58

3 Answers 3

1

There's globstar (**):

/usr $ shopt -s globstar
/usr $ cp **/firefox*<Tab>
firefox-developer-edition               firefox-developer-edition.png           firefox-developer-edition.png           firefox-developer-edition-symbolic.svg  firefox.png
firefox-developer-edition/              firefox-developer-edition.png           firefox-developer-edition.png           firefox.png                             firefox.png
firefox                                 firefox-developer-edition.png           firefox-developer-edition.png           firefox.png
[...]
# a few more <Tab>s later
/usr $ cp share/icons/hicolor/384x384/apps/firefox-developer-edition.png 

(The latter behaviour would require menu-complete.)


You can try using fzf ("a commmand-line fuzzy finder"). After installing it, and enabling the completion and key bindings for bash, CtrlT will get you prompt:

> firefxdeskop
  1/722561
> share/applications/firefox-developer-edition.desktop

And pressing Enter will paste the selected path to the command line.

0

I use find alot. I admit I'm probably not using the tool to its full potential, but to just find a file you can do find -name foo. Note the single dash, and it will only search in subdirectories. You can also use find -path to search the whole path. So for example if you only want to find the file "bar.txt" in the folder "foo" you can do find -path */foo/bar.txt.

0

In addition to @Simon's answer, the updatedb and locate commands will be of interest to you. Depending on your distribution, you may need to install the mlocate package first.

For the most part, you can use find and locate interchangeably. The only difference between them is how they achieve their goal internally, which gives both of them their own inherent pros and cons. You can learn more by reading this great answer to a similar question.

You must log in to answer this question.

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