0

I'm trying to learn shell commands. I know ls >output.txt saves the output to output.txt. However, what exactly does ls -z >output.txt do? In my book, it says it does Not save the output to output.txt. If this is true, where Does it save / print it? Also, is -z what causes it to not save it?

Lastly, what does ls -z 2>output.txt do? I know 2 refers to stderr (so the standard error). Does this mean it saves the error (if any) of ls in output.txt? If yes, where does the stdout get printed / saved? And what does the -z mean in this case?

Thanks in advance!

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • Is it `z` OR caps `Z`? – Am_I_Helpful Feb 26 '16 at 05:12
  • If you redirect `stderr`, then `stdout` goes, well, unredirected, that is goes to your terminal (or wherever you execute the command). – user3159253 Feb 26 '16 at 05:13
  • 1
    Putting `>output.txt` at the end of any command will save its standard output in the file. If the command doesn't produce any output, the file will be empty. – Barmar Feb 26 '16 at 05:14
  • 1
    According to my manual, there's no `-z` option to `ls`. There's a `-Z` option, but it's only useful on SELinux. – Barmar Feb 26 '16 at 05:17
  • Also, `ls` in my system (Linux, coreutils-8.22) doesn't support `-z` at all, while `-Z` causes printing SELinux context of each file. So perhaps your book describes a non-unix system and theoretically it might handle its commands quite differently from what is called 'shell' – user3159253 Feb 26 '16 at 05:17
  • So if you want to redirect both `ls -z >output.txt 2>error.txt`. Perhaps you book is using `-z` which is generally not a `ls` option to generate some error content. – Diego Torres Milano Feb 26 '16 at 06:06
  • 1
    Possible duplicate of [What does the 2> mean on the Unix command-line?](http://stackoverflow.com/questions/19108895/what-does-the-2-mean-on-the-unix-command-line) – miken32 Feb 26 '16 at 22:05

1 Answers1

1

There is no option -z for ls on Linux. So let's see what happens:

$ LANG=C ls -z  > /tmp/x
ls: invalid option -- 'z'
Try 'ls --help' for more information.

The error message goes to STDERR which is connected to the terminal. The standard output (which is empty) is redirected to /tmp/x so we get an empty file.

$ LANG=C ls -z  2> /tmp/x

In this second scenario STDOUT is connected to the terminal, however there is no output. The error message which got sent to STDERR lands in /tmp/x:

$ cat /tmp/x
ls: invalid option -- 'z'
Try 'ls --help' for more information.
neuhaus
  • 3,886
  • 1
  • 10
  • 27