The command line interface (CLI) on operating systems like Windows, macOS and Linux provides immense power and flexibility to interact with your computer. One handy feature of the CLI is the ability to save the output text generated by commands to a file for analysis later.
Instead of only printing the output to the terminal, you can store command results in a file using output redirection. This allows combining output from different commands, sharing data between tools, and setting up monitoring and logging.
In this guide, we will cover how you can redirect command line output to a text file on Windows, Mac, and Linux machines using built-in CLI capabilities.
Redirecting Output on Windows
On Windows, you can leverage the >
and >>
operators to send command output to a file.
>
creates a new file or overwrites an existing one to save output.>>
also creates a new file if it doesn't exist already, but appends output to the end of the file if it does.
Save Command Output to New File
To save the output of a command to a new text file, use >
like this:
command > C:\path\to\output.txt
For example:
ver > C:\Users\John\Desktop\system-info.txt
This will save the Windows system version information to a new text file called system-info.txt
on the desktop.
Append Output to Existing File
To append output to the end of an existing text file, use >>
instead:
command >> C:\path\to\output.txt
For instance:
systeminfo >> C:\Users\John\Desktop\system-info.txt
This will add the system information to the end of the system-info.txt
file without overwriting it.
Redirecting Output on Mac
The process of saving command output to a file is very similar on macOS. The >
and >>
operators work the same way.
Create New File with Output
Use >
to save command results to a new file:
command > /Users/John/Desktop/output.txt
Example:
sw_vers > /Users/john/Desktop/mac-version.txt
Append to Existing File
Use >>
to add output to the end of a file:
command >> /Users/John/Desktop/output.txt
For instance:
system_profiler SPSoftwareDataType >> /Users/john/Desktop/mac-info.txt
This will append macOS software data to the info text file.
Saving Output on Linux
Linux-based operating systems like Ubuntu also use the same >
and >>
redirection operators.
Create New File
To save output to new file, use >
:
command > /home/john/Documents/output.txt
Example:
uname -a > /home/john/Documents/system-info.txt
Append to Existing File
To add output to a file, use >>
:
command >> /home/john/Documents/output.txt
For instance:
lscpu >> /home/john/Documents/system-info.txt
This will append CPU architecture data to the info text file.
Display Output on Screen While Saving to File
The >
and >>
operators redirect output only to a file. To print the output on screen and save it to a file, use the tee
command.
Pipe the command output to tee
and specify file path:
command | tee /path/to/output.txt
This will display the output in the terminal and overwrite the file content.
To append instead of overwrite, use the -a
flag:
command | tee -a /path/to/output.txt
For example:
df -h | tee -a /home/john/Documents/disk-usage.txt
This prints the disk usage data on screen and adds it to the text file.
Practical Examples of Saving Command Output
Here are some examples of how you can save useful command output to text files:
Monitor ongoing processes -
ps aux | tee processes.txt
Log terminal session -
script -a session.txt
Store network interface details -
ifconfig | tee network-info.txt
List disk partitions -
fdisk -l | tee disk-partitions.txt
Check server uptime -
uptime | tee uptime.txt
View current directory contents -
ls -al | tee directory-listing.txt
Conclusion and Next Steps
The ability to save command line output to a file unlocks many possibilities through the composable and scriptable power of the CLI. Here are some next steps to take your skills further:
Learn to append timestamp to output so you know when data was logged
Write a script to collect and save important system stats to monitor over time
Integrate command output into other applications like spreadsheets or databases
Configure cron jobs or scheduled tasks to automatically run commands and save output
Process data from multiple commands using tools like
grep
,sed
,awk
etc.Create system log files, performance reports and automated monitoring
With some practice, you'll be able to leverage command line redirection proficiently to gather, process and analyze data the way you want!