Friday 28 March 2014

Script for disc space alert in Linux


#!/bin/bash

#admin email account
ADMIN="mymailid@domain.com"

# set usage alert threshold
THRESHOLD=95

#hostname
HOSTNAME=$(hostname)

#mail client
MAIL=/bin/mail

# store all disk info here
EMAIL=""

for line in $(df -hP | egrep '^/dev/' | awk '{ print $6 "_:_" $5 }')
do

        part=$(echo "$line" | awk -F"_:_" '{ print $1 }')
        part_usage=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )

        if [ $part_usage -ge $THRESHOLD -a -z "$EMAIL" ];
        then
                EMAIL="$(date): Running out of diskspace on $HOSTNAME\n"
                EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"

        elif [ $part_usage -ge $THRESHOLD ];
        then
                EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"
        fi
done

if [ -n "$EMAIL" ];
then
        echo -e "$EMAIL" | $MAIL -s "Alert: Partition(s) almost out of diskspace on $HOSTNAME" "$ADMIN"
fi

Script to Delete file older than 30 days in Linux


#! /bin/bash
find /backup/mysql/* -type f -mtime +30 -exec rm {} \;



Thursday 27 March 2014

Determine the File System type in Linux


blkid /dev/sda1

blkid -t TYPE=ext4

sudo parted -l

file -s /dev/sda1

tune2fs -l /dev/sda1 | grep features

df -T

mount

cat /proc/mounts

cd /proc/fs and ls

cd /sys/fs and ls

cat /etc/fstab

dumpe2fs /dev/sdb1 | head -15 -- has_journal means it is ext3.

dumpe2fs /dev/sdb1 | head -15 -- has_journal means it is ext4.

Ref : - http://unix.stackexchange.com/questions/60723/how-do-i-know-if-a-partition-is-ext2-ext3-or-ext4

Thursday 6 March 2014

Listing only Directory in Linux


The following commands will list all of the sub directories in the present directory,



ls -d */ | xargs -l basename

ls -d */ | cut -d/ -f1

ls -d */ | cut -f1 -d'/'

ls -p | grep "/" | cut -f1 -d'/'


echo */ | cut -f1 -d'/'


find . -maxdepth 1 -mindepth 1 -type d -printf %P\\n

find . -mindepth 1 -maxdepth 1 -type d  \( ! -iname ".*" \) | sed 's|^\./||g'

find [!.]* -maxdepth 0 -type d


for f in *;do if [[ -d $f  ]]; then echo $f;fi; done;