pgrep, watch, tee and xargs

LINUXLPIC1-101

2/28/2026

1. The pgrep command

Instead of using ps aux | grep nginx, you can use pgrep [name] and it will return just the PID of each process it finds with this name. It is most suitable for scripts.

Useful pgrep Flags:
  • pgrep -l [name]: Shows the PID and the process name (so you're sure you've got the right one).

  • pgrep -u [user] [name]: Only finds processes owned by a specific user.

  • pgrep -a [name]: Shows the full command line (arguments) used to start the process.

  • pgrep -P [PPID]: It lists all child processes belonging to a specific Parent PID.

2. How to use the watch command?

The watch command is used to run any command repeatedly at a regular interval and display the output in real-time. It’s essentially a way to turn a static command into a live dashboard.

The Syntax:

watch [options] "command"

Common Examples:
  • Monitor Disk Space: watch df -h (Updates every 2 seconds by default).

  • Monitor Memory Usage: watch free -m

3. The tee Command: The "T-Junction"

The tee command reads from standard input and writes to both standard output (your screen) and one or more files. It’s named after a plumber's "T" pipe.

Why it’s useful: Normally, if you use > to redirect to a file, the screen goes blank. tee lets you monitor the process in real-time while ensuring a permanent record is created.

If you need to append to a file instead of overwriting it, use the -a flag: command | tee -a existing_log.txt.

4. The xargs Command: The "Argument Builder"

xargs is a "power tool." It takes the output of one command and turns it into arguments for another command. This is essential when the second command doesn't support "piping" directly.

For example,

pgrep -u guest | xargs kill

A combination of several commands to get large files:

find /tmp -size +100M | tee large_files.txt | xargs rm