Linux menu

Showing posts with label Linux Unix Free Shell Scripts. Show all posts
Showing posts with label Linux Unix Free Shell Scripts. Show all posts

Thursday, September 25, 2014

Linux Shell Script Backup MySQL Database

#!/bin/bash
### Create Directory with Date where Database backup will be stored. ####
month=$(date | awk ‘{print $2}’)
day=$(date | awk ‘{print $3}’ )
year=$(date | awk ‘{print $6}’)
foldername=$(echo $day$month$year”_backups”)
### List all the databases in /usr/local/dblist file. ####
mysql -u root -p’mysqlpassword’ -e ‘show databases’ >/usr/local/dblist
list=$(cat /usr/local/dblist)
echo $foldername
### Create Backup Directory in /Backup/mysqlbackup …  ####
mkdir -p /Backup/mysqlbackup/$foldername
for i in $list
do
echo $i
mysqldump -u root -p’mysqlpassword’ $i | gzip > /Backup/mysqlbackup/$foldername/$i.sql.gz
echo ” “$i”.sql.gz file saved..”
done
You can put this shell script in crontab and run everyday. In this way you will have daily backups of all your databases.

Sample Output

./mysql.sh
17Sep2013_backups
database1
database1.sql.gz file saved…
hello_db
hello_db.sql.gz file saved…
site2
site2.sql.gz file saved…
test
test.sql.gz file saved…

Wednesday, September 24, 2014

Linux Shell Script Backup MySQL Database

#!/bin/bash
### Create Directory with Date where Database backup will be stored. ####
month=$(date | awk ‘{print $2}’)
day=$(date | awk ‘{print $3}’ )
year=$(date | awk ‘{print $6}’)
foldername=$(echo $day$month$year”_backups”)
### List all the databases in /usr/local/dblist file. ####
mysql -u root -p’mysqlpassword’ -e ‘show databases’ >/usr/local/dblist
list=$(cat /usr/local/dblist)
echo $foldername
### Create Backup Directory in /Backup/mysqlbackup …  ####
mkdir -p /Backup/mysqlbackup/$foldername
for i in $list
do
echo $i
mysqldump -u root -p’mysqlpassword’ $i | gzip > /Backup/mysqlbackup/$foldername/$i.sql.gz
echo ” “$i”.sql.gz file saved..”
done
You can put this shell script in crontab and run everyday. In this way you will have daily backups of all your databases.

Sample Output

./mysql.sh
17Sep2013_backups
database1
database1.sql.gz file saved…
hello_db
hello_db.sql.gz file saved…
site2
site2.sql.gz file saved…
test
test.sql.gz file saved…

Linux Shell Script To Delete Empty Directories

#!/bin/bash
#Check if user input parameter, if not ask to enter directory
if [ x"$1" = "x" ]; then
#Ask user to input directory where to start search for empty directories.
echo -n “Please enter directory where to delete empty folders: ”
#we read input
while read dir
do
#we check if input empty
test -z “$dir” && {
#if input empty – we ask once more to input directory
echo -n “Please enter directory: ”
continue
}
#if entered no empty data – continue to do other things
break
done
#if user entered parameter do next:
else
#dirname will be passed parameter
dir=$1
fi
#this check if directory exist, exit if not
if [ ! -d $dir ]; then
echo “No such directory”
exit 1
fi
#We will store list of all directories in temporary file
DirList=/tmp/ditlist.tmp
# we search for all directories
find $dir -type d > $DirList
#writing all directories to vatiable
dirs=`cat $DirList`
#start checking every directory
for dir in $dirs
do
#we are checking if directory is empty
[ `ls $dir | wc -l` -lt 1 ] || continue
#this ask user if really delete directory
echo -n “Remove empty directory $dir: [No/yes] ”
#reading users answer:
read answer
#Checing answer, if yes – we will delete folder, nothing in other case:
if [ "$answer" = "yes" ]; then
rmdir “$dir”
fi
done

Shell Script Output

test@centos:~$ rmempty.sh /tmp/test
Remove empty directory /tmp/test/test1: [No/yes] yes
Remove empty directory /tmp/test/test3/test5: [No/yes] yes
test@centos:~$

Linux Shell Script To Find Memory Usage Of An Application/Program In Linux

Memstat.sh is a shell script that calculates linux memory usage for each program / application. Script outputs shared and private memory for each program running in linux. Since memory calculation is bit complex, this shell script tries best to find more accurate results. Script use 2 files ie /proc//status (to get name of process) and /proc//smaps for memory statistic of process. Then script will convert all data into Kb, Mb, Gb. Also make sure you install bc command. 

Memstat.sh Shell Script

