Admins eHow SysAdmin Tips & Tricks

June 19, 2011

Linux Redirection Cheat Sheet

Filed under: Bash,linux — Tags: , , , , — admin @ 2:32 pm
Normal Redirect:

command > filename        Redirect command output to a file
command >> filename       APPEND into a file
command < filename        Type a text file and pass the text to command
commandA  |  commandB     Pipe the output from commandA into commandB
commandA &  commandB      Run commandA and then run commandB
commandA && commandB      Run commandA, if it succeeds then run commandB
commandA || commandB      Run commandA, if it fails then run commandB

Numeric handles:

STDIN  = 0  Keyboard input
STDOUT = 1  Text output
STDERR = 2  Error text output

command 2> filename       Redirect any error message into a file
command 2>> filename      Append any error message into a file
command > file 2>&1       Redirect errors and output to one file
command > file 2<&1       Redirect output and errors to one file
command > fileA 2> fileB  Redirect output and errors to separate files
command 2>&1 >filename    This will fail!

Redirect to /dev/null (hide errors):

command 2> /dev/null            Redirect error messages to /dev/null
command >/dev/null 2>&1         Redirect error and output to /dev/null
command >filename 2> /dev/null  Redirect output to file but suppress error

Source :

March 13, 2010

Shell script to show network speed

Filed under: CentOS,Debian,DreamBox,General — Tags: , , , , , , , , , — admin @ 11:37 am

The following shell script shows current download and upload speeds for the network interface you choose.

Copy the shell script in a file named, i.e:

Then after setting execution permissions:

chmod a+x

You can run the shell script passing as the first argument the network interface you want to monitor:

./ eth0

You will get a line like that:
eth0 DOWN:15 KB/s UP:880 B/s

This script works parsing /proc/net/dev file and calculating the difference between current transmitted or received bytes and their values one second ago.


# This shell script shows the network speed, both received and transmitted.

# Usage: interface
#   e.g: eth0

# Global variables

# This function parses /proc/net/dev file searching for a line containing $interface data.
# Within that line, the first and ninth numbers after ':' are respectively the received and transmited bytes.
    line=$(cat /proc/net/dev | grep $interface | cut -d ':' -f 2 | awk '{print "received_bytes="$1, "transmitted_bytes="$9}')
    eval $line

# Function which calculates the speed using actual and old byte number.
# Speed is shown in KByte per second when greater or equal than 1 KByte per second.
# This function should be called each second.

    let vel=$value-$old_value
    let velKB=$vel/1024
    if [ $velKB != 0 ];
 echo -n "$velKB KB/s";
 echo -n "$vel B/s";

# Gets initial values.

# Shows a message and waits for one second.
echo "Starting...";
sleep 1;
echo "";

# Main loop. It will repeat forever.
while true; 

    # Get new transmitted and received byte number values.

    # Calculates speeds.
    vel_recv=$(get_velocity $received_bytes $old_received_bytes)
    vel_trans=$(get_velocity $transmitted_bytes $old_transmitted_bytes)

    # Shows results in the console.
    echo -en "$interface DOWN:$vel_recv\tUP:$vel_trans\r"

    # Update old values to perform new calculations.

    # Waits one second.
    sleep 1;


Source : Linux Clues

Powered by WordPress