1.6 Redirection and Piping Flashcards
What is the difference between redirection and piping?
- Redirection occurs to and from files - sending command output to a file or getting command input from a file.
- Piping occurs between commands - connecting the output of one command to the input of another.
When might you choose to redirect the input of a command?
When you wish to take command input from a file rather than the keyboard.
What are the three default file descriptors that Linux uses to classify information for a command?
- Standard input or STDIN, file descriptor 0
- Standard output or STDOUT, file descriptor 1
- Standard error or STDERR, file descriptor 2
How can you overcome the 128 KB shell command size restriction?
You can overcome the 128 KB shell command size restriction by using the xargs command.
What is the default file descriptor if none is specified?
File descriptor 1, standard output.
What are the symbols for redirecting standard intput, standard output, and standard error?
- Standard input - command < filename
- Standard output - command 1> filename
- Standard error - command 2> filename
How do you redirect both standard output and standard error to a file?
command 1> filename 2> &1
How do you connect a file to the standard input of the sort command?
sort < filename
What is redirection?
Redirection directs standard input, output, and error streams from and to locations other than the default.
What is piping?
Piping connects the output stream of one command to the input stream of another command.
What is standard input and what is its file descriptor?
Standard input (stdin) comes from the keyboard. In redirection, 0 represents stdin.
What is standard output and what is its file descriptor?
Standard output (stdout) displays on the monitor. In redirection, 1 represents stdout.
What is standard error and what is its file descriptor?
Standard errors (stderr) display on the monitor. In redirection, 2 represents stderr.
What are the redirection symbols and how do they work?
- Greater-than symbol (>) - redirects output
- Less-than symbol (<) - redirects input
- Double greater-than symbol (>>) - appends output
What does the tee command do?
The tee command reads from standard input and writes to standard output and to files.
What does the command ls /usr > /tmp/deleteme do?
ls /usr > /tmp/deleteme places the list of files in the /usr directory into a file named /tmp/deleteme.
What does the command ls /nonesuch > /tmp/deleteme do?
ls /nonesuch > /tmp/deleteme does not write anything to a file and sends an error message to the monitor ‘/nonesuch not found’.
What does the command ls /nonesuch 2 > /tmp/deleteme do?
ls /nonesuch 2> /tmp/deleteme writes the standard error message to a file named /tmp/deleteme.
What does the command ls /bin /nonesuch > /tmp/deleteme do?
ls /bin /nonesuch > /tmp/deleteme writes the contents of the /bin directory to the /tmp/deleteme file, but sends the error message ‘/nonesuch not found’ to the screen.
What does the command ls /bin /nonesuch > /tmp/deleteme 2>&1 do?
ls /bin /nonesuch > /tmp/deleteme 2> &1 directs the standard output to the /tmp/deleteme file, then directs that the standard error messages be sent to the same place as the standard output. Both the list of files in the /bin directory and the error message are written to the file.
What does the command ls /bin /nonesuch 2>&1 > /tmp/deleteme do?
ls /bin /nonesuch 2> &1 > /tmp/deleteme writes the contents of the /bin directory to the /tmp/deleteme file, but sends the error message ‘/nonesuch not found’ to the screen. This is because standard error messages are directed to the same place that standard output goes, but this is before standard output has been directed to the file.
What does the command ls /bin >> /tmp/deleteme do?
ls /bin >> /tmp/deleteme appends the list of files from the /usr directory on to the end of the /tmp/deleteme file.
What does the command sort < unordered_file.txt > ordered_file.txt do?
sort < unordered_file.txt > ordered_file.txt takes input from the unordered_file.txt file sends it to the sort command, and then writes a new file named ordered_file.txt.
What does the command cat /usr/wordlist1 /usr/wordlist2 | sort do?
cat /usr/wordlist1 /usr/wordlist2 | sort sends the output of the cat command, the contents of wordlist1 and wordlist2, to the input of the sort command. The result is a sorted list of the combined contents of wordlist1 and wordlist2.