#!/bin/bash
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo “This script must be run as root” 1>&2
exit 1
fi
### Functions
#This function will count memory statistic for passed PID
get_process_mem ()
{
PID=$1
#we need to check if 2 files exist
if [ -f /proc/$PID/status ];
then
if [ -f /proc/$PID/smaps ];
then
#here we count memory usage, Pss, Private and Shared = Pss-Private
Pss=`cat /proc/$PID/smaps | grep -e “^Pss:” | awk ‘{print $2}’| paste -sd+ | bc `
Private=`cat /proc/$PID/smaps | grep -e “^Private” | awk ‘{print $2}’| paste -sd+ | bc `
#we need to be sure that we count Pss and Private memory, to avoid errors
if [ x"$Rss" != "x" -o x"$Private" != "x" ];
then
let Shared=${Pss}-${Private}
Name=`cat /proc/$PID/status | grep -e “^Name:” |cut -d':’ -f2`
#we keep all results in bytes
let Shared=${Shared}*1024
let Private=${Private}*1024
let Sum=${Shared}+${Private}
echo -e “$Private + $Shared = $Sum \t $Name”
fi
fi
fi
}
#this function make conversion from bytes to Kb or Mb or Gb
convert()
{
value=$1
power=0
#if value 0, we make it like 0.00
if [ "$value" = "0" ];
then
value=”0.00″
fi
#We make conversion till value bigger than 1024, and if yes we divide by 1024
while [ $(echo “${value} > 1024″|bc) -eq 1 ]
do
value=$(echo “scale=2;${value}/1024″ |bc)
let power=$power+1
done
#this part get b,kb,mb or gb according to number of divisions
case $power in
0) reg=b;;
1) reg=kb;;
2) reg=mb;;
3) reg=gb;;
esac
echo -n “${value} ${reg} ”
}
#to ensure that temp files not exist
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3
#if argument passed script will show statistic only for that pid, of not – we list all processes in /proc/ #and get statistic for all of them, all result we store in file /tmp/res
if [ $# -eq 0 ]
then
pids=`ls /proc | grep -e [0-9] | grep -v [A-Za-z] `
for i in $pids
do
get_process_mem $i >> /tmp/res
done
else
get_process_mem $1>> /tmp/res
fi
#This will sort result by memory usage
cat /tmp/res | sort -gr -k 5 > /tmp/res2
#this part will get uniq names from process list, and we will add all lines with same process list
#we will count nomber of processes with same name, so if more that 1 process where will be
# process(2) in output
for Name in `cat /tmp/res2 | awk ‘{print $6}’ | sort | uniq`
do
count=`cat /tmp/res2 | awk -v src=$Name ‘{if ($6==src) {print $6}}’|wc -l| awk ‘{print $1}’`
if [ $count = "1" ];
then
count=””
else
count=”(${count})”
fi
VmSizeKB=`cat /tmp/res2 | awk -v src=$Name ‘{if ($6==src) {print $1}}’ | paste -sd+ | bc`
VmRssKB=`cat /tmp/res2 | awk -v src=$Name ‘{if ($6==src) {print $3}}’ | paste -sd+ | bc`
total=`cat /tmp/res2 | awk ‘{print $5}’ | paste -sd+ | bc`
Sum=`echo “${VmRssKB}+${VmSizeKB}”|bc`
#all result stored in /tmp/res3 file
echo -e “$VmSizeKB + $VmRssKB = $Sum \t ${Name}${count}” >>/tmp/res3
done
#this make sort once more.
cat /tmp/res3 | sort -gr -k 5 | uniq > /tmp/res
#now we print result , first header
echo -e “Private \t + \t Shared \t = \t RAM used \t Program”
#after we read line by line of temp file
while read line
do
echo $line | while read a b c d e f
do
#we print all processes if Ram used if not 0
if [ $e != "0" ]; then
#here we use function that make conversion
echo -en “`convert $a` \t $b \t `convert $c` \t $d \t `convert $e` \t $f”
echo “”
fi
done
done < /tmp/res
#this part print footer, with counted Ram usage
echo "--------------------------------------------------------"
echo -e "\t\t\t\t\t\t `convert $total`"
echo "========================================================"
# we clean temporary file
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3

Memstat.sh Shell Script Output

[root@centos-cluster-node1 ~]# ./memstat.sh
Private + Shared = RAM used Program
36.26 mb + 268.00 kb = 36.52 mb python
20.49 mb + 238.00 kb = 20.72 mb iscsiuio
4.78 mb + 451.00 kb = 5.22 mb rgmanager(2)
3.62 mb + 283.00 kb = 3.90 mb NetworkManager
2.53 mb + 1.36 mb = 3.89 mb sshd(3)
2.30 mb + 355.00 kb = 2.64 mb multipathd
2.25 mb + 176.00 kb = 2.42 mb hald
1.69 mb + 298.00 kb = 1.98 mb iscsid(2)
1.45 mb + 432.00 kb = 1.87 mb dhclient(3)
1.62 mb + 161.00 kb = 1.77 mb cupsd
704.00 kb + 819.00 kb = 1.48 mb udevd(3)
856.00 kb + 554.00 kb = 1.37 mb bash(2)
1.00 mb + 314.00 kb = 1.31 mb qmgr
984.00 kb + 314.00 kb = 1.26 mb pickup
976.00 kb + 316.00 kb = 1.26 mb master
1.07 mb + 21.00 kb = 1.09 mb rsyslogd
804.00 kb + 240.00 kb = 1.01 mb modem-manager
904.00 kb + 40.00 kb = 944.00 kb pcscd
804.00 kb + 33.00 kb = 837.00 kb ricci
788.00 kb + 38.00 kb = 826.00 kb dbus-daemon
660.00 kb + 32.00 kb = 692.00 kb crond
536.00 kb + 69.00 kb = 605.00 kb rpc.statd
528.00 kb + 46.00 kb = 574.00 kb init
216.00 kb + 357.00 kb = 573.00 kb saslauthd(5)
544.00 kb + 21.00 kb = 565.00 kb wpa_supplicant
484.00 kb + 72.00 kb = 556.00 kb mingetty(6)
316.00 kb + 58.00 kb = 374.00 kb rpcbind
116.00 kb + 237.00 kb = 353.00 kb memstat.sh
328.00 kb + 13.00 kb = 341.00 kb auditd
248.00 kb + 84.00 kb = 332.00 kb hald-runner
312.00 kb + 8.00 kb = 320.00 kb oddjobd
216.00 kb + 81.00 kb = 297.00 kb hald-addon-stor
196.00 kb + 88.00 kb = 284.00 kb hald-addon-inpu
272.00 kb + 4.00 kb = 276.00 kb rpc.idmapd
176.00 kb + 52.00 kb = 228.00 kb hald-addon-acpi
——————————————————–
98.57 mb
========================================================
[root@centos-cluster-node1 ~]#

Linux Shell Script To Check File Exists Under A Path

This Shell script helps to check if the specified file exits under a given path. Script prompt you to provide file name and directory path.

Shell Script

#!/bin/bash
#We tell user that he need to enter filename
echo -n “Please enter file to check: ”
#We write filename to variable file
read file
#We tell user that he need to enter path to file
echo -n “Please enter path to check: ”
#We write path to variable path
read path
#we check if we have read permission on path
if [ -r $path ]
then
#if we have read permissions, we check if file exist
if [ -f ${path}/${file} ]
then
#if file exist, we tell user
echo “File ${path}/${file} exist”
#end of if loop
fi
#if we don’t have read permissions on path
else
#we warn user that we don’t have read permissions on path
echo “You don’t have access to folder $path”
fi

Script Output

test@server:~$ ls /tmp
haze-MsFUwz qtsingleapp-homeye-aeea-3e8
MozillaMailnews qtsingleapp-homeye-aeea-3e8-lockfile
pulse-2L9K88eMlGn7 ssh-UP2NOLoESr17
pulse-PKdhtXMmr18n unity_support_test.0
pulse-SfiK5uhmdkQW
test@server:~$ ./file_exist.sh
Please enter file to check: unity_support_test.0
Please enter path to check: /tmp
File /tmp/unity_support_test.0 exist

Linux Shell Script To Monitor Ftp Server Connection

#!/bin/bash
function usage {
echo “Usage: $0″
echo “–ftpserver ”
echo “–help – this help”
exit 1
}
if [ $# -eq 0 ]; then
usage
fi
while [ $# -gt 0 ]
do
case “$1″ in
–ftpserver) HOST=$2;TIMEOUT=$3;shift;;
–help) usage;;
*) break;;
esac
shift
done
if [ x$HOST = "x" ]; then
usage
fi
### Global Variables
ORIG_DIR=”/tmp/”
LOG_FILE=”/tmp/ftp.log”
EMAIL=”info@linoxide.com”
#next function will make all checks.
function check {
#few more variables
####VARIABLES
MAXTIMEOUT=300
ORIG_FILE=”check.file”
FTPTIMEOUT=’20’
USERNAME=”user”
FOLDER=”test”
PASSWORD=”password”
DATAFOLDER=”somefolder”
cd $ORIG_DIR
echo -n “Creating file … ”
dd if=/dev/urandom of=$ORIG_DIR/$ORIG_FILE bs=104857 count=150 > /dev/null 2>&1
echo “Done”
echo -n “Uploading file … ”
ftp -inv < $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
binary
passive
put $ORIG_FILE
quote size $ORIG_FILE
close
bye
EOF
echo ” DONE”
echo -n “Checking MD5 sum … ”
ftp -inv <> $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
passive
binary
quote size $ORIG_FILE
dir
quote XMD5 $ORIG_FILE
close
bye
EOF
MD5=`tail -2 $LOG_FILE | head -1 | awk ‘{print $2}’ | tr [:upper:] [:lower:]`
ftp -in </dev/null
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
delete $ORIG_FILE
close
bye
EOF
MD5_ORIG=`/usr/bin/md5sum $ORIG_FILE | awk ‘{print $1}’`
if [ x"${MD5}" != x"${MD5_ORIG}" ]; then
RESULT=” File corrupted.”
else
RESULT=” MD5 sum OK ”
fi
echo $RESULT
rm $ORIG_FILE
}
check

