I have 2 files in my current directory : /home/tushar/Desktop
file1.txt
file4.txt
So when I hit command:
1. ls file{1..4}.txt
Output:
ls: cannot access 'file2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
file1.txt file4.txt
Now I redirect both stderr and stdout to a file res.txt using :
2. ls file{1..4}.txt 1>res.txt 2>res.txt
res.txt :
file1.txt
file4.txt
ile2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
Above, we can see that the some content of the stderr got omitted in the file res.txt.
Now I changed my command to:
3. ls file{1..4}.txt 1>res.txt 2>&1
res.txt :
ls: cannot access 'file2.txt': No such file or directory
ls: cannot access 'file3.txt': No such file or directory
file1.txt
file4.txt
Both above commands are exactly same except in 2nd I redirected stderr where stdout is redirecting using file descriptor(&1).
Now I know I should use a command which opens res.txt in appending mode as given below :
4. ls file{1..4}.txt 1>>res.txt 2>&1
OR
5. ls file{1..4}.txt &> res.txt
but my concerns are:
Why the results of cmd 2 and cmd 3 are different ?
Why cmd 3 derived correct output even without using >> symbol but not cmd 2 ?