Sunday, February 1, 2015

Quick and dirty bash shell script to find issues on your linux server

I usually would place this file in my home directory and name it 'find_issues.sh'

cd ~
nano find_issues.sh
chmod +x find_issues.sh

content of find_issues.sh:


#!/bin/bash

read -sn 1 -p "Press any key to check keyword 'error'";echo
tail -n 1000 /var/log/messages | grep error

read -sn 1 -p "Press any key to check keyword 'fail'";echo
tail -n 1000 /var/log/messages | grep fail

read -sn 1 -p "Press any key to check keyword 'panic'";echo
tail -n 1000 /var/log/messages | grep panic

read -sn 1 -p "Press any key to check keyword 'fault'";echo
tail -n 1000 /var/log/messages | grep fault

read -sn 1 -p "Press any key to check keyword 'timeout'";echo
tail -n 1000 /var/log/messages | grep timeout

Check if directory exists using bash or linux shell script

if [ -d directory_path ]
then
   echo "dir exists"
else
   echo "dir does not exists"
fi

Check if file exists using bash or linux shell script


if [ -f file_path_name.ext ]
then
   echo "file exists"
else
   echo "file does not exists"
fi

Disable email notification from cronjobs or crontab

First technique to disable ALL email notifications:

add this line on the very top of your crontab:

MAILTO=""


Second technique is to disable for individual task:

somecommand >/dev/null 2>&1



Remove file(s) in directory recursively older than certain number of days using find and xargs

find <dir_path> -type f -mtime +<days> | xargs rm

for example to remove files older than 3 days:

find /data/log -type f -mtime +3 | xargs rm

for older than 5 minutes:
find /data/log -type f -mmin +5 | xargs rm

find files changed within last # days:
find /dir -type f -mtime -{num_day} | xargs rm


Even though all the examples above uses 'rm' to remove files, you can substitute 'rm' with other commands such as 'ls' to list all files recursively.

To make sure all linux mounts from /etc/fstab is mounted or remount it

mount -a


this command surprisingly does not double-up the mounts. it is smart enough.

Find large files size in linux file system from command line recursively

find -maxdepth 6 -type f -size +1G > ~/largefiles.txt