Log inSign up

Print, capture and count text with echo, tee and wc

Use echo (or printf for exact formatting) to print text, pipe output into tee to see it on screen and write it to a file at the same time, and pipe anything into wc -l to count lines. For files owned by root, echo "line" | sudo tee -a /etc/file is the reliable pattern, because a plain sudo echo >> file redirect runs without root rights.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Where do these three commands come from?

All three ship with every server image, so there is nothing to install. On Ubuntu 24.04, Ubuntu 22.04, Debian 12 and Debian 13, tee and wc come from the GNU coreutils package; Rocky Linux 9 and AlmaLinux 9 use the same coreutils, so the flags below behave identically on all four. echo exists twice: as a Bash builtin (the one you normally run) and as /usr/bin/echo. The builtin wins unless you type the full path.

type -a echo
tee --version | head -n 1
wc --version | head -n 1

Log in over SSH first (see your first SSH session) and work as a normal user with sudo, for example deploy. If you do not have one yet, follow create a sudo user.

How do you print text and variables with echo?

Give echo its arguments and it prints them separated by single spaces, then adds a newline. Double quotes keep spacing intact and still expand variables; single quotes print everything literally.

echo Hello from the server
echo "Host: $(hostname), user: $USER"
echo 'Literal $HOME is not expanded here'
echo -n "no newline at the end"; echo
echo -e "column1\tcolumn2\nnext line"

The -n flag drops the trailing newline and -e turns on escapes such as \t (tab) and \n (newline). Escape handling differs between shells, so scripts that must run under sh as well as Bash are safer with printf.

When is printf the better choice?

Pick printf whenever the output format matters, such as aligned columns, padded numbers or text without a final newline. It takes a format string followed by values and never adds a newline unless you ask for \n.

printf '%s\n' "first" "second" "third"
printf '%-12s %6s\n' "service" "port" "sshd" "22" "nginx" "443"
printf '%05d\n' 42
printf '%.2f GB free\n' 18.4567

The format string repeats for each group of values, which is why the second line prints three neat rows. Escapes always work in printf, so its behaviour does not change between Bash, dash and other shells.

How do you write a file as root with tee?

Pipe the text into sudo tee: tee is the program that opens the file, so it is tee that needs root, not echo. A redirect like sudo echo "x" > /etc/file fails with "Permission denied" because your own shell performs the redirect before sudo starts.

echo "198.51.100.24 app.example.net" | sudo tee -a /etc/hosts
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/90-swappiness.conf > /dev/null
sudo sysctl --system | grep swappiness

Use -a to append and leave it out to replace the whole file. The trailing > /dev/null hides the copy that tee would otherwise echo back to the terminal. For multi-line files, combine tee with a heredoc:

sudo tee /etc/motd > /dev/null <<'EOF'
Production web node, app.example.net
Changes go through the deploy user only.
EOF

The quoted 'EOF' marker stops the shell from expanding $ signs inside the block.

How do you keep a log of a command while you watch it?

Pipe the command into tee with a file name, and you get live output plus a saved copy. This is handy for long upgrades or migrations where you want proof of what happened.

sudo apt update 2>&1 | tee ~/apt-update.log
sudo dnf -y upgrade 2>&1 | tee ~/dnf-upgrade-$(date +%F).log
ping -c 4 198.51.100.24 | tee -a ~/network-notes.txt

The 2>&1 part folds error messages into the same stream, otherwise errors reach the screen but not the log. You can also send one stream to several files at once with tee one.log two.log. When a pipeline should fail if any stage fails, add set -o pipefail in scripts; the pipes and redirection guide explains why.

How do you count lines, words and bytes with wc?

Run wc on a file (or a stream) and it prints lines, words and bytes, in that order. Single flags narrow the output: -l lines, -w words, -c bytes, -m characters and -L the longest line.

wc /etc/passwd
wc -l /etc/passwd
wc -l < /etc/passwd
wc -c /var/log/syslog /var/log/auth.log

Reading from standard input (the < form) prints only the number, without a file name, which is cleaner inside scripts. On Rocky Linux and AlmaLinux the system log lives in /var/log/messages and SSH logins go to /var/log/secure, so adjust paths there.

What are useful everyday wc counts on a server?

The most common use is counting results from another command. A few practical one-liners:

# user accounts that can log in with a shell
grep -c -E '/(bash|sh|zsh)$' /etc/passwd
# established TCP connections right now
ss -Htn state established | wc -l
# files under a web root
find /var/www -type f | wc -l
# failed SSH password attempts today (Ubuntu/Debian)
sudo journalctl -u ssh --since today | grep -c "Failed password"

On Rocky and Alma the SSH unit is called sshd, so use -u sshd. Note that grep -c already counts matching lines, so piping grep into wc -l is only needed when other filters sit between them. The -H flag on ss drops its header line, which would otherwise add one to the total. For deeper text processing see grep and sed and awk.

How do you combine all three in a small health report?

Build each line with printf, count with wc, and hand the whole block to tee so it lands both on screen and in a dated file.

{
  printf 'Report for %s at %s\n' "$(hostname)" "$(date -Is)"
  printf 'Logged-in sessions: %s\n' "$(who | wc -l)"
  printf 'Listening TCP ports: %s\n' "$(ss -Hltn | wc -l)"
  printf 'Packages installed: %s\n' "$(dpkg -l 2>/dev/null | grep -c '^ii')"
} | tee ~/report-$(date +%F).txt

On Rocky Linux or AlmaLinux swap the package line for rpm -qa | wc -l. Schedule the block with cron or a systemd timer if you want a daily snapshot.

Why does my count or file look wrong?

  • wc -l is one short: the last line has no trailing newline. wc counts newline characters, not lines of text. Add one with echo >> file or use awk 'END{print NR}' file.
  • tee wiped the file: you left out -a. Without it tee truncates first.
  • Permission denied with sudo: the redirect ran as your user. Move sudo onto tee as shown above.
  • Literal -e appears in output: the script runs under sh, where echo handles flags differently. Switch to printf.
  • Bytes and characters differ: UTF-8 text uses several bytes per character, so wc -c and wc -m disagree on non-ASCII text. That is expected.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
S1-2plenty for learning the shell and small scripts1 vCPU, 2 GB RAM, 55 GB SSD$6.00/mo$72.00 for 12 months

Questions about echo, tee and wc

Is echo or printf safer inside scripts?

printf. Its escape handling is defined the same way in every POSIX shell, while echo flags like -e and -n vary between Bash, dash and BusyBox.

Can tee write to a file and pipe on to another command?

Yes. tee copies its input to every file you name and also passes it to standard output, so cmd | tee out.txt | grep error saves everything and still filters.

How do I count only non-empty lines?

Filter first: grep -c . file counts lines that contain at least one character, and grep -cv '^\s*$' file also skips lines that hold only spaces.

Does wc work on very large logs?

Yes, it streams the file and uses very little memory. On a multi-gigabyte log, wc -l is limited mostly by disk read speed, which NVMe plans handle quickly.

Why does sudo tee print my text again?

tee always writes to standard output as well as to the file. Add > /dev/null at the end of the command to silence that copy.