Linux Shell Script Output

./ftp.sh –ftpserver ftp.example.com 20
Uploading file … DONE
Checking MD5 sum … MD5 sum OK

Understanding the above script

#Since there will be few times of using same commands, we will create function with name usage
#This function just will show us help menu and will exit from script.
function usage {
echo “Usage: $0″
#we tell user which arguments script expect
echo “–ftpserver ”
echo “–help – this help”
#since not argument of bad argument – exit from script
exit 1
}
#We check if user pass parameters to script
if [ $# -eq 0 ]; then
#if no parameters just run function usage that show help menu
usage
fi
while [ $# -gt 0 ] #if user pass some parameters we check each
do
case “$1″ in #we take first argument passed by user
#if user pass –ftpserver as first parameter, so second will be ftp servername and third timeout. 
–ftpserver) HOST=$2;TIMEOUT=$3;shift;;
#if user pass –help as parameter we just output help menu
–help) usage;;
#if anything else passed – just exit from script
*) break;;
esac
shift
done #finish of while loop
#if user didn’t pass ftp servername – we show help menu
if [ x$HOST = "x" ]; then
usage
fi
## We will use some variables, so in next lines just putting their values.
### Global Variables
ORIG_DIR=”/tmp/”
LOG_FILE=”/tmp/ftp.log”
EMAIL=”info@linoxide.com”
#next function will make all checks.
function check {
#few more variables
####VARIABLES
MAXTIMEOUT=300
ORIG_FILE=”check.file”
FTPTIMEOUT=’20’
USERNAME=”user”
FOLDER=”test”
PASSWORD=”password”
DATAFOLDER=”somefolder”
#entering temp folder
cd $ORIG_DIR
#with dd we generate 15Mb file with name from $ORIG_FILE variable and with random content.
echo -n “Creating file … ”
dd if=/dev/urandom of=$ORIG_DIR/$ORIG_FILE bs=104857 count=150 > /dev/null 2>&1
echo “Done”
# This tell user that we starting to upload file
echo -n “Uploading file … ”
#we use ftp command to upload file, -i turns off interactive prompting 
ftp -inv < $LOG_FILE
#connect to host
open $HOST
#connect with user and password
user $USERNAME $PASSWORD
#cd to some test folder
cd $FOLDER/$DATAFOLDER
#we tell ftp server that we will use binary mode
binary
#we will use passive mode
passive
#this command copy local file to ftp server
put $ORIG_FILE
#getting size of remove file
quote size $ORIG_FILE
#closing connection
close
bye
#exit from ftp command
EOF
#telling user that upload done
echo ” DONE”
#starting md5 check
echo -n “Checking MD5 sum … ”
#same as above 
ftp -inv <> $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
passive
binary
quote size $ORIG_FILE
dir
#getting md5 of remote file
quote XMD5 $ORIG_FILE
close
bye
EOF
#getting md5sum value from log file, since it will be in upper case we convert it to lower case with tr
MD5=`tail -2 $LOG_FILE | head -1 | awk ‘{print $2}’ | tr [:upper:] [:lower:]`
#we need to connect to remote server once more to delete file
ftp -in </dev/null
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
#this command delete remote file
delete $ORIG_FILE
close
bye
EOF
#with md5sum command we get md5sum of local file
MD5_ORIG=`/usr/bin/md5sum $ORIG_FILE | awk ‘{print $1}’`
#this line compare md5sums, and variable RESULT will contain message according to check
if [ x"${MD5}" != x"${MD5_ORIG}" ]; then
RESULT=” File corrupted.”
else
RESULT=” MD5 sum OK ”
fi
#this will tell user if file was uploaded successfully or not.
echo $RESULT
#deleting local file
rm $ORIG_FILE
#end of function
}
#to run function (without arguments)
check

