Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:18:18

And you would think them awake, while they were asleep. And We turned them to the right and to the left, while their dog stretched his forelegs at the entrance. If you had looked at them, you would have turned from them in flight and been filled by them with terror.  

And you would have supposed them — had you seen them — awake, that is, conscious, because their eyes were open (ayqāz, ‘awake’, is the plural of yaqiz), though they were asleep (ruqūd is the plural of rāqid). And We caused them to turn over to the right and to the left, lest the earth consume their flesh, and their dog ˹lay˺ stretching its forelegs, his paws, on the threshold, at the opening of the cave: whenever they turned over it would turn over just like them, both during sleep and consciousness. If you had observed them you would have turned away from them in flight and you would have been filled (read la-mulli’ta or lamuli’ta) with awe because of them (read ru‘ban or ru‘uban, ‘awe’): ˹it was˺ through this awe that God protected them from anyone entering upon them.
القرآن:١٨:١٨

وَتَحْسَبُهُمْ أَيْقَاظًا وَهُمْ رُقُودٌ ۚ وَنُقَلِّبُهُمْ ذَاتَ الْيَمِينِ وَذَاتَ الشِّمَالِ ۖ وَكَلْبُهُمْ بَاسِطٌ ذِرَاعَيْهِ بِالْوَصِيدِ ۚ لَوِ اطَّلَعْتَ عَلَيْهِمْ لَوَلَّيْتَ مِنْهُمْ فِرَارًا وَلَمُلِئْتَ مِنْهُمْ رُعْبًا  

{وتحسبهم} لو رأيتهم {أيقاظا} أي منتبهين لأن أعينهم منفتحة، جمع يقظ بكسر القاف {وهم رقود} نيام جمع راقد {ونقلبهم ذات اليمين وذات الشمال} لئلا تأكل الأرض لحومهم {وكلبهم باسط ذراعيه} يديه {بالوصيد} بفناء الكهف وكانوا إذا انقلبوا انقلب هو مثلهم في النوم واليقظة {لو اطلعت عليهم لولَّيت منهم فرارا ولملِّئت} بالتشديد والتخفيف {منهم رعْبا} بسكون العين وضمها منعهم الله بالرعب من دخول أحد عليهم.