Question | Click to View Answer |
Write the output of the ls /bin command to the ~/Desktop/some_executables file. |
ls /bin > ~/Desktop/some_executables
|
Run the following command and then delete the contents of the greeting file (keep the file, just delete the contents). echo "hi there" > ~/Desktop/greeting
|
> ~/Desktop/greeting
The > redirection operator clobbers files. In this example, no new text is written to the clobbered file, so it's empty. |
Where are standard output and standard error redirected to by default? |
Standard output and standard error are directed to the Terminal by default for commands typed into the Terminal. |
Redirect the standard error of the following command to a text file called ~/Desktop/feed_me: $ ls /fake_dir
|
ls /fake_dir 2> ~/Desktop/feed_me
The shell internally references standard input as 0, standard output as 1, and standard error as 2. The > operator redirects standard output by default, but it redirects standard error if 2 is prepended (e.g. 2>). |
Write the standard output of the following command to the ~/Desktop/dunno files: echo "does this work"
|
# one method with explicit syntax
echo "does this work" 1> ~/Desktop/dunno
# another method
echo "does this work" > ~/Desktop/dunno
|
Explain this code: ls /not_here >> ~/Desktop/whatever 2>&1
cat ~/Desktop/whatever
|
2>&1 redirects standard error (2) to standard output (1). Standard output is appended to the ~/Desktop/whatever file, so 2>&1 appends standard error to the ~/Desktop/whatever file as well. |
Explain this code: echo $SHELL >> ~/Desktop/whatever 2>&1
cat ~/Desktop/whatever
|
The standard output is appended to the ~/Desktop/whatever file. |
Explain the following code: ls /weird 2> /dev/null
|
The standard error from the $ ls /weird command is redirected to the /dev/null file. /dev/null is a special type of file that accepts standard output and standard error and just discards it without doing anything. |
Sort the executables in the /bin directory and view the sorted list with the less pager program. |
ls /bin | sort | less
|
Return the number of unique executables in the PATH variable. |
DIRS=$( echo $PATH | tr ":" " ")
ls $DIRS | sort | uniq | wc -l
|
List all the processes running on all terminals that contain the word bin. |
ps -x | grep bin
|