Linux commands file output

How to save terminal output to a file under Linux/Unix

H ow do I save the terminal output to a file when using BASH/KSH/CSH/TCSH under Linux, macOS, *BSD or Unix-like operating systems?

Yes, we can save command output by redirecting it to a file. The standard streams for input, output, and error are as follows (also known as file descriptors):

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux, macOS, or Unix
Est. reading time 3 minutes
  1. stdin (numeric value 0) – Keyboard
  2. stdout (numeric value 1) – Screen/display
  3. stderr (numeric value 2) – Screen/display
  • Redirect stdout/stderr to a file
  • Redirect stdout to a stderr OR redirect stderr to a stdout
  • To redirect stderr and stdout to a file
  • We can redirect stderr and stdout to stdout too
  • And finally you can redirect stderr and stdout to stderr

How to save terminal output to a file

By default, the command sends outputs to stdout and can be redirected to the file using the following syntax:
command > filename.txt
For example, save the date command output to a file named mydate.txt, run:
date > mydate.txt
To view file contains use the cat command:
cat mydate.txt

Feed data to our commands (input redirection)

We can read input from a file using the following simple syntax and the file must already exist:
command

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Append output to a file

If the filename.txt/mydate.txt (file) already exists, it will get overwritten. To append output, run:
command >> filename.txt
echo «——————» >> mydate.txt
ls -l /etc/resolv.conf >> mydate.txt
Verify it:
cat mydate.txt

Please note that the file such as mydate.txt is overwritten unless the bash noclobber option is set using the set command. For example, turn off noclobber option:
set -o noclobber
echo «some data» > mydata.txt
Sample outputs:

We can turn on noclobber option as follows:
set +o noclobber
echo «foo bar» > mydata.txt

How to redirect stderr to a file

The syntax is as follows:
command &> file.txt
command &>> file.txt
OR
command 2> file.txt
command 2>> file.txt
Above works with bash and other modern shell. For POSIX version try:
command >output.txt 2>&1
command >>output.txt 2>&1
In this example, send the find command errors to a file named err.log:
find / -iname «*.conf» &>err.log
## OR ##
find / -iname «*.conf» 2>err.log
## POSIX version ##
find . -iname «*.conf» >err.log 2>&1
Verify it:
cat err.log
Sample outputs:

How to suppress error messages

Use the following syntax:
command 2>&-
find . -iname «*.txt» 2>&-
We can also redirect error messages (stderr) to standard output (stdout), run:
command 2>&1
echo «foo» 2>&1
kill $target_pid 2>&1 > /dev/null

How to redirect both stdout and stderr to a file

The syntax is as follows to redirect both stdout and stderr to a file:
command 2>&1 | tee output.txt
For example:
find . -iname «*.txt» 2>&1 | tee cmd.log
cat cmd.log
To append text to end of file use the following syntx:
find . -iname «*.conf» 2>&1 | tee -a cmd.log
cat cmd.log

How to combine redirections

The following command example simply combines input and output redirection. The file resume.txt is checked for spelling mistakes, and the output is redirected to an error log file named err.log:
spell error.log

How to redirect screen output (stdout) and errors (stderr) to /dev/null

Try the following syntax
command > /dev/null 2>&1
/path/to/script.py > /dev/null 2>&1

Redirect both standard error and standard out messages to a log file

command > log.txt 2>&1
/path/to/my-appname.py > my-appname.log 2>&1

Conclusion

You learned how to save terminal output to a file when using Linux or Unix-like operating system with modern shell such as Bash or KSH including POSIX syntax. See bash docs here for more info or type the following man command:
man bash

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Save Command Output to a File in Linux

There are many things you can do with the output of a command in Linux. You can assign the output of a command to a variable, send it to another command/program for processing through a pipe or redirect it to a file for further analysis.

In this short article, I will show you a simple but useful command-line trick: how to view output of a command on the screen and also write to a file in Linux.

Viewing Output On Screen and also Writing to a File

Assuming you want to get a full summary of available and used disk space of a file system on a Linux system, you can employ the df command; it also helps you determine the file system type on a partition.

Check Filesystem Disk Space

With the -h flag, you can show the file system disk space statistics in a “human readable” format (displays statistics details in bytes, mega bytes and gigabyte).

Disk Space in Human Readable Format

Now to display the above information on the screen and also write it to a file, say for later analysis and/or send to a system administrator via email, run the command below.

Linux Command Output to File

Here, the magic is done by the tee command, it reads from standard input and writes to standard output as well as files.

If a file(s) already exists, you can append it using the -a or —append option like this.

Note: You can also use pydf an alternative “df” command to check disk usage in different colors.

For more information, read through the df and tee man pages.

You may also like to read similar articles.

In this short article, I showed you how to view output of a command on the screen and also write to a file in Linux. If you have any questions or additional ideas to share, do that via the comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How to write the output into the file in Linux

How do I save terminal output to a file?

A command can receive input from a file and send output to a file.

Writing the output into the file

