Store the output of a Linux command to a file
This file can be used as information for another operation or to log terminal activity. Here are 4 different ways in which to save terminal content in a file.
The following methods are applicable on all Linux distributions and can even be used on Macs and BSDs. The only thing you have to consider is which method is best to get the job done.
1. Use the redirection operator
The most basic and common way to redirect output from the terminal to a file is to use the > and >> operators. They redirect output to a specified text file.
The > operator redirects the output to a file and overwrites the contents of that file while the >> operator appends the output to the end of the file. Here is an illustrative example:
First create a file using the touch command.
Now, a line is inserted into 'file1.txt' using the > operator. This can be thought of as dumping the output of the echo command into 'file1.txt'.
echo "first statement" > file1.txt
Another line is included in 'file1.txt'.
echo "second statement" > file1.txt
When viewing the contents of 'file1.txt', we see that only the second line is stored in the file. This is because the > operator overwrites the existing file contents.
If you want to append content to the file, the >> operator needs to be specified.
If you just want to save the error, use the 2> and 2>> operators instead.
If you care about logging everything, use &> and &>> to redirect all output (including errors) to a file without displaying anything in the terminal.
2. Use the tee command
With the redirection operators shown above, the output of the command is not displayed on the screen. If you want it to display the output on the terminal and write to a file, you can use the tee command.
| tee -a
Icon | pass the output of the command as input to tee, thereby displaying the output on the screen. If switch -a is specified, then tee will append that output to the specified file, otherwise it will overwrite the contents of that file.
You can also use the |& operator and the tee command to display everything and log it.
|& tee -a
3. Using the script command
With the script command, the output of commands entered after it is automatically written to a file until prompted to stop. This can be likened to a session that records terminal activity.
First, the script command is called with the name of the file to store the terminal activity.
script
A message prompts that the script has started, then the commands are typed one after the other - date, pwd, ls and cal.
To terminate, the exit command is called. There is a message that the 'script' operation has completed. View the contents of 'script_log.txt' using cat. It can be seen that the contents of the file are like an exact copy of the terminal.
But only when this file is viewed in the shell with the cat command, will we get completely understandable information. So try another alternative, save the terminal output in '2-script_log.txt'.
Commandspwd and ls are entered.
Without typing exit, the terminal window is closed. When viewing the contents of '2-script_log.txt', you will see it as empty.
When a script session is started, the contents of that session are kept in memory and written to the file only when the exit command is called. Here, since exit is not called, the contents of that session are not saved to '2-script_log.txt'.
The script's overriding and appending behavior is similar to the redirect operators and the tee command. Switch -a appends the session's contents to the previously existing file. Here, the output of the echo command is appended to 'script_log.txt'.
You can see the previously saved output of the date, pwd, ls, cal commands, followed by timestamp; then information about the next session (ending with timestamp).
4. Use the logave command
logave works similar to tee - it displays the output on the screen and also saves to a file. It is used as below:
logsave
logave writes the output of the command
There is a lot of information stored with the result of a command. Two timestamps are stored here: The first is when the command was started, and the second is the time at which the command completes execution.
The output of multiple commands can be added to the same file when switch -a is used with logave.
logsave -a
When viewing the contents of mylog_file.txt, you will see that the output of the two previously entered commands is delimited by a single line.
You should read it
May be interested
- 11 df commands in Linux (with example)with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
- How to use the Install command to copy files in Linuxinstall is a flexible file copying command in linux and macos suitable for professional users. read this article to discover how to use the install command.
- How to completely delete a file in Linux so that it cannot be restoredin linux, files can be deleted but can still be recovered. these are things to do when you really want them to disappear.
- Instructions for using find command in Linuxthe find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
- Find and delete all .DS_Store files with a single commandby default, the '.ds_store' file is hidden, and most of these files do not affect the system. however, when you compress a folder and send it to a friend (used on another operating system) or upload a folder to the server, you will encounter some problems.
- How to use the stat command on Linuxthe linux stat command shows you more details than the ls command. the following article will show you how to use the stat command in linux. like ls, the stat command has many options. this makes it a great candidate for the use of alias.
- 14 interesting Linux commands in Terminalterminal is a very powerful tool, but it can become 'interesting' through a few bash commands that quantrimang will introduce to you later. let's follow up and find out because some commands are quite useful.
- 18 Interesting Linux Commands in Terminalterminal is a very powerful tool, but it can be made 'interesting' through a few bash commands that tipsmake.com will introduce to you below. let's follow and learn because some commands are quite useful.
- Clip command in Windowsclip command redirects the command output from the command line to the windows clipboard. you can then paste this text output into other programs.
- How to copy (and paste) files and folders from the Linux command linecopying and pasting files is one of the most basic things you can do on your computer. on linux, you have several options to accomplish this.