Wednesday, September 17, 2014

Linux Shell Script To Check Disk Usage Is Out Of Space

Script that check used space on all mounted devices and warn if the used space is more than the threshold. In this scenario we were using 25% of disk, that’s why threshold is so small


#!/bin/bash
threshold=”20″
i=2
result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`
for percent in $result; do
if ((percent > threshold))
then
partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`
echo “$partition at $(hostname -f) is ${percent}% full”
fi
let i=$i+1
done

Test Script Result

bobbin@linoxide:/$ df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 52G 4.7G 45G 10% /
tmpfs 1.9G 0 1.9G 0% /lib/init/rw
udev 1.9G 192K 1.9G 1% /dev
tmpfs 1.9G 2.6M 1.9G 1% /dev/shm
/dev/sda6 92G 22G 66G 25% /home
bobbin@linoxide:/$ ./df_script.sh
/dev/sda6 at linoxide.lviv.example.com is 25% full

Learn Above Shell Script

#This set threshold value
threshold=”20″
#Counter, will be used later, set to 2, since first line in df output is description.
i=2
#Getting list of percentage of all disks, df -kh show all disk usage, grep -v – without description line, awk ‘{ print $5 }’ – we need only 5th value from line and sed ‘s/%//g’ – to remove % from result.
result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`
#for every value in result we start loop.
for percent in $result; do
#compare, if current value bigger than threshold, if yes next lines.
if ((percent > threshold))
then
#taking name of partition, here we use counter. Df list of all partitions, head – take only $i lines from top, tail -1 take only last line, awk ‘{print $1}’ – take only first value in line.
partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`
#print to console – what partition and how much used in %.
echo “$partition at $(hostname -f) is ${percent}% full”
#end of if loop
fi
#counter increased by 1.
let i=$i+1
#end of for loop.
done

