diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 2d63dd88f121e0cbcb4effa498c56530aa6223fb..c71882a731778e9c94daeff74739e3d484f6c94b 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -363,8 +363,8 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
 			next->size = buf.st_size;
 		if (node)
 			node->next = next;
-		if (!head)
-			head = node;
+		else
+			head = next;
 	}
 	*headp = head;