Tuesday, October 22, 2013

Send a file as an attachment in linux

Have a file you need sent as an attachment, not an inline message?  Use mutt and this example command.  the trick is the "--" which

The meaning of the double-dash "--" is:
-- Used with a Bash builtin, it means the end of options to that particular command.
-- The double-dash is also used in conjunction with set.


echo "This is the body" |mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com

NOTES:
"-" a single dash can be used for:
-- redirection from/to stdin or stdout [dash]
-- Example: cat -
-- Example: tar cf - .
-- 
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
-- The "-" can be used to pipe stdout to other commands. This permits such stunts as prepending lines to a file.
-- example: grep Linux file1 | diff file2 -




Monday, February 27, 2012

RHEL 5 yum: Get repo package was installed from

If you need to get the source (repo) that a package was installed from:


1) yum -y install yum-utils 

2) sudo repoquery --repoid=<reponame> -a |xargs sudo yum list installed| egrep -i <packagename>


Also found:

Available:
yum --disablerepo=* --enablerepo=<repo_name> list | grep -v "installed" | grep <package_name>

Installed:
yum --disablerepo=* --enablerepo=<repo_name> list | grep "installed" | grep <package_name>

Thursday, February 16, 2012

RHEL: Identify if you are on a virtual machine (vm)

So you need to tell if you are working on a server that is a virtual machine or on physical hardware? This is for RedHat (RHEL), but may work on other *nix flavors...


cat /sys/class/dmi/id/product_name

or

/usr/sbin/virt-what

or

If you suspect that it may be a vmware vm, you can search for vmware tools.  Make sure path is set correctly, and:

vm<tab>

Tuesday, February 14, 2012

linux:shell Loop on a space delimited varialble


var="string with spaces"

# note that $var must NOT be quoted here!
for X in $var
do
    echo "$X"
done

Linux:shell see if arg1 has been provided

An easy way to tell if arg1 has been given when user called the script:

# Note the quotes around $1

if [ -z "$1" ]; then
    usage
else
    do-something
fi

Tuesday, February 7, 2012

Convert epoch time to date/time format

date -d @<epoch_time>

Example: date -d @1328608176
Gives: Tue Feb  7 04:49:36 EST 2012



Monday, January 30, 2012

Quickly Make a Backup of a File in Linux

So you are Admin-ing away, and you are following SOP and making backups of all the files you edit.  Right?  However, as you have 100 servers to do and 5 files on each, that is a lot of typing!  You become lazy and forget the backup on one file, and then it happens.  A reboot hangs and you have no idea why, except that you were just editing system files...

Here is a quick way to backup any file:
cp /path/to/file{,.bak}

Example:
cp /etc/fstab{,.bak.2012-01-30}

The above example will produce a file named: /etc/fstab.bak.2012-01-30

This may seem a trivial savings in typing, but when you are scripting or in a hurry it can be a great time saver.  It also protects the backup file name from any typos. 

Make those backups and save yourself the frustration of having to go into single user to fix it!  Better procedures equal less downtime.