How to Redirect output to a file
The output of a process can be redirected to a file by typing the command followed by the output redirection operator and file name.
For example, to redirect the results of the who command to a file named users, type the following:
who > users
Note: If the users file already exists, it is deleted and replaced, unless the noclobber option of the set built-in ksh (Korn shell) or csh (C shell) command is specified. To see the contents of the users file, type the following:
A list similar to the following is displayed:
denise lft/0 May 13 08:05
marta pts/1 May 13 08:10
endrica pts/2 May 13 09:33
How to Redirect output to append to a file
When the notation >> filename is added to the end of a command, the output of the command is appended to the specified file name, rather than writing over any existing data. The >> symbol is known as the append redirection operator.
For example, to append file2 to file1, type the following:
cat file2 >> file1
Note: If the file1 file does not exist, it is created, unless the noclobber option of the set built-in ksh (Korn shell) or csh (C shell) command is specified.
Displaying program output and copying to a file
The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files. Use the tee command to view your output immediately and at the same time, store it for future use.
For example, type the following:
ps -ef | tee program.ps
This displays the standard output of the ps -ef command on the display device, and at the same time, saves a copy of it in the program.ps file. If the program.ps file already exists, it is deleted and replaced unless the noclobber option of the set built-in command is specified. For example, to view and save the output from a command to an existing file:
ls -l | tee -a program.ls
This displays the standard output of ls -l at the display device and at the same time appends a copy of it to the end of the program.ls file.
Also Read: Frequently Asked Interview Questions and Answers
Also Read: Frequently Asked Interview Questions and Answers
The system displays information similar to the following, and the program.ls file contains the same information:
-rw-rw-rw- 1 jones staff 2301 Sep 19 08:53 161414
-rw-rw-rw- 1 jones staff 6317 Aug 31 13:17 def.rpt
-rw-rw-rw- 1 jones staff 5550 Sep 10 14:13 try.doc
Copying your screen to a file (capture and script commands)
Use the capture command, which emulates a VT100 terminal, to copy everything printed on your terminal to a file that you specify. Use the script command to copy everything printed on your terminal to a file that you specify, without emulating a VT100 terminal.
Both commands are useful for printing records of terminal dialogs. For example, to capture the screen of a terminal while emulating a VT100, at the prompt, type the following:
capture screen.01
The system displays information similar to the following:
Capture command is started. The file is screen.01.
Use ^P to dump screen to file screen.01.
You are now emulating a vt100 terminal.
Press Any Key to continue.
After entering data and dumping the screen contents, stop the capture command by pressing Ctrl-D or typing exit and pressing Enter. The system displays information similar to the following:
Capture command is complete. The file is screen.01.
You are NO LONGER emulating a vt100 terminal.
Use the cat command to display the contents of your file.
For example, to capture the screen of a terminal, at the prompt, type the following:
script
The system displays information similar to the following:
Script command is started. The file is typescript.
Everything displayed on the screen is now copied to the typescript file.
To stop the script command, press Ctrl-D or type exit and press Enter. The system displays information similar to the following:
Script command is complete. The file is typescript.
Use the cat command to display the contents of your file.
How to send Standard error and other output redirection
In addition to the standard input and standard output, commands often produce other types of output, such as error or status messages known as diagnostic output. Like standard output, standard error output is written to the screen unless it is redirected.
To redirect standard error or other output, use a file descriptor. A file descriptor is a number associated with each of the I/O files that a command ordinarily uses. File descriptors can also be specified to redirect standard input and standard output, but are already the default values. The following numbers are associated with standard input, output, and error:
0 Standard input (keyboard)
1 Standard output (display)
2 Standard error (display)
To redirect standard error output, type the file descriptor number 2 in front of the output or append redirection symbols (> or > >) and a file name after the symbol. For example, the following command takes the standard error output from the cc command where it is used to compile the testfile.c file and appends it to the end of the ERRORS file:
Other types of output can also be redirected using the file descriptors from 0 through 9. For example, if the cmd command writes output to file descriptor 9, you can redirect that output to the savedata file with the following command:
cmd 9> savedata
If a command writes to more than one output, you can independently redirect each one. Suppose that a command directs its standard output to file descriptor 1, directs its standard error output to file descriptor 2, and builds a data file on file descriptor 9. The following command line redirects each of these outputs to a different file:
command > standard 2> error 9> data
0 Comment to "Redirecting the output in AIX"
Post a Comment