Thursday, April 22, 2010

Sending an e-mail with attachment using mutt command line interface

Sending an e-mail message with an attachment file using command line interface is a useful operation. It is a convenient way, say, to backup some small files.

$> echo "Hello! This is a backup." | mutt -s "backup" -a '/tmp/backup.tgz' -- 'mail@example.com'

This command sends an e-mail message with a body specified as an argument of echo command ("Hello! This is a backup.").

The subject of an e-mail is given with -s argument (-s "backup").

Attachment is specified with -a option (-a '/tmp/backup.tgz').

Be sure to specify two dashes (--) before an e-mail address ('mail@example.com'), otherwise you get "No recipients specified." message on some systems.

How to search file in Linux using a locate command

The idea of locate command is straightforward.

If a search pattern does not contain any metacharacters, locate command displays all file names that contain that string at any position of the path:

$> locate yastbrowser
/usr/share/YaST2/yastbrowser
/usr/share/YaST2/yastbrowser/application.ini
/usr/share/YaST2/yastbrowser/chrome
/usr/share/YaST2/yastbrowser/chrome/chrome.manifest
/usr/share/YaST2/yastbrowser/chrome/classic-browser.jar
/usr/share/YaST2/yastbrowser/chrome/en-US.jar
/usr/share/YaST2/yastbrowser/chrome/yastbrowser.jar
/usr/share/YaST2/yastbrowser/components
/usr/share/YaST2/yastbrowser/components/nsCmdlineHandler.js
/usr/share/YaST2/yastbrowser/defaults
/usr/share/YaST2/yastbrowser/defaults/preferences
/usr/share/YaST2/yastbrowser/defaults/preferences/yastbrowser-prefs.js


If pattern does contain metacharacters, locate shows only matching pathes:

$> locate *yastbrowser
/usr/share/YaST2/yastbrowser

Wednesday, April 21, 2010

How to generate strong password in Linux

The simplest way to generate strong password in Linux is to use the pwgen command:
$> pwgen -y
The  "-y" parameter instructs the program to include at least one special character in the password.
As usual, for details see
$> man pwgen 

Tuesday, April 20, 2010

Print information on Ethernet interface in Linux

# ethtool eth0
The command is to be executed in the superuser mode.
It prints information on the Ethernet interface (eth0), such as Wake-on-LAN options, etc.

In order to disable Wake-on-LAN feature execute the following:
# ethtool -s eth0 wol d 

Monday, April 19, 2010

Print desired number of hexdump lines with xxd

$> xxd -l 0xN0 FILE
The command prints the first N hexdump lines (-l 0xN0) of the file (FILE).
The stuff works because each line of xxd output consists of 16=0x10 octets by default, so 0xN0 is the number of octets in the first N lines.

The other useful way to view binary file is the following: 
$> xxd FILE | less 

Wednesday, January 27, 2010

Use grep recursively

$> grep -r --include='*.c' ll_que .
recursivly (-r) search all subdirectories of current directory (.) for files matching pattern (--include='*.c') and containing some line (ll_que).

Sunday, November 8, 2009

How to fix some sort of mp3-files

#!/bin/bash

# 
# I had some mp3-files that were repeated twice inside themselfes.
# It looked like someone copied them one day with append command.
# The script splits such files into to equal parts and checks if
# those parts are coinside.
#

for f in *.mp3 ; do
    # Calculate size of the file
    size=`du -b "${f}" | cut -f 1`
    half_size=`echo ${size}/2 | bc`

    # Split file into two parts
    f1="$f".1
    f2="$f".2
    head -c $half_size "$f" > "${f1}"
    tail -c $half_size "$f" > "${f2}"

    # Calculate checksums and check if they coinside
    md1=`md5sum "${f1}" | cut -f 1 -d " "`
    md2=`md5sum "${f2}" | cut -f 1 -d " "`
    if [ ${md1} != ${md2} ] ; then
        echo "Problems with $f"
    fi
done