Bash script to find average file size in a directory

If you want output in Kb


 ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024 "k"}'
Average file size: 28683.4k

If you want output in MB
ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024/1024 "MB"}'
Average file size: 28.0112MB

If you want output in GB

ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024/1024/1024 "GB"}'
Average file size: 0.0273546GB



Step by Step method to upgrade RHEL5 to RHEL6



First on current RHEL5 backup the list of packages installed just in case if you want to cross verify.

rpm -qa --queryformat "%{NAME}\n" | sort > /root/all_old_packages

--------------------------------
Check Selinux is enabled or not if enable disable it and uninstall it .

command used is

sestatus

SELinux status:                 disabled  ( in my case it is disabled )
---------------------------------
 vi /etc/inittab change to runlevel 3

id:3:initdefault:  ( mine is a server which is already in runlevel 3 )

-------------------------------------

remove old docs

rm -rf /usr/share/doc/HTML/*/docs/common ( this is mandatory else upgrade will fail)

-----------------------------------------

Now boot with RHEL6 DVD on boot menu press esc

then enter " linux upgradeany "

Follow instructions and you are done . IF you face any issues you can got to other run levels and check what caused failure. You can troubleshoot that issue and upgrade. During my installation i faced only one issue . That is the reason i have deleted old documents.


How to check syntax of dhcpd (dhcpd.conf file )

/usr/sbin/dhcpd -t -cf /etc/dhcpd.conf

 Some other quick tips:




1. Syntax checking for dhcpd files:



/usr/sbin/dhcpd -t -cf /etc/dhcpd.conf



This will do a basic syntax check to find those missing ; or other odd things.



2. host {} are global. You will see this warning if you place the host in a subnet zone. This is because when ISC will parse the file and make hostnames work globally (you can also make it do neat if then things but that makes my head hurt.) For the most part it is useful to put hosts in a group setting. This keeps them out of subnets, and also allows you to put common items with them (say for PXE booting with cobbler).



group {

next-server rhn.example.com;

filename “pxelinux.0″;



host noah {

hardware ethernet 00:11:00:11:00:11;

fixed-address 192.168.1.121;

}

}

How to get list of all cron jobs ( crontab ) in a server

Run below as root :


for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done


If you want to see who owns that cron job run below :

for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done


getent passwd cut -d: -f1 perl -e'while(<>){chomp;$l = `crontab -u $_ -l 2>/dev/null`;print "$_\n$l\n" if $l}'

Bash Script to get public ip

#!/bin/bash




pubip=$( curl http://ip4.me 2>/dev/null | sed -e 's#<[^>]*>##g' | grep '^[0-9]' )



echo $pubip



you can replace curl command with



curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'