Let's break it down.
"2" is "stderr" a.k.a. "standard error"
"1" is "stdout" a.k.a. "standard output"
">" can be thought of as "sent to"
"&" can be thought of as "the same place as"
By default, "1" is normally sent to your screen. So is "2".
To send "standard output" to a file named "dirlist", you would do this:
1>dirlist Read this as:
Code:
"standard output" + "sent to" + "dirlist"
You can leave off the leading "1" and it means the same thing, since "1" is assumed if nothing is specified. Therefore,
>dirlist is just a shorthand way of writing
1>dirlist
Looking at the text string "2>&1", this can be read as:
Code:
"standard error" + "sent to" + "the same place as" + "standard output"
Now, order is important.
ls 2>&1 >dirlist is not the same as
ls >dirlist 2>&1
The first case directs 2 into the same place as 1, and
then directs 1 into dirlist. But at the time 2 was sent to the same place as 1, 1 was going to it's default. Your screen. But 2 goes to your screen by default too! So that operation didn't really do anything.
In the second case, 1 was directed to dirlist
first, and
then 2 was directed to the same place as 1. So 2 would then go to dirlist also.
So to capture
both stdout and stderr to a file, first direct 1 to the file, then direct 2 to the same place as 1. e.g.,
ls >dirlist 2>&1