When a Linux person styles any command into the bash prompt, the terminal ordinarily prints the output of the invoked command so you can examine it straight away. Nonetheless, bash also permits you to “redirect” or conserve any command’s output in the technique.
This posting will discuss three unique methods of redirecting the output of the top command to any file.
Process 1: Single File Output Redirection
For employing the redirection of bash, execute any script, then define the > or >> operator followed by the file route to which the output really should be redirected.
- “>>” operator is utilized for using the command’s output to a file, including the output to the file’s present contents.
- “>” operator is applied to redirect the command’s output to a solitary file and exchange the file’s present-day articles.
We can say that technically, this is a file redirection of “stdout,” which is the standard screen. Now, we will execute the sample example. The “ls” command displays the material of the present directory’s folders and documents following its execution.
Nevertheless, this command will conserve the output to the specified file in the adhering to illustration fairly than printing it to the terminal.
ls > /home/linuxhint/outputfile
Make the most of the offered command syntax for checking the content of the file.
Now, publish out the under-specified command for printing the content of the “output file” in the terminal.
$ cat /dwelling/linuxhint/outputfile
The operator “>” overwrites the file material with the command execution output. Instead, you can use the “>>” operator for preserving the various instructions output in a one file. For occasion, the execution of the provided command will increase the technique details to the distinct file.
uname -a >> /route/to/file
$ cat /house/linuxhint/outputfile
Method 2: Redirecting terminal output to a one file
Didn’t like the strategy of utilizing the”>” or “>>” operator for redirecting output? Really do not get worried! The tee command is below to rescue you.
command | tee /path/to/file
$ ls | tee /household/linuxhint/outputfile
The below-presented tee command will overwrite the file content with the command’s output comparable to the “>” operator.
$ uname -a | tee -a /house/linuxhint/outputfile
Approach 3: The top command
System administrators also use the Linux best command to see true-time technique studies such as load average, program uptime, working responsibilities, employed memory, certain facts about each and every operating process, and a summary of threads or procedures. By utilizing the -b flag, this command aids to get the details about the presently executing processes in the program. The major command will allow the prime to function in batch manner and the -n flag to ascertain the amount of iterations the command really should get as output.
$ top -b -n 1 > topfile.txt
All of the output resulting from the prime command’s execution will be redirected to the specified file. Now, publish out the “less” command for examining the information of the file.
The -n flag will mail the one snapshot of executed command to the specified file. To retrieve only the initially iteration, specify the “1” following the “-n” flag.
$ major -b -n 1 > leading-iteration.txt
Employ the “cat” command for viewing the working responsibilities facts.
$ cat top rated-iteration.txt | grep Tasks
In Linux, for redirecting output to a file, make use of the ”>” and ”>>” redirection operators or the top command. Redirection makes it possible for you to save or redirect the output of a command in a different file on your method. You can use it to help you save the outputs and use them later for various reasons.