samtools sort: fail to open "****.sorted.bam.tmp.1020.bam": Too many open files
increase the RAM allowance for samtools sort (with -m 1500M or -m 2G, for example) such that there are fewer (larger) temp files to merge
command :
samtools sort -m 2G -n in.bam -o in.sorted.bam