Consider the following example from [bedtools intersect help page][1]. Say, there are 4 bed files as follows


    $ cat query.bed
    chr1  1   20
    chr1  40  45
    chr1  70  90
    chr1  105 120
    chr2  1   20
    chr2  40  45
    chr2  70  90
    chr2  105 120
    chr3  1   20
    chr3  40  45
    chr3  70  90
    chr3  105 120
    chr3  150 200
    chr4  10  20

 --

    $ cat d1.bed
    chr1  5   25
    chr1  65  75
    chr1  95  100
    chr2  5   25
    chr2  65  75
    chr2  95  100
    chr3  5   25
    chr3  65  75
    chr3  95  100

 --


    $ cat d2.bed
    chr1  40  50
    chr1  110 125
    chr2  40  50
    chr2  110 125
    chr3  40  50
    chr3  110 125

 --


    $ cat d3.bed
    chr1  85  115
    chr2  85  115
    chr3  85  115

If we use bedtools like this

    $ bedtools intersect -a query.bed \
        -b d1.bed d2.bed d3.bed
    chr1  5   20
    chr1  40  45
    chr1  70  75
    chr1  85  90
    chr1  110 120
    chr1  105 115
    chr2  5   20
    chr2  40  45
    chr2  70  75
    chr2  85  90
    chr2  110 120
    chr2  105 115
    chr3  5   20
    chr3  40  45
    chr3  70  75
    chr3  85  90
    chr3  110 120
    chr3  105 115

It reports the interval for e.g.  `chr1  5   20` , however, I don't want that to be reported, because it is not present in  `d3.bed`. I know that it is being reported because it is overlapping in `query.bed` and `d1.bed`. But my requirement is different. How can I tweak `bedtools` to get ONLY those records which are overlapping across all files.

  [1]: https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html
Try the following

    bedtools multiinter -i *.bed > test1
    files="$(ls *.bed|wc -l)"
    awk  -v OFS='\t' -v files=$files '$4==files' test1 |cut -f 1-3 > test2

What it does:

    bedtools multiinter -i *.bed > test1 # use bedtools multi intersect to get statistics on intersections of all bed files
    files="$(ls *.bed|wc -l)" # make a variable called files that is the number of bed files in the directory
    awk  -v OFS='\t' -v files=$files '$4==files' test1 |cut -f 1-3 > test2 # get only regions that are found in all of the files (column 4 is num, which is the number of input files that have that region)
  
test2 is the file that contains regions found in all files assuming that the only bed files in the directory are query.bed, d1.bed, d2.bed, and d3.bed
