44

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
  • 363,520
  • 117
  • 767
  • 871
ayy lmao
  • 771
  • 3
  • 8
  • 10
  • [Redirecting output to file](http://unix.stackexchange.com/questions/47823/redirecting-output-to-file) – Costas Dec 02 '14 at 11:51

3 Answers3

49

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,135
  • 3
  • 11
  • 18
22

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

Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
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
  • 1
  • 5