Save output of the terminal to a file under Linux or Unix bash

    How do I save the terminal output to a file when using BASH/KSH/CSH/TCSH on Linux, MacOS, *BSD or Unix-like operating systems?

    Yes, we can save the output of the command by redirecting it to a file. The standard flows for input, output and errors are as follows (also called file descriptors)


    1. stdin (numeric value 0) – keyboard
    2. stdout (numeric value 1) – screen/display
    3. stderr (numeric value 2) – screen/display

    How to save the output of the terminal to file
    So 1 means stdout and 2 means stderr. Based on the information at our disposal:

    • stdout/stderr redirect to a file
    • divert a stdout to a stderr OR divert a stderr to a stdout
    • To redirect stderr and stdout to a file
    • We can also divert stderr and stdout to stdout.
    • And finally, you can redirect stderr and stdout to stderr

    How to save the output of the terminal in afile

    By default, the command sends the output to stdout and can be redirected to the file using the following syntax For example, save the output of the date command in a file named madate.txt, enter :date > madate.txt To view the file, use the command cat :cat madate.txt

    Data transfer to our orders (diversion of inputs)

    We can read the items in a file using the following simple syntax, and the file must already exist: Command
    < input.txt
    cat < /etc/passwd
    wc -l < /etc/passwd

    Adding an output to a file

    If the file name .txt/mondate.txt (file) already exists, it will be overwritten. To connect the output, start Command
    >> file name.txt
    ls -l /etc/resolv.conf >> madate.txt
    cat mydate.txt
    How to save the terminal output to a
    file Note that the file will be overwritten as mydate.txt unless the bash noclobber option is set with the set command. Switch z. B. Set the No-Clobber option to :
    set -o No-Clobber
    echo some data > mydata.txt
    sample outputs :

    bash: mydata.txt: cannot overwrite an existing file

    We can activate the No-Clobber option as follows:
    set +o noclobber
    echo foo bar > mydata.txt

    How can I redirect stderr to afile?

    The syntax is as follows:
    Command &> file.txt
    Command &> file.txt
    Command 2> file.txt
    Command 2>> file.txt
    The above file works with bash and other modern shells For the POSIX version, try : 1A Command >output.txt 2>&1Command >>output.txt 2>&D In this example, send the command find to a file named err.log:find / -iname *.conf &>err.log### OR ##find / -iname *.conf 2>err.log### POSIX version ##find . -iname *.conf >err.log 2>&1Check it:cat err.logOutput

    you will find: ./ :
    Search for: ‘./systemd-private-timesyncd.service-KOh0jg’ : Authorization refused to find
    : ./snap.demo : Authorization refused to find
    : ./snap.lxd : Authorization refused to find
    : ./.vbox-root-ipc: Authorization refused

    How to delete error messages

    Use the following syntax:
    Find Command 2>&-
    . -in name *.txt 2>&-
    We can also redirect error messages (stderr) to the default output (stdout), run it out
    Command 2>&1
    echo foo 2>&1
    kill $target_pid 2>&1 > /dev/null

    How can I redirect both stdout and stderr to afile?

    The syntax is as follows to redirect both stdout and stderr in a single file
    Command 2>&1 | tee output.txt
    For example:
    find . -in name *.txt 2>&1 | tee cmd.log
    cat cmd.log
    To add text to the end of the file, use the following syntax
    find . -in name *.conf 2>&1 | tee -a cmd.log
    cat cmd.log

    How to combine shipping

    The following control example simply combines input and output bypass. The summary.txt file is checked for spelling errors and the output is redirected to an error log file named err.log:
    spelling error.log

    Try the following syntax
    Command > /dev/null 2>&1
    /path/to/ > /dev/null 2>&1

    Reroute standard error and standard output messages to alog file

    Command > log.txt 2>&1
    /Path/to/ > myname.log 2>&1


    You have learned how to save the terminal output to a file when using Linux or a Unix-like operating system with a modern shell like Bash or KSH, including POSIX syntax. For more information, see the bash documents here or type the following man assignment:
    man bash


    Compiled by : Vivek kite

    The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and trainer for the Linux/Unix shell scripting operating system. Get the latest tutorials on SysAdmin, Linux/Unix and Open Source via an RSS/XML feed or a weekly email terminal output to file linux,python save terminal output to file,save terminal history to file,how to save command line output in text file linux,linux redirect output to file and screen,save terminal output to file mac,shell script output to log file,shell script redirect output to file

    Recent Articles

    FedRAMP – What’s the Big Deal?

      In case you are somebody who works for a cloud service supplier within the enterprise of federal contracting, you in all probability have already...

    Planning a Game Night? Here Are 5 Of the Best Multiplayer Games You Can Enjoy With Friends – NoobsLab

      It has been months because the quarantine has began, and we perceive that cabin fever is getting the perfect of us. The perfect factor...

    Bash How to Execute a Command in a Variable? – Linux Hint

    Bash scripts will be created in a wide range of alternative ways and most of us are aware of executing the straightforward instructions inside...

    Things you should know about IP address

      You employ wifi for years however don’t know concerning the IP deal with and all the time should ask for assist every time...

    55 New security vulnerabilities reported in Software and Services for Apple

      A workforce of 5 safety researchers analyzed a number of Apple on-line providers for 3 months and located as many as 55 vulnerabilities, 11...

    Related Stories