I am running a query on contentversion to fetch files related to opportunity from Salesforce. However the content version records returned from the query does not tell me what opportunity id is that related to. I know that ContentDocumentLink has a column called LinkedEntityId which would be opportunity id in my case.
Is there a way I can create a formula field on Contentversion object and lookup the LinkedEntityId from the contentDocumentLink? or Is there any other way to achieve this usecase?
ContentDocumentLink is a junction object establishing a many-to-many relationship between ContentDocument (not ContentVersion) and arbitrary sObjects. The only approach you'd have to place linked sObject Ids on ContentVersion objects would be an Apex trigger. It is not possible to do this via a formula field.
However, since the relationship is many-to-many, your code must not assume that each version or document is related to exactly one record of any specific sObject unless you have other code in your Salesforce org enforcing that constraint.