The syntax is
command > filename
For example, send output of the ls command to file named foo.txt
$ ls > foo.txt
View foo.txt using the cat command:
$ cat foo.txt
Please note that when you type ‘ls > foo.txt’, shell redirects the output of the ls command to a file named foo.txt, replacing the existing contents of the file. In other words, the contents of the file will be overwritten.

Appending the output or data to the file

The syntax is
command >> filename
For example the following will append data:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Verify it:
cat /tmp/data.txt

How to save the output of a command to a file in bash using tee command

The tee command read from standard input and write to standard output and files. The syntax is as follows for writing data into the file:
command | tee file.txt
Want to append data? Try
command | tee -a output.txt

Examples

Display output of the date command on screen and save to the file named /tmp/output.txt. If the output.txt already exists, it gets overwritten:
$ date | tee /tmp/output.txt
$ cat /tmp/output.txt
Same as above but append to the given files, do not overwrite file:
$ pwd | tee -a /tmp/test.txt
$ echo «Today is $(date)» | tee -a /tmp/test.txt
$ hostnamectl | tee -a /tmp/test.txt
$ cat /tmp/test.txt

The above commands will append the output to the end of the file, just like the shell >> operator as explained earlier.

I/O redirection summary for bash and POSIX shell

Shell operator Description Overwrite existing file?
command > output.txt Save terminal output (standard output) to a file named output.txt Yes
command >> output.txt Append terminal output (standard output) to a file named output.txt No
command Takes standard input from output.txt file N/A
command 0 Takes standard input from output.txt file N/A
command 1> output.txt Puts standard output to output.txt file Yes
command 1>> output.txt Appends standard output to output.txt No
command 2> output.txt Puts standard error to output.txt Yes
command 2>> output.txt Appends standard error to output.txt file No
command &> output.txt Puts both standard error and output to output.txt Yes
command > output.txt 2>&1 <POSIX> Puts both standard error and output to file named output.txt Yes
command &>> output.txt Appends both standard error and output to file named output.txt No
command >> output.txt 2>&1 <POSIX> Appends both standard error and output to file called output.txt No
command | tee output.txt Puts standard output to output.txt while displaying output on screen Yes
command | tee -a output.txt Appends standard output to output.txt while displaying output on screen No
command |& tee output.txt Puts both standard output and error to output.txt while displaying output on terminal Yes
command 2>&1 | tee output.txt <POSIX> Puts both standard output and error to file named output.txt while displaying output on terminal Yes
command |& tee -a output.txt Append both standard output and error to file called output.txt while displaying output on terminal No
command 2>&1 | tee -a output.txt <POSIX> Append both standard output and error to file named output.txt while displaying output on terminal No

Conclusion

You learned how to write the output to the file in Linux or Unix-like system when using bash or POSIX shell. We have:

  1. /dev/stdin (standard input) — File descriptor 0 is duplicated.
  2. /dev/stdout (standard output) — File descriptor 1 is duplicated.
  3. /dev/stderr (standard error) — File descriptor 2 is duplicated.

See I/O redirection documentation for more information. We can read bash man page as follows using the man command:
man bash

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Save the Output of Command in a File using Linux Terminal

We all know how running a command in the Linux command line, the Terminal, results in the execution of the command and printing of the results in the Terminal itself. Sometimes, this immediate display of output is not enough, especially if we want to save the output for later use. Fortunately, the Linux bash and all operating system bash, for that matter, is equipped with the ability to print the output of a command to a specified file. In this article, we will deal with the following two tasks:

  • Saving Command Output to A File
  • Printing Output on Terminal and Saving it to a File

The commands mentioned in this article have been executed on an Ubuntu 18.04 LTS system.

Saving Command Output to A File

We will run a few examples where the output of the commands will be saved on the file name we specify.

Create New File/Replace existing file

If you want to save the output of a command to a new file or replace the contents of an already existing file with the output of the command, please use the following syntax:

For example, I will use the lscpu command(that displays system information) and then print its contents to the file named systeminformation.txt. This file does not already exist on my system.

When I access this file through the file manager, it looks like this:

Append Output to Existing File

If you want to save the output of a Linux command to a file without messing with its already existing contents, you can use the following syntax:

For example, the following command will append the result of the ls command at the end of my already existing file systeminformation.txt.

The following file that once contained only my system information now also contains the output of my ls command:

Printing Output on Terminal and Saving it to a File

You might have noticed that the commands that we mentioned above only save the output to the file without printing them on the Terminal itself. Use the following syntax if you want to view the output of the command on the Terminal as well:

For example, the text that we want to echo in the following image will now be echoed on the Terminal and also printed to the file myfile.txt.

These are the contents of the file generated through the command:

If you want to append the output of a command to an already existing file. Please follow this syntax:

For example, the following image shows how some more text will be echoed and then added to my already existing file:

This is how the file looks like now:

These output oriented text files that we generated through this article can be much more useful in some cases than the usual printing of output on the terminal. That’s how powerful the Ubuntu bash is!

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Источник

Читайте также:  Что такое windows system32 igfxtray exe
Оцените статью