# How to find all files/headings that are linked to a selection of files?

**URL:** <https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575>\
**Category:** How To\
**Created:** [May 11, 2021, 8:35am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575 "2021-05-11T08:35:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [May 11, 2021, 8:35am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/1 "2021-05-11T08:35:58Z")

</div>

Can I retrieve all files/headings that have links to the files `football` AND `books`, for example, to retrieve all football-related books in my collection?

- The backlinks buffer basically does the same thing, but only with regard to one – namely the currently displayed – file.
- I know I can search by tags, but I only use tags for very broad categories of files (e.g. `private` vs. `work`) and I’m using links for everything else.
- I also know I can use `deft` for searching, but I feel this isn’t a very clever way since it’s not taking advantage of all the network information contained in the collection.

Given that I have all these nice semantic “labels” in the form of links, it would be very powerful to be able to perform searches like the toy example above. I would imagine the necessary information should be contained in the database since basically the same thing happens for the backlinks buffer. I was surprised I could not find any information for how to do this. Is there a way? This would be incredibly powerful.

By the way, thanks again for this awesome package! 🙂

---

<div class="post-metadata">

**Author:** ![mash](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@mash](https://org-roam.discourse.group/u/mash)\
**Post date:** [May 11, 2021, 12:58pm UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/2 "2021-05-11T12:58:39Z")

</div>

Can you be more clear on what you’re trying to do.

I sounds like all you need to do is add notes for football and books, then link each other note you want to show up in the org-roam backlinks buffer to either of those two notes (or more). A note does not have to have text, I have lots of blank notes that are used to connect ideas, basically a tagging or category system, so football and books could be blank targets that act as categories or tags to organize other notes.

If you’re looking for search then deft, or `grep` are what you might use. Otherwise you’re looking for search packages that work with org-mode, outside of the scope of org-roam.

---

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [May 11, 2021, 1:17pm UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/3 "2021-05-11T13:17:23Z")

</div>

The thing is that I can use the backlinks buffer of `football` to find all notes that are related to football. I would want to limit these to notes that are _also_ related to `books`, however.

I think some people are already using tags for something like this one way or another: e.g., 'show me all notes that are tagged as both `football` and `book`'. I would like to do the same thing based on links.

I don’t think this would be possible with what you are describing, but I’m not entirely sure whether I understood you correctly.

---

<div class="post-metadata">

**Author:** ![zaeph](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/zaeph/32/764_2.png) [@zaeph](https://org-roam.discourse.group/u/zaeph)\
**Post date:** [May 12, 2021, 1:54pm UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/4 "2021-05-12T13:54:14Z")

</div>

Advanced filtering and queries is something that we’re working on for v2. When the database is a little more fixated, we’ll be able to get started on this.

---

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [May 12, 2021, 4:25pm UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/5 "2021-05-12T16:25:01Z")

</div>

Cool, I’m looking forward to it, thanks 🙂

---

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [May 24, 2021, 8:15am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/6 "2021-05-24T08:15:40Z")

</div>

One more thought about this: what I have in mind is basically similar to what you can do in org-agenda.

Quoting from the [org-mode documentation](https://orgmode.org/manual/Search-view.html):

> The search string ‘+computer +wifi -ethernet -{8.11[bg]}’ matches note entries that contain the keywords ‘computer’ and ‘wifi’, but not the keyword ‘ethernet’, and which are also not matched by the regular expression ‘8.11[bg]’, meaning to exclude both ‘8.11b’ and ‘8.11g’.

I think I read some time that you are considering to use the org mode tags system for tagging headings. Maybe – under the hood – links could be considered as tags for the current heading. The underlying way of querying might then still be different from what’s behind org-agenda, but at least there would be a well-established interface for querying stuff and maybe also some code that could be re-used?

Having said that, the basic unit for links/tags/queries should probably be headings rather than files. Which is what you’re moving towards anyway in the new version I think, right?

---

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [May 27, 2021, 2:53pm UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/7 "2021-05-27T14:53:22Z")

</div>

I’ve had a quick look into the SQLite database. I don’t know much about these things, but it seems like this should be pretty easy to implement since all the information is available in the DB.

For example, I used the following query to retrieve the file names of all notes that are related (target of links) to notes that have `embedding` **and** `change` in their file names:

```sql
SELECT DISTINCT(source)
FROM links
WHERE dest LIKE "%embedding%"
INTERSECT
SELECT DISTINCT(source)
FROM links
WHERE dest LIKE "%change%"

```

This is just quick and dirty and far from perfect, of course. But maybe this can be seen as a proof of concept for what I am looking for. And it is already really useful for me even like this.

---

<div class="post-metadata">

**Author:** ![KirillR](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@KirillR](https://org-roam.discourse.group/u/KirillR)\
**Post date:** [July 9, 2021, 7:25am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/8 "2021-07-09T07:25:47Z")

</div>

This feature would be amazing!

Linking [Designing a query language](https://org-roam.discourse.group/t/designing-a-query-language/1075) due to similarity.

I’m using tags + [org-ql](https://github.com/alphapapa/org-ql) extensively in my workflows. If I could filter nodes by where they are referenced (or/and by what they reference), I’d ditch the tags and use that “graph lookup”. Including most or all of my org-agenda queries.

* * *

Is there any kind of code design ideas related to that? I’m a horrible Emacs Lisp programmer, but I’m excited to at least make some dirty experiments. I’m pretty fluent with SQL though.

@zaeph let us know if it’s something you guys discussed or maybe played with.

---

<div class="post-metadata">

**Author:** ![KirillR](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@KirillR](https://org-roam.discourse.group/u/KirillR)\
**Post date:** [July 22, 2021, 5:47am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/9 "2021-07-22T05:47:31Z")

</div>

@jethro I’ve been wondering if it’s somehow related to the `org-roam-agenda` you’ve had in mind?

---

<div class="post-metadata">

**Author:** ![wuqui](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@wuqui](https://org-roam.discourse.group/u/wuqui)\
**Post date:** [July 22, 2021, 7:00am UTC](https://org-roam.discourse.group/t/how-to-find-all-files-headings-that-are-linked-to-a-selection-of-files/1575/10 "2021-07-22T07:00:56Z")

</div>

Logseq already has nice query functionality: [https://logseq.github.io/#/page/queries](https://logseq.github.io/#/page/queries).

As far as I know, they also use this query system for the agenda functionalities they offer.

I also really like `org-ql` btw. Maybe that could serve as inspiration for a graph query language.
