Which of the following commands will redirect errors in a script to a file

CONCEPT: Every program you run from the shell opens three files: Standard input, standard output, and standard error. The files provide the primary means of communications between the programs, and exist for as long as the process runs.

The standard input file provides a way to send data to a process. As a default, the standard input is read from the terminal keyboard.

The standard output provides a means for the program to output data. As a default, the standard output goes to the terminal display screen.

The standard error is where the program reports any errors encountered during execution. By default, the standard error goes to the terminal display.

CONCEPT: A program can be told where to look for input and where to send output, using input/output redirection. Unix uses the "less than" and "greater than" special characters (< and >) to signify input and output redirection, respectively.

Redirecting input

Using the "less-than" sign with a file name like this:< file1in a shell command instructs the shell to read input from a file called "file1" instead of from the keyboard.

EXAMPLE:Use standard input redirection to send the contents of the file /etc/passwd to the more command:

more < /etc/passwd

Many Unix commands that will accept a file name as a command line argument, will also accept input from standard input if no file is given on the command line.

EXAMPLE: To see the first ten lines of the /etc/passwd file, the command:

head /etc/passwdwill work just the same as the command:head < /etc/passwd

Redirecting output

Using the "greater-than" sign with a file name like this:> file2causes the shell to place the output from the command in a file called "file2" instead of on the screen. If the file "file2" already exists, the old version will be overwritten.

EXAMPLE: Type the command

ls /tmp > ~/ls.outto redirect the output of the ls command into a file called "ls.out" in your home directory. Remember that the tilde (~) is Unix shorthand for your home directory. In this command, the ls command will list the contents of the /tmp directory.

Use two "greater-than" signs to append to an existing file. For example:

>> file2causes the shell to append the output from a command to the end of a file called "file2". If the file "file2" does not already exist, it will be created.

EXAMPLE: In this example, I list the contents of the /tmp directory, and put it in a file called myls. Then, I list the contents of the /etc directory, and append it to the file myls:

ls /tmp > myls
ls /etc >> myls

Redirecting error

Redirecting standard error is a bit trickier, depending on the kind of shell you're using (there's more than one flavor of shell program!). In the POSIX shell and ksh, redirect the standard error with the symbol "2>".

EXAMPLE: Sort the /etc/passwd file, place the results in a file called foo, and trap any errors in a file called err with the command:

Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your standard input (stdin) device, and the screen or a particular terminal window is the standard output (stdout) device.

However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.

5.1.2. The redirection operators

5.1.2.1. Output redirection with > and |

Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.

As we saw before, the cat command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.

nancy:~> cat test1
some words

nancy:~> cat test2
some other words

nancy:~> cat test1 test2 > test3

nancy:~> cat test3
some words
some other words

Which of the following commands will redirect errors in a script to a file
Don't overwrite!

Redirecting "nothing" to an existing file is equal to emptying the file:

nancy:~> ls -l list
-rw-rw-r--    1 nancy   nancy     117 Apr  2 18:09 list

nancy:~> > list

nancy:~> ls -l list
-rw-rw-r--    1 nancy   nancy       0 Apr  4 12:01 list

This process is called truncating.

The same redirection to an nonexistent file will create a new empty file with the given name:

nancy:~> ls -l newlist
ls: newlist: No such file or directory

nancy:~> > newlist

nancy:~> ls -l newlist
-rw-rw-r--  1 nancy   nancy	    0 Apr  4 12:05 newlist

Chapter 7 gives some more examples on the use of this sort of redirection.

Some examples using piping of commands:

To find a word within some text, display all lines matching "pattern1", and exclude lines also matching "pattern2" from being displayed:

grep pattern1 file | grep -v pattern2

To display output of a directory listing one page at a time:

ls -la | less

To find a file in a directory:

ls -l | grep part_of_file_name

5.1.2.2. Input redirection

In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the "<" (less-than symbol) operator.

Below is an example of sending a file to somebody, using input redirection.

andy:~> mail [email protected] < to_do

If the user mike exists on the system, you don't need to type the full address. If you want to reach somebody on the Internet, enter the fully qualified address as an argument to mail.

This reads a bit more difficult than the beginner's cat file | mail someone, but it is of course a much more elegant way of using the available tools.

5.1.2.3. Combining redirections

The following example combines input and output redirection. The file text.txt is first checked for spelling mistakes, and the output is redirected to an error log file:

spell < text.txt > error.log

The following command lists all commands that you can issue to examine another file when using less:

mike:~> less --help | grep -i examine
  :e [file]      Examine a new file.
  :n          *  Examine the (N-th) next file from the command line.
  :p          *  Examine the (N-th) previous file from the command line.
  :x          *  Examine the first (or N-th) file from the command line.

The -i option is used for case-insensitive searches - remember that UNIX systems are very case-sensitive.

If you want to save output of this command for future reference, redirect the output to a file:

mike:~> less --help | grep -i examine > examine-files-in-less

mike:~> cat examine-files-in-less
  :e [file]      Examine a new file.
  :n          *  Examine the (N-th) next file from the command line.
  :p          *  Examine the (N-th) previous file from the command line.
  :x          *  Examine the first (or N-th) file from the command line.

Again, make sure you don't use names of existing files that you still need. Redirecting output to existing files will replace the content of those files.

5.1.2.4. The >> operator

Instead of overwriting file data, you can also append text to an existing file using two subsequent greater-than signs:

Example:

mike:~> cat wishlist
more money
less work

mike:~> date >> wishlist

mike:~> cat wishlist
more money
less work
Thu Feb 28 20:23:07 CET 2002

The date command would normally put the last line on the screen; now it is appended to the file wishlist.

How to redirect command output to a file in shell script?

There are multiple ways to redirect output from shell scripts and commands..
Redirect STDOUT. ... .
Redirect STDERR. ... .
Send STDOUT and STDERR to the same file. ... .
Redirect output, but append the file. ... .
Redirect to another process or to nowhere. ... .
Use redirection in a script..

What command makes a file in bash?

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file. $ cat > secondFile.

Which of the following Linux commands will create an empty file?

touch command: It is used to create a file without any content. The file created using touch command is empty.

What command will append the output of the ls command to the ls txt file?

For example, run ls command and store its output the file called “file-lists.txt”:.
ls -l /bin > file-lists.txt. ... .
cat file-lists.txt. ... .
more file-lists.txt. ... .
# command must be run as root # grep '1.2.3.4' /var/log/httpd/access_log > /root/spam-log.txt..