LinuxCommandLibrary

logsave

Record terminal output to a file

TLDR

Execute command with specified argument(s) and save its output to log file

$ logsave [path/to/logfile] [command]
copy

Take input from stdin and save it in a log file
$ logsave [logfile] -
copy

Append the output to a log file, instead of replacing its current contents
$ logsave -a [logfile] [command]
copy

Show verbose output
$ logsave -v [logfile] [command]
copy

SYNOPSIS

logsave [options] logfile command [arguments]

PARAMETERS

-a
    Append to logfile instead of overwriting it.

-c
    Create logfile if it doesn't already exist. (This may be the default behavior on some systems).

-s
    Suppress logfile creation. If the logfile does not exist, no output will be saved.

-v
    Verbose mode. Print additional information to standard error.

logfile
    The file to save the command output to.

command [arguments]
    The command to execute and log the output of.

DESCRIPTION

The logsave command executes a given command and saves its output to a log file. It's a simple utility primarily designed for logging the output of scripts or programs, often used in cron jobs or automated tasks where capturing the output is necessary for auditing or debugging. logsave prepends the log file with the date and time of the command execution. It's a lightweight alternative to more complex logging solutions and is useful when you need a quick and easy way to capture the output of a command without extensive configuration.
The command creates a new log file or appends to an existing one if the -a (append) option is given. Standard error is redirected to standard output before being saved to the log file. The exit code from the command that is run is kept and passed back after the log is written to disk.

CAVEATS

logsave redirects stderr to stdout before saving it to the file. This means that error messages and standard output will be mixed together in the log file. If fine-grained separation of the two streams is needed, use redirection operators (2>&1) directly within the command itself or utilize dedicated logging tools.

EXIT STATUS

The logsave command returns the exit status of the command that was executed. If the command succeeds, logsave will return 0. If the command fails, logsave will return the failure code of the command. If logsave fails itself (e.g., due to a permissions error), it will return a non-zero exit status.

EXAMPLES

To log the output of `ls -l` to `mylog.txt`:
logsave mylog.txt ls -l

To append the output of `date` to `mylog.txt`:
logsave -a mylog.txt date

HISTORY

The logsave command has been a part of various Unix-like operating systems for a considerable amount of time. Its primary goal has consistently been to offer a simple and direct means of capturing command output to a file. It's designed to be easily integrated into scripts and cron jobs.

SEE ALSO

script(1), tee(1)

Copied to clipboard