1.6 Redirection and Piping Flashcards

2
Q

What is the difference between redirection and piping?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When might you choose to redirect the input of a command?

A

When you wish to take command input from a file rather than the keyboard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three default file descriptors that Linux uses to classify information for a command?

A
  1. Standard input or STDIN, file descriptor 0
  2. Standard output or STDOUT, file descriptor 1
  3. Standard error or STDERR, file descriptor 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you overcome the 128 KB shell command size restriction?

A

You can overcome the 128 KB shell command size restriction by using the xargs command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the default file descriptor if none is specified?

A

File descriptor 1, standard output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the symbols for redirecting standard intput, standard output, and standard error?

A
  • Standard input - command < filename
  • Standard output - command 1> filename
  • Standard error - command 2> filename
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you redirect both standard output and standard error to a file?

A

command 1> filename 2> &1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you connect a file to the standard input of the sort command?

A

sort < filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is redirection?

A

Redirection directs standard input, output, and error streams from and to locations other than the default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is piping?

A

Piping connects the output stream of one command to the input stream of another command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is standard input and what is its file descriptor?

A

Standard input (stdin) comes from the keyboard. In redirection, 0 represents stdin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is standard output and what is its file descriptor?

A

Standard output (stdout) displays on the monitor. In redirection, 1 represents stdout.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is standard error and what is its file descriptor?

A

Standard errors (stderr) display on the monitor. In redirection, 2 represents stderr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the redirection symbols and how do they work?

A
  • Greater-than symbol (>) - redirects output
  • Less-than symbol ( - redirects input
  • Double greater-than symbol (>>) - appends output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the tee command do?

A

The tee command reads from standard input and writes to standard output and to files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the command ls /usr > /tmp/deleteme do?

A

ls /usr > /tmp/deleteme places the list of files in the /usr directory into a file named /tmp/deleteme.

18
Q

What does the command ls /nonesuch > /tmp/deleteme do?

A

ls /nonesuch > /tmp/deleteme does not write anything to a file and sends an error message to the monitor ‘/nonesuch not found’.

19
Q

What does the command ls /nonesuch 2 > /tmp/deleteme do?

A

ls /nonesuch 2> /tmp/deleteme writes the standard error message to a file named /tmp/deleteme.

20
Q

What does the command ls /bin /nonesuch > /tmp/deleteme do?

A

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.

21
Q

What does the command ls /bin /nonesuch > /tmp/deleteme 2>&1 do?

A

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.

22
Q

What does the command ls /bin /nonesuch 2>&1 > /tmp/deleteme do?

A

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.

23
Q

What does the command ls /bin >> /tmp/deleteme do?

A

ls /bin >> /tmp/deleteme appends the list of files from the /usr directory on to the end of the /tmp/deleteme file.

24
Q

What does the command sort < unordered_file.txt > ordered_file.txt do?

A

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.

25
Q

What does the command cat /usr/wordlist1 /usr/wordlist2 | sort do?

A

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.

26
Q

What does the command cat /usr/wordlist1 /usr/wordlist2 | mail jdoe do?

A

cat /usr/wordlist1 /usr/wordlist2 | mail jdoe mails the combined list of words in wordlist1 and wordlist2 to the user jdoe.

27
Q

What does the command ls /bin | sort | mail jdoe do?

A

ls /bin | sort | mail jdoe lists the contents of /bin then sorts the combined contents and mails them to jdoe.

28
Q

What does the command cat /usr/wordlist1 /usr/wordlist2 | sort | tee sortedwordlist do?

A

cat /usr/wordlist1 /usr/wordlist2 | sort | tee sortedwordlist lists the contents of wordlist1 and wordlist2 then sorts the combined contents, then sends the results to the monitor and a file named sortedwordlist.

29
Q

What does the command cat /usr/wordlist1 | tee log.txt do?

A

cat /usr/wordlist1 | tee log.txt writes to the standard output and the log.txt file.

30
Q

What does the xargs command do?

A

The xargs command reads items from the standard input and breaks up long lists of arguments into smaller, usable lists.

31
Q

What are the main features of the xargs command?

A
  • Makes it easier to pipe input into commands that take arguments.
  • Overcomes a 128 KB shell command size restriction in older Linux kernels.
32
Q

Which commands does xargs commonly take input from?

A
  • find
  • ls
  • locate
  • grep -l
33
Q

Which xargs option ignores spaces in file names

A

xargs -0 ignores spaces in file names.

34
Q

What does the command find / -print0 -name *.odt | xargs -0 rm do?

A

find / -print0 -name *.odt | xargs -0 rm deletes all .odt files in the file system, even those with spaces in the file names.

35
Q

Which xargs option replace the initial argument of a command with the argument from the standard input?

A

xargs -I variable replaces the initial argument of a command with the argument from the standard input.

36
Q

What does the command find / -name ‘*.jpg’ | xargs -I var1 cp var1 /home/user/Pictures do?

A

find / -name ‘*.jpg’ | xargs -I var1 cp var1 /home/user/Pictures finds all the .jpg files on the computer and copies them into the /home/user/Pictures directory.