Redirect and send output of apt-get command error into a file on Ubuntu

I am writing script with apt-get command. Is it possible to send only errors of apt-get command into a file /root/apt-get-error.log on Ubuntu Linux?

You can redirect output easily.

Redirecting both STDERR and STDOUT with bash

All you have to do is type:

sudo apt-get -y command &> /root/log-stdout-error.txt
sudo apt -y command &> /root/log-stdout-error.txt

Alternative syntax is:

sudo apt-get -y install bar > /root/log.txt 2>&1
## append it 
sudo apt -y remove mysql-client foo bar >> /root/log.txt 2>&1

You may want to set DEBIAN_FRONTEND=noninteractive when doing this:

export DEBIAN_FRONTEND=noninteractive
apt -y install foo  &> /root/log-stdout-error.txt

Redirecting only stderr

The syntax is:

sudo apt-get -y install something 2>/root/file.log
## append stderr to a file #
sudo apt-get -y install something 2>>/root/file.log

Checkout my guide that explains various ways:


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki