How to Pipe Output to File in Linux [4 Ways]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Redirecting the output of a command to a file through a pipe is a fundamental concept in command-line environments, like Bash scripting. and it involves redirecting the output of a command to a file for further use or storage. This process allows you to save, analyze, or share the output of commands, making it a valuable tool for working with data and automating tasks. In this tutorial, I will discuss the basics of pipe, and how you can use it to redirect the output of a command to a file in Linux.

Key Takeaways

  • Learning basics about the pipe to redirect command output.
  • Practicing bash script examples of different ways to pipe output to a file.

Free Downloads

Use of Pipe Output to a File

Though it is possible to pipe output in many ways such as pipe to a variable and pipe to another command, piping output to a file is useful in various scenarios:

  • Logging: You can log the output of commands to a file for later review, debugging, or auditing.
  • Data Extraction: You can extract and save specific information, such as log entries, data records, or error messages, from command output to a file.
  • Scripting: When writing Bash scripts, redirecting output to files is crucial for saving data, and creating reports like generating log files.
  • Automation: Redirecting output allows you to automate tasks by capturing information and processing it further.
  • Sharing: You can save the results of commands to files that you can easily share with others or use in documentation.

4 Cases of How to Pipe Output to a File

In Bash, you can pipe the output of a command to a file using various methods depending on your specific use case. Also, through piping, you can operate many more command-line operations. In this tutorial, I’ll show you four common ways that you can use to pipe output to a file.

Case 01: Pipe Output to a File Using Redirection Operator

In Linux, you can use output redirection to send the output of a command to a file. This operator is denoted by “>”. Generally, the displaying of the standard output (stdout) of a command is changed from the terminal screen to a file or another command’s input by using this operator. However, it overwrites the file’s contents if it already exists. Follow the below steps to check the entire process:

Steps to Follow >
❶ Open the Ubuntu Terminal.
❷ Now, open a file, let’s say, named ‘output.sh’ in the nano editor by using the following command:

nano output.sh

➌ Next, write the following script inside the Bash file in the nano text editor:

#! /bin/bash 

cat input.txt | sort -r > file.txt 
cat file.txt
EXPLANATION

In the script’s first line, ‘#!’ is shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash situated in the /bin directory. In the next line, the input.txt file is passed to the sort command by using it with the cat command. Then the output is redirected to a file named file.txt’ using the ‘>’ operator. Finally, file.txt is previewed. However, if file.txt’ already exists, the script will overwrite it with the new output. Otherwise, a new file will be created.

➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
➎ After that, use the following command to make the script executable:

chmod u+x output.sh
explanation
  • chmod: Changes the permission of files and directories.
  • u+x: Argument with chmod command to add the executable permission for the user.
  • output.sh: File which you want to make executable.

➏ Finally, run the script by the following command:

./output.sh

Executing output.sh scriptThe image shows that the above script sorts the content of input.txt and stores the sorted output in a file named file.txt.

Redirect the output of a command to a log file directly from the terminal by the command syntax:

command > filename.txt

For example, I want to log the current date-time to a file named today.txt:

date > today.txt

The date command shows the current date and time. Now, let’s preview the contents of list.txt by:

cat today.txt

Pipe output of date command to a file today.txtThe above image shows that the current date is redirected to a file called today.txt.

Case 02: Pipe Output to a File Using Append Operator

When you redirect a stdout to a file with the ‘>’ redirection operator, it overwrites the file. So, you can use >> instead of >, if you want to append the output of a command to a file preserving the existing content in it. Check out and execute the following script for the purpose.

You can follow the Steps of Case 01, to save & make the script executable.
Script (append.sh) >

#! /bin/bash 

cat input.txt | sort >> file.txt 
cat file.txt

EXPLANATION
In this script, the output of the sort command is appended to a file named ‘file.txt’ using the ‘>>’ operator. It writes inside the ‘file.txt’ file, generally at the bottom of the previously stored content of the file.

At first, let’s preview the content of the file.txt file by the command below:

cat file.txt

Now, execute the script:

./append.sh

Executing append.sh scriptThe script adds the command output to the last of the ‘file.txt’ file.

Case 03: Pipe Output to a File Using “tee” Command

The tee command is useful in Unix-like operating systems such as Linux for redirecting output. It allows you to save the output of a command to a file and simultaneously see the output in the terminal. This can be handy in various scenarios, such as logging, monitoring, and recording command outputs. Here’s an example of how to use the tee command to redirect output to a file:

date | tee date.txt

It will show the output of the ‘date command’ in the terminal as well as save it to date.txt like the below image.Pipe output of date command to a file date.txt with tee command Moreover, you can use the tee command to pass the output to another command as well as save output in a file. For example, you want to store all the files and folder names in your current directory in a file named log.txt and also want to count them. The following script shows the way.

You can follow the Steps of Case 01, to save & make the script executable.
Script (files.sh) >

#! /bin/bash 

line=$(ls | tee log.txt | wc -l) 
echo “Number of files: $line”
EXPLANATION

In this script, the tee command pipes the output of the ls command to the log.txt file as well as passes it to the wc command. Here, it counts the number of files and stores it in the line variable. Finally, the echo command prints the number of files.

Now, to check whether piping the output to a file is successful, preview the content of the log.txt file by:

cat log.txt

Executing files.sh scriptThe above script pipes the output of the previous command (ls) to a file (log.txt).

Case 04: Pipe Output to a Variable

You can capture the output of a command and pipe it in a variable in Bash using the read command. It reads a line from stdin and assigns it to a variable. The following script shows the way:

You can follow the Steps of Case 01, to save & make the script executable.
Script (variable.sh) >

#!/bin/bash 

# Capture the current date and time in a variable 
date | { read current_datetime; 
echo "Current date and time: $current_datetime"; }
EXPLANATION

In this script, the date command’s output is captured and piped in the current_datetime variable. In the first line, the date command outputs the current date-time, and the pipe operator ( | ) sends the output as input for the next command, where the read command reads input from the pipe and assigns it to the variable ‘current_datetime’. Finally, the echo command echoes the variable value

Execute the script by:

./variable.sh

Executing variable.sh script that pipe output of a command to a variableThe output shows that whenever I run the script, the output of the date command is piped in the current_datetime variable.

Conclusion

Piping output to a file is a versatile technique that helps you manage and utilize command output effectively in the Unix command-line environment. Particularly, it is useful for creating logs, monitoring long-running processes, and recording command execution. The redirection operator and tee command provide a flexible way to capture and pipe command output while retaining the visibility of that output in the terminal.

People Also Ask

How do I print output to a file in Linux?

You can use the redirection operator for this purpose with the syntax command > filename.txt. This will print the output of the command to filename.txt rather than showing it in the terminal. Later, you can see the content of the file using cat command. For example, date > today.txt will print the output of the date command to today.txt file that can be previewed by cat today.txt.

How do I pipe output to a file in terminal?

You can use the redirection operator and tee command for this purpose. If you want to pipe the output of a command to a file rather than showing it in the terminal, use the syntax command > filename.txt. In the case you want to see the output in the terminal as well as pipe it into a file, use command | tee filename.txt syntax.

How do I save a Linux terminal output to a file?

You can save a Linux terminal output to a file by piping the output with a redirection operator. Use the syntax for the purpose: command > log.txt. This will print the output of the command to log.txt rather than showing it in the terminal. Later, you can see the content of the file using cat log.txt.

How do I echo into a file?

You can echo into a file by directly using the echo command with the redirection operator with the syntax echo “some text” > filename.txt. If finename.txt already exists, the command will overwrite it with the new text. Otherwise, a new fill will be created. However, if you don’t want to replace the previous content of finename.txt, use >> instead of > in the above command.

Related Articles


<< Go Back to Bash Piping | Bash Redirection and Piping | Bash Scripting Tutorial

4.9/5 - (7 votes)
Ashikur Rahman

Hello, I’m Ashikur Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation in Computer Science and Engineering from Khulna University of Engineering & Technology (KUET). Also, I’m pursuing my master’s in the same department at Bangladesh University of Engineering & Technology (BUET). I like to learn new technologies, contribute, and share those with others. Here my goal is to provide beneficial and user-friendly articles on Linux distribution for everyone. Read Full Bio

Leave a Comment