How to save command line output to file on Windows, Mac and Linux

You can do this using output redirection on the command line. Learn how to send command output to a text file on Windows, Mac, and Linux.

Redirect command line output to a file

There are two operators you can use to redirect command output to file: >> and > . It is important that you understand the difference between these two operators to avoid unintended data loss.

The > icon creates a new file or overwrites the old file if it already exists. The >> operator also creates a new file if it does not exist but it will not overwrite the existing file. If the file already exists, it will add the text to the end of the file.

To redirect the output of a command to a file, enter the command, then specify the > or >> operator , and finally provide the path to the file you want to redirect the output to. For example, here's how you can save the output from the ls command, which lists the contents of a directory:

ls > /path/to/file

Replace /path/to/file with the full path to the file you want to use. The command will run silently, storing the output in the file you specify.

To view the contents of a file in the terminal, you can use the cat command. Again, replace /path/to/file with the full path to the file you want to view.

cat /path/to/file

You should see the output from the command in your new file:

How to save command line output to file on Windows, Mac and Linux Picture 1

 

The > operator replaces the content of an existing file. If you want to save the results from multiple commands into a single file, use the >> operator instead. This will add to the file, so you won't lose any previous output you saved.

For example, try adding system information to the end of the file you just created. Just run uname -a on Linux/Mac - or the ver command if you're on Windows - and add the >> operator along with the path to the file:

uname -a >> /path/to/file

Repeat this process as many times as you need to continue adding command output to the end of the file.

How to save command line output to file on Windows, Mac and Linux Picture 2

Export output to screen and redirect it to a file

The > and >> operators do not display the output of the command on the screen, they just send it to a file. If you want to send output to a file and view it on the screen, use the tee command.

 

To use the tee command, send output from another command to that command using the pipe operator, a vertical bar (|). For example, here's how you can send the output of the ls command to tee using a pipe:

ls | tee /path/to/output.txt

The tee command then sends that output to both the screen and the file you specify. This operation will overwrite the file or create a new file if it does not exist, just like the > operator .

To use the tee command to print the result to the screen and append it to the end of the file, add the -a flag before the file path, for example:

uname -a | tee -a /path/to/output.txt

You will see the command output on the screen and you can use cat to verify that tee also added it to the file:

How to save command line output to file on Windows, Mac and Linux Picture 3

4 ★ | 1 Vote

May be interested

  • 5 reasons why people love the Linux command line5 reasons why people love the Linux command line
    many people are afraid of the command line. they see it as something for software developers or geeks. but the command line is merely another way of interacting with a pc, and there are some tasks that are very easy to do with the cli.
  • How to use Isof command on LinuxHow to use Isof command on Linux
    if everything in linux is a file, you'll have more than just the file on your hard drive. this article will show you how to use the lsof command to see all other devices and processes processed as a file.
  • How to check whether a Linux PC is 64-bit or 32-bit by command lineHow to check whether a Linux PC is 64-bit or 32-bit by command line
    how to check if your linux system is 32-bit or 64-bit? in this article, tipsmake.com will show you how to define it using the gui and the command line.
  • Tail command in LinuxTail command in Linux
    tail is a complement to the head command. as its name implies, the tail command outputs the last n data numbers of the given input. by default, the tail command prints the last 10 lines of the specified file. if more than one file name is provided then the file name will be preceded by the data from each file.
  • How to delete files and folders using Linux command lineHow to delete files and folders using Linux command line
    in linux, the rm command is used to delete files and directories.
  • How to print from the Linux command lineHow to print from the Linux command line
    printing documents from the linux command line is easy. you use the lp command to request printing and lpq to see which print jobs are in the queue.
  • Navigation IO in Unix / LinuxNavigation IO in Unix / Linux
    most unix system commands receive input from your terminal and send the output back to your terminal. a command usually reads its input from a location called standard input, which happens to your terminal by default.
  • 20+ essential Linux security commands20+ essential Linux security commands
    here are some of the most important security commands for everyday work on linux systems. if you're interested in security issues on your linux system, don't ignore these helpful security commands.
  • How to quickly create new blank text files on Windows, Mac and LinuxHow to quickly create new blank text files on Windows, Mac and Linux
    text file is useful for everything. recording a note, storing information, and journaling are just a few of the many things you can do with text files. today, we will show you how to create new blank text files in windows, mac and linux quickly. in windows, it is easy to locate new text files. but on mac and linux, this job requires some initial setup, then creating a new text file is also quick and easy.
  • How to access SSH on Windows 7 using CygwinHow to access SSH on Windows 7 using Cygwin
    if you are comfortable using linux / unix and want to access ssh on a windows 7 computer, you can use cygwin. this windows linux emulator provides this functionality and gives you a familiar work environment with just a few simple steps.