47

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?

1

3 Answers 3

54

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

2
  • 1
    This should be accepted. Commented Feb 26, 2022 at 6:41
  • How does redirection work if a process is running continuously? Does the output get periodically written to the file? Or does it buffer everything into RAM and only flush when the process is stopped with CTRL+C? Commented May 21, 2024 at 21:33
25

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.