ls dir
screen result:
file_a file_b file_c
but
ls dir > ls.txt
ls.txt content:
file_a
file_b
file_c
why?
ls dir: List the files in the working directory. [1]
ls dir > ls.txt: Most command line programs that display their results do so by sending their results to a facility called standard output. By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used. In this example, the ls command is executed and the results are written in a file named ls.txt. Since the output of ls was redirected to the file, no results appear on the display.[2]
The result of ls dir
is written in short format this is why it appears in one line. ls dir > ls.txt
however writes each folder/file name in a separate line of the text file.
If you would like to print the result of ls dir in that format you can use the long format ls dir -l
.