45

I am currently doing preparation for my GCSE computing controlled assessment on Linux. I type ls > list and ls >> list into the command line, but it does not do anything. I have googled it but I can't find what it exactly does.

What does:

ls > list

and

ls >> list

do?

slm
  • 369,824
ayy lmao
  • 781

3 Answers3

51

Both redirect stdout to file.

ls > list

If the file exists it'll be replaced.

ls >> list

If the file does not exist it'll be created. If it exists, it'll be appended to the end of the file.

Find out more: IO Redirection

Albert
  • 1,185
23

This:

ls > list

means redirect the output from the ls command to create a new file called list. If the file already exists, replace it.

Whereas

ls >> list

means redirect the output from the ls command and append it to the file called list If the file doesn't exist then create it.


Typically > is used when wiping out an existing file is ok. This often means that outputs continually overwrites a file based on the latest current state of things. For instance every time you test a program you might over-write the previous test output.

Typically >> is used for items such as logging events, parsing or other data processing where data is created or transformed piece by piece into a new form

1

Both commands redirect output of ls command to file with name list. > will create/replace the output file with name list. >> will create (if file list not exists already) or append the file list. Can see the contents of file list using cat list.

Mr. B
  • 23