-1

This is a question I found in a test:

Run tree from your home directory.
Explain and demonstrate what it does.
Use output redirection to do this.

I don't understand the output redirection part. Could you explain it to me please?

Byte Commander
  • 107,489

2 Answers2

2

tree : tree lists the contents of directories in a tree-like format. It's a really neat and useful program you can use at the command line to view the structure of your file system.

output redirection means store command output somewhere else other than simple printing on terminal . if you want to store tree command output in file then use command

tree > test 

that will redirect output to this file. this command will create a file test and if this already exits then it will replace it't content with tree command output. if you don't want to replace and just want to append then use command

tree >> test

this will append output of tree command to file test. you can give full path of file where you want to store output.

pl_rock
  • 11,297
0

Probably in that context output redirection = redirection of standard output (stdout) to a regular file.

tree >tree_output.txt

However output redirection in general means redirection of one of the default output streams (standard output (stdout) and standard error (stderr), normally both directed to the terminal) to anything that can be considered a file (regular file, stderr if stdout is the stream in question, stdout if stderr is the stream in question, pipe, named pipe (FIFO), block device, character device ...), and it's not limited to the redirection of standard output (stdout) to a regular file like in the example.

So there are lots of possible output redirections;

Assuming for the sake of brevity that the output in question is the standard output (stdout), these are the most common possible redirections:

  • echo foo >file: stdout is redirected to a regular file;
  • echo foo >&2: stdout is redirected to stderr;
  • echo foo | cat: stdout is redirected to a pipe;
  • mkfifo fifo && echo foo >fifo: stdout is redirected to a named pipe (FIFO);
  • echo foo >/dev/sdX: stdout is redirected to a block device (DON'T DO THAT - it could do nothing but messing up the partition table of the block device in question);

More esoteric redirections:

  • echo foo >/dev/tty: stdout is redirected to a character device, which happens to be the current pseudo-terminal;
  • Hitting Ctrl+Alt+F1, logging in, hitting Ctrl+Alt+F7, hitting Ctrl+Alt+T, running echo foo>/dev/tty1 and hitting Ctrl+Alt+F1; stdout is redirected to a character device, which happens to be the first pseudo-terminal;

So output redirection in general means redirection of one of the default output streams (standard output (stdout) and standard error (stderr), normally both directed to the terminal) to anything that can be considered a file;

In order to run tree using output redirection (and actually showing something on the terminal) there are multiple ways:

  • tree >tree_output.txt && cat tree_output.txt: runs tree redirecting stdout to a regular file;
  • tree >&2: runs tree redirecting stdout to stderr;
  • tree | cat: runs tree redirecting stdout to a pipe;
  • mkfifo fifo && tree >fifo: runs tree redirecting stdout to a named pipe;
  • tree >/dev/tty: runs tree redirecting stdout to a character device (the current pseudo-terminal);
  • Hitting Ctrl+Alt+F1, logging in, hitting Ctrl+Alt+F7, hitting Ctrl+Alt+T, running echo foo>/dev/tty1 and hitting Ctrl+Alt+F1tree >/dev/tty1: runs tree redirecting stdout to a character device (the first pseudo-terminal);
kos
  • 35,891