We all know that by default every command output is displayed on the terminal after execution.
The output of a command can be used as a variable or input to another command or then redirected to a file for later reference.
This is not always been the situation. also, the saved output of the command can shared with somebody for further analysis.
In this article, we’ll see how to save the output of a command to a file in different ways.
- Using Redirection Operators ‘>’ & ‘>>’
- Using tee command
- Using logsave command
- Script – A Simple Command-Line Tool for Recording Your Terminal Session Activity
- How to Automatically Record the Terminal Session Activity of All Users on Linux
1) Saving the terminal output to a file using Redirection Operators ‘>’ & ‘>>’
The most common way to redirect output of the command from the terminal to a file is to use the ‘>’ and ‘>>’ operators.
- > : It redirects output to a file and overwrites an existing contents of the file.
- >> : It appends the output to the end of the file.
To create/overwrite a file, use the following syntax:
$ command > [filename]
For example, to save the hostnamectl command output to the file ‘myfile.txt’, run:
$ hostnamectl > myfile.txt
To append a file, use the following syntax:
$ command >> [filename]
For instance, to append uname command output to the file ‘myfile.txt’, run:
$ uname -a >> myfile.txt
Now, you can see the output of both commands from the myfile.txt file using the cat command.
$ cat myfile.txt
2) Saving command output to a file using tee command
The above method wont show the command output on the terminal but the tee command writes the result to standard output (stdout) and one or more files simultaneously.
[command] | tee [options] [filename]
To create a new file or overwrite the contents of an existing file with the output of the command using the tee command, run:
$ free | tee myfile_tee.txt total used free shared buff/cache available Mem: 16212768 9433216 1468668 2596720 5310884 3875308 Swap: 2097492 1015188 1082304
To append a command output to file without overwriting it’s existing contents, run:
$ date | tee -a myfile_tee.txt Fri Jul 16 13:53:34 IST 2021
Now, you can see the output of both commands from the ‘myfile_tee.txt’ file using the cat command.
$ cat myfile_tee.txt total used free shared buff/cache available Mem: 16212768 9433216 1468668 2596720 5310884 3875308 Swap: 2097492 1015188 1082304 Fri Jul 16 13:53:34 IST 2021
3) Redirect command output to a file using logsave command
The logsave command works similar to tee command, and adds timestamps (date and time) the command is executed.
It adds two timestamps for every command output:
- Start Time (First Time): This refers the start time of the command execution.
- End Time (Second Time): This indicates the completion time of the command.
Make a note: logsave command requires superuser privileges to run (eg. root or sudo).
logsave [filename] [command]
For instance, to save the lsblk command output to the file ‘myfile_logsave.txt’, run:
$ sudo logsave myfile_logsave.txt lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 931.5G 0 disk ├─sda1 8:1 0 128M 0 part └─sda2 8:2 0 931.4G 0 part /run/media/linuxgeek/DATA nvme0n1 259:0 0 238.5G 0 disk ├─nvme0n1p1 259:1 0 512M 0 part ├─nvme0n1p2 259:2 0 236G 0 part / └─nvme0n1p3 259:3 0 2G 0 part [SWAP]
To append the output of the ip command to an already existing file ‘myfile_logsave.txt’, run:
$ sudo logsave -a myfile_logsave.txt ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000 link/ether c8:5b:76:4d:d4:5c brd ff:ff:ff:ff:ff:ff 3: wlan0: mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether e4:a7:a0:32:fc:e9 brd ff:ff:ff:ff:ff:ff inet 192.168.1.6/24 brd 192.168.1.255 scope global dynamic noprefixroute wlan0 valid_lft 81661sec preferred_lft 81661sec inet6 fe80::cdcf:7111:ce4b:f02e/64 scope link noprefixroute valid_lft forever preferred_lft forever
Now, you can see the output of both commands from the ‘myfile_logsave.txt’ file using the cat command.
$ cat myfile_logsave.txt
In this guide, we’ve shown you how to redirect Linux command output to a file using three different methods.
If you have any questions or feedback, feel free to comment below.