How to write all file name in a text file and output to a different folder (directory)?

Specify a different file path after the redirection symbol >>.

You currently only provide the file name. The file therefore will be saved in the current directory. Prepend the path where the file should be stored, and it will be stored there.

Example with absolute path /home/user/test/:

ls >> /home/user/test/Out_file.txt

Example with relative path: it will store the file in the folder above the one your in, ex. your in /home/user/test/ and the file will be in /home/user/:

ls >> ../Out_file.txt