Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Linux Tee Command with Examples - DesignLinux

Oct 11 2020
designlinux 0 Comments
Linux Tee Command with Examples

The tee command is used to read the standard input and write and writes to both standard output and one or more files at the same time. Generally, tee is used with combination of other commands through piping. In this tutorial, we’ll cover the basics of using the tee command.

tee Command Syntax#

Below is the basic syntax of tee command:

tee [OPTIONS] [FILE]
  • Here, options are:
    • -a (--append) – It will append to the given file instead of overwrite.
    • -i (--ignore-interrupts) – Ignore interrupt signals.
    • Use tee --help to view all available options.
  • FILE – One or more files. Each of which the output data is written to.

How to Use the tee Command#

Usually, the tee command is used to display the standard output (stdout) of a program and write it in a file.

For example, we will get the disk space details using df command and store the output to the output.txt file by piping with tee command:

df -h | tee output.txt
Filesystem      Size  Used Avail Use% Mounted on
udev            984M     0  984M   0% /dev
tmpfs           200M  624K  199M   1% /run
/dev/vda1        25G  3.8G   21G  16% /
tmpfs           997M     0  997M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           997M     0  997M   0% /sys/fs/cgroup
/dev/vda15      105M  3.6M  101M   4% /boot/efi
tmpfs           200M     0  200M   0% /run/user/1000

You can view the content of the output.txt file using the cat command.

Write to Multiple File#

You can also use tee command to write to multiple files. Pass the list of files separated by space as arguments:

command | tee file1.txt file2.txt file3.txt

Append to File#

Use the -a (--append) option to append the output to the file. The tee command will overwrite the specified file.

command | tee -a file.txt

Ignore Interrupt#

You can use -i (–ignore-interrupts) option to ignore interrupts. It’s useful when stopping the command during execution with Ctrl+C and want to exit gracefully.

command | tee -i file.txt

Hide the Output#

To hide the standard output, redirect it to /dev/null:

command | tee file.txt >/dev/null

Using tee in Conjunction with sudo#

When you try to write to a file owned by a root or sudo user, it will throw permission denied error. You should perform the redirection using sudo user.

sudo echo "newline" > /etc/nginx/file.conf

It will show something like this:

bash: /etc/nginx/file.conf: Permission denied

Simply prepend sudo before the tee command as shown below:

echo "newline" | sudo tee -a /etc/file.conf

The echo command will pass the output to the tee command and elevate to sudo permissions and write to the file.

Using tee in conjunction with sudo allows you to write to files owned by other users.

Conclusion#

The tee command reads from standard input and writes it to standard output and one ore more files.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: tee, terminal

How to Get and Change the Current Working Directory in Python

Prev Post

Wall command in Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 49
  • 605,799

DesignLinux.com © All rights reserved

Go to mobile version