How do I watch a long command run AND save its complete output?

Quick Linux Tip #42:

Try: ./deploy.sh 2>&1 | tee -a deploy.log

Info: tee splits stdout to both terminal and file. 2>&1 redirects stderr into stdout first to capture error logs. -a appends output instead of overwriting.

Examples:

  • $ ./script.sh 2>&1 | tee output.log  # Overwrite log with output + errors
  • $ make 2>&1 | tee build.log | grep -E 'error|warning'  # Log all, filter display
  • $ sudo apt upgrade 2>&1 | tee -a /var/log/upgrades.log  # Append system upgrade log

Note: Without 2>&1, tee only captures standard output and misses error messages. Use tee -a when running repeated scripts to maintain a history.




LinuxTeck.com
linuxteck@ubuntu:~$ ./deploy.sh 2>&1 | tee -a deploy.log
[INFO] Starting deployment process...
[INFO] Pulling latest code updates...
[WARN] Environment variable PORT not set (default: 8080)
[SUCCESS] Deployment completed!

linuxteck@ubuntu:~$ cat deploy.log
[INFO] Starting deployment process...
[INFO] Pulling latest code updates...
[WARN] Environment variable PORT not set (default: 8080)
[SUCCESS] Deployment completed!
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #41: How do I find duplicate files even if they have different names? NEXT ARTICLE Quick Linux Tip #43: How do I extract config values from between BEGIN and END markers?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.