We present an algorithm for finding a system of recurrence relations for the number of permutations of length n that satisfy a certain set of conditions. A rewriting of these relations automatically gives a system of functional equations satisfied by the multivariate generating function that counts permutations by their length and the indices of the corresponding recurrence relations. We propose an approach to describing such equations. In several interesting cases the algorithm recovers and refines, in a unified way, results on -avoiding permutations and permutations containing exactly once, where is any classical, generalized, and distanced pattern of length three.