Shell Script Service Status Check And start If It’s Not Running

#!/bin/bash
if [ "$#" = 0 ]
then
echo “Usage $0 ”
exit 1
fi
service=$1
is_running=`ps aux | grep -v grep| grep -v “$0″ | grep $service| wc -l | awk ‘{print $1}’`
if [ $is_running != "0" ] ;
then
echo “Service $service is running”
else
echo
initd=`ls /etc/init.d/ | grep $service | wc -l | awk ‘{ print $1 }’`
if [ $initd = "1" ];
then
startup=`ls /etc/init.d/ | grep $service`
echo -n “Found startap script /etc/init.d/${startup}. Start it? Y/n ? ”
read answer
if [ $answer = "y" -o $answer = "Y" ];
then
echo “Starting service…”
/etc/init.d/${startup} start
fi
fi
fi

Results

bobbin@linoxide:/$ ./service.sh apparmor
Service apparmor is not running
Found startap script /etc/init.d/apparmor. Start it? Y/n ? Y
Starting service…
* Starting AppArmor profiles [OK]

Learn Above Script Line By Line

#check if service name passed to script as argument, if there no arguments (0) do next
if [ "$#" = 0 ]
then
“Usage $0 ” #write to terminal usage
echo
#since no arguments – we need to exit script and user re-run it
exit 1
fi
#get service name from first argument
service=$1
#this check ,if service running using ps command, after we remove our process from output, since script will also match, with wc we count number of matching lines.
is_running=`ps aux | grep -v grep| grep -v “$0″ | grep $service| wc -l | awk ‘{print $1}’`
#is number of lines are not 0 do next
if [ $is_running != "0" ] ;
then
#just put this line to terminal
echo “Service $service is running”
#if number of precesses is 0
else
#Service $service is not running” #just put this string to terminal
echo
#checking for files in /etc/init.d
(directory with start-up scripts) with name similar to service
initd=`ls /etc/init.d/ | grep $service | wc -l | awk ‘{ print $1 }’`
#if there is script with similar name
if [ $initd = "1" ];
then
#this line get name of startup script (ls – lists files in directory
startup=`ls /etc/init.d/ | grep $service`
echo -n “Found startap script /etc/init.d/${startup}. Start it? Y/n ? ”
#waiting for user answer
read answer
#if answer Y or y
if [ $answer = "y" -o $answer = "Y" ];
then
echo “Starting service…”
#running startup script
/etc/init.d/${startup} start
fi
#exit of if loop
fi
#exit of if loop
fi

Shell Script Find All Zip / Rar Files Then Unzip / Unrar

Shell Script

