florit
florit

Reputation: 345

How can I redirect stout of each command to a separate log file in bash?

I have a bash script, using commands like ffmpeg, zip and scp to convert a movie file, zip it and upload it to a server. I’d like to redirect stout of these commands to separate files. Is there a way to achieve this, without having to specify the log file individually after each of these commands?

ffmpeg -> ./ffmpeg.log
zip -> ./zip.log
scp -> ./scp.log

Something like: exec > >(tee "./replace-this-with-each-command.log") 2>&1 at the beginning of my script.

Upvotes: 1

Views: 185

Answers (1)

KamilCuk
KamilCuk

Reputation: 141493

Just write a function that redirects the data to a filename based on the command.

log() {
   "$@" > >(tee "./$(basename "$1").log")
}
log ffmpeg
log zip
log scp

Upvotes: 4

Related Questions