#!/bin/bash
list=`find /home/linoxide/ -type f -name “*.rar”`
for line in $list; do
DEST=${line%/*}
unrar x $line $DEST
done

Shell Script Output

Server1:~$ ./rar.sh
UNRAR 3.93 freeware Copyright (c) 1993-2010 Alexander Roshal
Extracting from /home/linoxide/Dropbox/Yevhen/test.rar
Extracting /home/linoxide/Dropbox/Yevhen/wget.sh OK
All OK
UNRAR 3.93 freeware Copyright (c) 1993-2010 Alexander Roshal
Extracting from /home/linoxide/Pictures/test.rar
Extracting /home/linoxide/Pictures/wget.sh OK
All OK

Learning above shell script

#this line must be in every bash script, just ensure that you use correct path
list=`find /home/linoxide/ -type f -name “*.rar”` # get list of file and write this list to variable with name list, find command used to find all files (-type f) where name match *.rar (-name key)
for line in $list; do #this line take every line from list to line variable
DEST=${line%/*} # remove from line filename, so just destination will be in DEST variable.
unrar x $line $DEST # unrar file from line variable to DEST dir
done # finish of for loop.

Linux Shell Script To Create New Linux User's

#!/bin/bash
while [ x$username = "x" ]; do
read -p “Please enter the username you wish to create : ” username
if id -u $username >/dev/null 2>&1; then
echo “User already exists”
username=””
fi
done
while [ x$group = "x" ]; do
read -p “Please enter the primary group. If group not exist, it will be created : ” group
if id -g $group >/dev/null 2>&1; then
echo “Group exist”
else
groupadd $group
fi
done
read -p “Please enter bash [/bin/bash] : ” bash
if [ x"$bash" = "x" ]; then
bash=”/bin/bash”
fi
read -p “Please enter homedir [/home/$username] : ” homedir
if [ x"$homedir" = "x" ]; then
homedir=”/home/$username”
fi
read -p “Please confirm [y/n]” confirm
if [ "$confirm" = "y" ]; then
useradd -g $group -s $bash -d $homedir -m $username
fi

Sample Result

sudo ./linux_user.sh
Please enter the username you wish to create : test
Please enter the primary group. If group not exist, it will be created : test
Please enter bash [/bin/bash] :
Please enter homedir [/home/test] :
Please confirm [y/n]y
22:12:58 [test@Desktop] :~ id test
uid=1003(test) gid=1003(test) groups=1003(test)

Learn Above Script Line by Line

#Write to console ask to enter group and save input to group variable
read -p “Please enter the primary group. If group not exist, it will be created : ” group
#check if group already exist
if id -g $group >/dev/null 2>&1; then
#just warn that group already exist
echo “Group exist”
else
#if group not exist – create one more
groupadd $group
fi
#end of while loop
done
#ask to enter preferred bash
read -p “Please enter bash [/bin/bash] : ” bash
#check if no input
if [ x"$bash" = "x" ]; then
#if no input, use default bash
bash=”/bin/bash”
fi
#ask to enter preferred homedir
read -p “Please enter homedir [/home/$username] : ” homedir
#check if no input
if [ x"$homedir" = "x" ]; then
#if no input , use default homedir
homedir=”/home/$username”
fi
#ask to confirm all inputs
read -p “Please confirm [y/n]” confirm
#if input y
if [ "$confirm" = "y" ]; then
#command to add user with all entered info
useradd -g $group -s $bash -d $homedir -m $username
fi

Shell Script : Find Files Older X Days And Ask User To Delete

#!/bin/bash
#we check for parameters
#Directory is requirede parameter, to avoid deleting from any other folders
if [ $# -eq 0 ]; then
echo “`basename $0` ”
echo “Script will delete file folders older than inside ”
echo “If no days inputed, will use 7 days as default”
fi
#We save variables
DIR=$1
#check if user input days
if [ x"$2" = "x" ]; then
#if user didn’t input days, we will use default value
DAYS=”7″
else
DAYS=”$2″
fi
#this will create list of folders older that X days. We use command find to find them, and set maxdepth
# to 1, we don’t need recursively find all folders
dirlist=`find $DIR -maxdepth 1 -type d -mtime +$DAYS`
#now we will process each folder
for dir in ${dirlist}
do
#this will check if user have read and write permissions
if [ ! -r ${dir} -o ! -w ${dir} ]; then
#if no permissions just warn, without try to delete
echo “Access denied to folder ${dir}”
else
#if we have permissions – we ask user if really delete
read -p “Delete folder ${dir} and all subfolders? [y/n]” confirm
#check if user confirm deleting
if [ "$confirm" = "y" ]; then
#if user confirm – rm command to delete
rm -rf ${dir}
fi
fi
done
#same as with directories we do with files
#we get list
filelist=`find $DIR -maxdepth 1 -type f -mtime +$DAYS`
#proceed with every file
for file in ${filelist}
do
#we check permissions
if [ ! -r "${file}" -o ! -w "${file}" ]; then
echo “Access denied to file ${file}”
else
#if we have permissions we ask about confirmation
read -p “Delete file ${file}? [y/n]” confirm
if [ "$confirm" = "y" ]; then
#if confirmed – we delete file.
rm -rf ${file}
fi
fi
done

Output

Server1:~/$ ./delete_xdays.sh /home/yevhen/Downloads 7
Delete folder /home/yevhen/Downloads/CENTOS and all subfolders? [y/n]n
Delete folder /home/yevhen/Downloads/TFTP and all subfolders? [y/n]y
Delete file /home/yevhen/Downloads/Oracle_Solaris_Studio.certificate.pem? [y/n]^C
Server1:~/$ ./delete_xdays.sh /home/yevhen/Downloads 7
Delete folder /home/yevhen/Downloads/CENTOS and all subfolders? [y/n]n
Delete file /home/yevhen/Downloads/Oracle_Solaris_Studio.certificate.pem? [y/n]n
Delete file /home/yevhen/Downloads/sol-11_1-text-x86.iso? [y/n]^C

Linux Shell Script To Collect Linux System Information

#!/bin/bash
#clear console
clear
#just echo welcome messages
echo “This is information provided by $0 . Program starts now.”
echo “Hello, $USER”
echo
#print today’s date
echo “Today’s date is `date`, this is week `date +”%V”`.”
echo
#list of currently loged user via w command.
echo “These users are currently connected:”
w | cut -d ” ” -f 1 – | grep -v USER | sort -u
echo
#info about system with command uname and keys -m and -s
echo “This is `uname -s` running on a `uname -m` processor.”
echo
#info about uptime, using uptime command
echo “This is the uptime information:”
uptime
echo
#info about free memory via free command
echo “Free memory:”
free
echo
#info about disk usage
echo “Disk usage:”
df -kh
echo

Output

Below is the script output.
This is information provided by ./system_info.sh . Program starts now.
Hello, yevhen
Today’s date is Sunday, March 3 2013 22:31:43 +0200, this is week 09.
These users are currently connected:
yevhen
This is Linux running on a i686 processor.
This is the uptime information:
22:31:43 up 1:24, 2 users, load average: 0.45, 0.18, 0.07
Free memory:
total used free shared buffers cached
Mem: 3374792 1589476 1785316 0 96720 725652
-/+ buffers/cache: 767104 2607688
Swap: 4192924 0 4192924
Disk usage:
Filesystem Size Used Avail Use% mounted
/dev/sda1 42G 5,5G 35G 14% /
tmpfs 1,7G 0 1,7G 0% /lib/init/rw
udev 1,7G 168K 1,7G 1% /dev
tmpfs 1,7G 160K 1,7G 1% /dev/shm
/dev/sda6 184G 26G 149G 15% /home

Shell Script To Find Directories Which Consume Highest Space

#/bin/bash
#check if user input argument
if [ $# -eq 0 ]; then
#if no argument print next messge and exit from script
echo “Usage: $0 ”
exit 1
fi
# Save first arguments to variables
CheckedDir=”$1″
#
HeadValue=$2
#set value for variable count value 1
count=1
#just print empty line
echo “”
#Print next message:
echo “Here is the ${HeadValue} biggest directories located in ${CheckedDir}:”
echo “”
#Getting list of directories and space they use.
du -a –max-depth=1 –one-file-system ${CheckedDir}/ |
#next we sort result
sort -rn |
sed “1d” |
# next we get only first X directories
head -“${HeadValue}” |
#next print result to user
while read size dirrr ; do
#counting size in Mb
size=”$(( size / 1024 ))”
#show output for user
echo “N°${count} : ${dirrr} is ${size} Mb”
((count++))
done
echo “”

Script Output

./top5dir.sh /home/yevhen/
Here is the biggest directories located in /home/yevhen/:
N°1 : /home/yevhen//Fly is 14043 Mb
N°2 : /home/yevhen//VirtualBox VMs is 5837 Mb
N°3 : /home/yevhen//Downloads is 2224 Mb
N°4 : /home/yevhen//.icedove is 963 Mb
N°5 : /home/yevhen//Downloads is 645 Mb
N°6 : /home/yevhen//.wine is 602 Mb
N°7 : /home/yevhen//.cache is 324 Mb
N°8 : /home/yevhen//Dropbox is 324 Mb
N°9 : /home/yevhen//.config is 263 Mb
N°10 : /home/yevhen//.local is 153 Mb

Linux Shell Script To Delete Duplicate Files

#!/bin/bash
#file, where we will store full list of files.
ListOfFiles=/tmp/listoffiles.txt
#we ask user to enter directory where search for duplicated files
echo -n “Please enter directory where to search for duplicated files: ”
#we read user input
while read dir
do
#we check if user input is not empty
test -z “$dir” && {
#if user input empty we ask once more to enter directory
echo -n “Please enter directory: ”
continue
}
#if directory entered, exit from while loop
break
done
#getting list of files inside entered directory
find $dir -type f -print > $ListOfFiles
#writing list of files to variable
FileList=`cat $ListOfFiles`
#we get number of files
count=`wc -l $ListOfFiles| awk ‘{print $1}’`
#counter
i=1
#we get files one by one
for file in $FileList
do
#just make this variable empty for every loop
samefiles=””
#we need to get all non-proceeded files
let tailvalue=$count-$i
#we get only filename, without path
filename=$(basename $file)
#getting list of un-proceeded files, and we check if there is file with same filename
samefiles=`tail -${tailvalue} $ListOfFiles | grep $filename`
#starting loop for all same files
for samefile in $samefiles
do
#we get md5sum of filename with same name
msf=`md5sum $samefile | awk ‘{print $1}’`
#we get md5sum of original file
ms=`md5sum $file | awk ‘{print $1}’`
#we compare md5sums
if [ "$msf" = "$ms" ]; then
#if md5sums equal, we tell user about duplicated files
echo “File $file duplicated to $samefile”
#end of if loop
fi
#end of while loop
done
#increase counter by 1
let i=$i+1
done

Script Output

./finddup.sh
Please enter directory where to search for duplicated files: /tmp
File /tmp/1/user.list duplicated to /tmp/user.list

Shell Script To Run Commands In Users Home Directory

#Next line tell with shell to use
#!/bin/bash
#we store in variable path to file
UserListFile=/tmp/user.list
#We create function with name Usage
Usage () {
#printing help
echo “$0 – execute command in all HOME directories
usage: $0 [-a] command [arg ...]
-a: process HOME directories of all users
The given command will be executed with the arguments specified in the
home directory of users.
”
#exit from program
exit 1
#end of function
}
#we check if there less than 1 parameter
if [ $# -lt 1 ]
then
#if less – print help
Usage
#in other case
else
#we check passed parameters, starting from first
case “$1″ in
#if parameter -a then we write to All variable value true
-a) All=true
#removing parameter
shift ;;
#if parameter starting with -, and not -a, we print help
-*) Usage ;;
#exit from case
esac
#exit from fi loop
fi
#we check if user pass -a as parameter
if [ "$All" = true ]
#if -a passed
then
#we get list of all users
cat /etc/passwd | cut -d':’ -f1 | sed -e ‘s/:/ /g’ > $UserListFile
# in other case
else
#print message
echo “Please enter users, type [ to exit”
#cleaning file with userlist
echo -n > $UserListFile
#starting while loop to read users
while read UserName Rest
do
#if user print [ to exit loop
if [ $UserName = “[" ]; then
#exit from loop
break
#if user print other data
else
#we check if user put any symbol
test -z “$UserName” && continue
#we check if user exist
grep “^$UserName” /etc/passwd > /dev/null || {
#if not exist print message to user
echo “Can’t find user ${UserName}; ignored.”
#we continue loop
continue
}
#if user exist – we store it to userfile
echo $UserName >> $UserListFile
#exit of if loop
fi
#exit from reading user input
done
#exit from if loop
fi
#just print empty line
echo “”
#stop all other parameters as command we need to run
Command=”$@”
#getting list of users
UserList=`cat $UserListFile`
#getting user one by one from list
for user in ${UserList}
do
#one more empty line
echo “”
#getting user home directory
HomeDir=`grep “^$user” /etc/passwd | cut -d”:” -f6`
# we check if we have read and execute permissions to cd and run command
[ -r "$HomeDir" -a -x "$HomeDir" ] || {
# if not just warn user
echo “Read or execute of folder $HomeDir denied”
continue
#exit of check
}
#we telling user about what command we run and inside what directory
echo “Runnig $Command inside $HomeDir”
#entering home
cd $HomeDir
#running command
$Command
done
#cleaning, removing temp files
rm -rf $UserListFile

Sccript Output

./exec_home.sh ls
Please enter users, type exit to exit
sshd
yevhen
exit
Runnig ls inside /var/run/sshd
Runnig ls inside /home/yevhen
Desktop Downloads examples.desktop Pictures Templates
Documents Dropbox Music Public Videos

Linux Shell Script To Find Kernel Version From Multiple Servers

#!/bin/bash
#we user variable serverlist to keep there path to file with server names
serverlist=’server_list.txt’
#we write in variable all server list
servers=`cat $serverlist`
#we use variable result to keep there path to file with result
result=’result.txt’
#this print header to file with resilt using \t\t to add 2 tab symbols
echo -e “Servername \t\t kernel version”> $result
#this get each line of serverlist one by one and write to server variable
for server in $servers
do
#this login to server by ssh and get uname -r
kernel=`ssh root@${server} “uname -r”`
#this write server name and kernel version separated by 2 tab to result file
echo -e “$server \t\t $kernel” >> $result
#end of for loop.
done
server_list.txt file
# cat server_list.txt
dev
web1
svn

Shell Script Output

./kernel_version.sh
centos_node1@bobbin:~/Documents/Work/Bobbin$ cat result.txt
Servername kernel version
dev 3.3.8-gentoo
web1 3.2.12-gentoo
svn 3.2.12-gentoo