Sunday, 25 November 2012

Delete files older than x days in batch script in windows

Delete files older than x days in .bat script in windows :


If anyone needs to delete files older than x days in .bat script in windows, you could use the following command.

forfiles /p "d:\test" /s /m *.* /c "cmd /c Del @path" /d –15 

Check below things to run above command successfully :
1) Make sure the “d:\test” is replaced with the correct path in above command.
2) Run above command and if forfiles command not found error occurs then we need to add the “C:\Windows\System32\” to the PATH environment variable because the forfiles command is resides there.


Below is the documentation  and example of forfiles:





If you find this post useful. Please comment Thanks :)

Wednesday, 21 November 2012

Linux File System Structure

What is file system?
-File System is nothing but structured set of files and directories which operating system uses as data structure.


     When user installs any software on Linux or Unix system,configuration files,manual pages (help pages),binaries, libraries goes under there respective directories like etc,bin,sbin etc. So user of the system should aware of where he should look for files .


For example if you have install FTP Server on your Linux machine then you should aware of 


-/etc/vsftpd.conf  : ftp configuration file.

-/var/ftp :contains users uploaded files.
-/etc/init.d/vsftpd : this is the ftp service.
-/var/log/vsftpd.log : log file for debugging issues and errors.

If know this structure you need not have to search again and again on internet for file locations.


Below I have explained Linux/Unix file system structure:




Linux File System
Linux File System


1) /  : Root Directory


  • Linux file system starts with /, the root directory which is parent directory for all other directories in Linux File System.
  • In days past it was also the home directory of 'root' user but now it has been given his own directory named '/root'.


2) /bin :User Binaries


  • contains essential system programs that must be available
  • these are needed by all the users of the system
  • these binary commands are like cp, mv, rm, cat, ls etc.

3) /sbin :System Binaries

  • contains binaries which  used for system maintenance and/or administrative tasks.
  • these binaries are essential for booting, restoring, recovering, and/or repairing the system
  • These binaries are used by system administrator for maintenance 
  • examples commands : shutdown, fastboot, fasthalt, fdisk, fsck, reboot, ifconfig, route etc.

4) /boot : Boot Related Files.


  • contains everything required for the boot process.
  • Example GRUB boot−loader,boot.b(boot sector) ,config−kernel−version etc.

5) /dev  :All Hardware Device files


  • contains all device files.
  • very interesting directory that highlights one important aspect of the Linux filesystem − everything is a file or a directory.Look through this directory and you should hopefully see
  • /dev/cdrom : CD -ROM
    /dev/fd0      : floppy drive
    /dev/hda1   : can be hard disk partition 1 like C: on windows

6) /etc: Configuration Files

  • contains all configuration files of installed programs.
  • this directory contains init.d and xinit.d directory which contains some services/daemon processes which user can  start,stop,restar. Contains services like  ftpd ,mysqld,network,sshd.
  • Contains configuration files like : crontab ,ftpuser,host.conf,yum.conf

7) /lib : Library To Run Executable Binaries

  • contains library to run executable binaries in bin,sbin etc.
  • similar to windows DLL files. These file names contains extension *.so.
  • e.g. libcap.so.22, libc.so.6

8) /media : Removable Media Files

  • contains files related to the removable media like usb drive,cd-rom,floppy.
  • temporary mount directory for removable devices.

9) /mnt: User Binaries

  • Temporarily  directory where user mount their filesystems or devices.
  • Mounting is the process by which you make a filesystem available to the system.
  • e.g System user can mount pen-drive under /mnt directory. so /mnt will have directory containing pen drive directory like /mnt/my_kingston

10) /opt : Optional Packages

  • reserved for all the software and add−on packages that are not part of the default installation.

11) /usr: Unix System Resource

  • one of the most important directories in the system user should aware of.
  • contains all the user binaries, their documentation, libraries, header files, etc
  • If you are looking for binaries,libraries,doc,config files etc  then also check  /usr directory.It can be found under below directories:
    • /usr/bin : Contains binaries of program's like vi,gcc,mozilla etc.
    • /usr/etc : Configuration files.
    • /usr/lib  : Contains program libraries.
    • /usr/sbin : binaries for administrative use.
    • /usr/man : help man pages.
  • /usr/share : Contains shareable  files.
  • /usr/src : Holds kernal sources,header−files and documentation.
  • /usr/local : Contains files of applications which are used locally.


12) /var : Variable Data

  • Contains variable data like system logging files, mail and printer spool directories, temporary files etc.
  • e.g /var/log (all logs),/var/mail,/var/tmp,/var/spool,/var/lib (dynamic data libraries/files)


13) /tmp : Temporary Files

  • Contains temporarily required files.
  • Many programs use this to create temporary storage of data.

14) /proc : Process Information Pseudo−file System

  • Doesn't contain 'real' files but runtime system information.
  • e.g. system memory, devices mounted, hardware configuration, etc

15) /srv: Service Data

  • Contains service related data.
  • e.g. /srv/ftp contains ftp service related data.

16) /root: Home Directory Of Root User

  • home directory of the System Administrator, 'root'.

17) /lost+found: Recovered files

  • Holds recovered files when system crash happens.

Thursday, 25 October 2012

Command to search a string in a file in linux



Below is simple command which searches string all the  files in specified URL

find pathToDir -type f -exec grep -l yourstring {} \;


Where

pathToDir :path of directory where you want search string in files.
yourstring :String you want to search


Also check below links :


Wednesday, 24 October 2012

How to kill tomcat process in linux

Find and kill tomcat process in linux with below steps:

Step 1 : Find the process id of the tomcat process:

Use  following commands to find tomcat process:


ps -ef  | grep tomcat  :here try with capital Tomcat as well

if not found with above command use below command :

ps -ef  | grep java

if still not found with above commands use below command which lists down all the processes and  go through each processes one by one to find tomcat process:

ps -ef

In above commands
-e :select all process
-f  : list down all the processes

Note *:Check command man ps for more options.

Step 2: From above steps we will able to find out tomcat process pid. use below command to
 kill the process

kill -9 pid

-9 is special Kill signal, which will kill the process. There is very less chance of failing single with option -9.

Note *:Check command man kill for more options.

Step 3 :execute step 1 to check whether tomcat process is killed or not.



How to kill Process In Linux


Some time we fire stop command on particular process using “service” command but it doesn’t stop. In such case we need to stop the process using ps command.

Follow below steps to kill process:

Step 1 :  Before killing the process we need identify which process to kill. Use below command to find particular process:

ps -ef  | grep java


Note: Here all wunning java processes will be listed. change java to other process name as per needed.

-e : select all  running processes
-f :  list down all the process

Description: ps -ef will get all the running process list which is given to grep commnad which will only show java processes.

Step 2: Identify the process id (pid ) from above command result and use below command to kill the process:

kill pid

if above command does not work.Then use below command

kill -9 pid

-9 is special Kill signal, which will kill the process. There is very less chance of failing single with option -9.

step 3 : verify process is killed or not using step 1

Note*: use command man ps and man kill  for more options for ps and kill command.


E.g.  click to see example for find and kill tomcat process



Also check below links :


Tuesday, 16 October 2012

Send Mail In Linux With Attachment and Without Attachment



1) Send Mail Command  In Linux   


Listed below different commands to send mail.

1) Send mail by passing mail body from text file.
$ mailx -s "Email Test subject" you@me.com test@me.com  < message.txt

2) Send mail by passing mail body from echo.
$ echo "this is test body" |  mail -s  "Email Test subject"  you@me.com test@me.com

3) Send mail by passing mail body from shell script variable.
$ echo "$body" | mail -s "Email Test subject" you@me.com test@me.com

If above command is not working check below packages and services functioning:

           Check mail service is installed and running with below commands.
           1) Check  sendmail installed or not:

                   a)  For Fedora,Redhat,Cestos or rpm packages : rpm -q sendmail
                   b)  For Ubuntu,Debian or deb packages           : dpkg -s apache-perl

                    If package sendmail is not installed use following command to install sendmail.     

                   a)  For Fedora,Redhat,Cestos   : yum  install sendmail
                   b)  For Ubuntu,Debian           : apt -get sendmail

              2) Check if sendmail service is running :

                   service sendmail status

                   If service is not running start with following command

                   service sendmail start

              3) If mail service is running and mail is not sent then check the log file at /var/log/maillog.

2) Send Mail Command With Attachment In Linux:


       To send mail with attachment perform following steps:

       1)Install sendmail  with following command:

               a)  For Fedora,Redhat,Cestos   : yum  install sendmail

               b)  For Ubuntu,Debian           :  apt -get sendmail


       2) Install mutt with following command:

               a) For Fedora,Redhat,Cestos: yum  install mutt

               b)  For Ubuntu,Debian           : apt -get mutt


        3) Start sendmail service with command service sendmail start

        4) use following command to send mail with attachment


mutt -s "subject" -a "/usr/MyAttachment.txt" me@you.com < tempBody.txt

3) Send Mail In Crontab

         If user want to execute  command using crontab and send a status of command in a mail use following :

e.g  Execute backup.sh every day at 2 am in the morning and send output of script in a mail.

0  2  * *  *  usr/backup.sh | mail -s  "Email Test subject"  you@me.com test@me.com
 


Also check below links :






Some Useful commands In Linux/Unix



There are few commands which are frequently used and we stuck for those command.
I have listed some of the basic commands used frequently.


1) Search string or word in a file in a specified directory:


find /path/to/dir -type f -exec grep -l yourstring {} \; 


2)

Find files by name

a) locate -i filename :This command searches in database.here -i is ignorecase.(This is Fast)
b) find filename        : It searches the file system.(This is slow)

3) Find process and kill particular process.

a) ps -ef | grep tomcat :
Shows running tomcat process details.
command explanation : -e stands for list all process and f formats output view.Output of ps -ef
is given to grep command which search given string in the given output.

b) kill pid : This command will kill the process. If process still not stopped then use kill -9 pid

command.

4) Check the available space on disk:


  df -h : Outputs total space utilized and free space available. -h stands for human readable 
format e.g. mb,gb etc.

5) Check the size of file or directory in linux


  du -ah path : Display the size of directory. “-a” stands for show file along with size and “h”  

for human readable format like mb,gc etc.

6) How to send mail in Linux


mailx -s "Subject line" me@you.com <message.txt : Before firing this command check  

sendmail  is running with command service sendmail status.

7) Installing software in linux is based on different linux flavours


a)RedHat,Fedora,CentOs : yum install software name  e.g yum install sendmail. yum 

command is for rpm packages
b)Ubuntu : apt -get software name  e.g apt -get sendmail. apt command is used for debian 

packages.

8) How start service in linux


  service start servicename :Starts the tomcat service e.g service start tomcat
 service status servicename : Check status of service.
 service stop servicename : Stops the service.

9) Change permission of file or directory in linux:


Below is good link for understanding file permission. Tead it once so concept will be clear 

forever.
http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html

10) Cut paste file from one location to other (move file from one dir to other)


     a) mv file.txt directorypath/  : Move file.txt  to given directory path
        b) mv file1.txt directorypath/file1.txt  : Move file.txt to directory path and rename it to

file1.txt

11) Renaming file in linux : same mv command is used for renaming file.


mv file.txt file1.txt : As path file.txt and file1.txt is in the same directory mv utility will 

understand it’s a renaming file not moving.

12) Deleting file and folder in linux:

    rm -f filename : Delete file . -f option tell that don’t prompt message like “are you sure you 
want to delete this file?”.
   rm -rf directorypath : This command delete directory. -r will delete subdirectories as well 
-f is explain above.
  rm -f ./*.log : This command will delete all the files in current directory with extension .log.


Also check below links :


Wednesday, 26 September 2012

Mysql Database Backup In Linux

Taking backup of mysql databases is the most common requirement of the applications.Same requirement came to my project.which needs to take backup of mysql database and send status via mail.This script only be used for small size databases. It is useless for big size databases.

Requirement to run below script :


1)Check sendmail is installed and running using command : service sendmail status.
2)mutt is installed or not :This is used to send mail with attachment.Note if you want to send mail with attachment comment mail command and uncomment mutt command
in the below script .

To use the following script replace text with yellow background with following :

1) DATBASE_USERNAME    :Give your database user name
2) USER_PASSWORD           :Give your database user password
3) DATABASE_SERVER_IP  :IP of server where database is located (most of the cases it is localhost)
4) DATABASE_NAME           :Give your database name
5) COMMA_SEPARATED_EMAILID : Give the list of comma separated email id.


Change Backup file paths and log paths as per your requirement.

   Script:

#File Name: DbBackupScript
#
#Created for taking backup of the  application database
#Script will run mysql dump command and will save the content under
#Backup location : /var/mybackups/
#If any error occurse firing mysqldump command, error log will be
#generated under #/var/mybackups/mysqlDumperr.log
#
#After every backup, mail will be send to user defined in EMAILTO variable
#
#Created by user
#
#appended extension to backup file as .e.g backup_wed.sql,backup_mon.sql
TODAY=$(date +"%a")

#these variables are used for holding the backup status string which used while
#sending mail
backupStatus=''

#following command will take backup of database
mysqldump -uDATBASE_USERNAME -pUSER_PASSWORD -h DATABASE_SERVER_IP   --log-error=/var/mybackups/backuplogs/mysqlDumperr.log   --result-file=/var/mybackups/backup_$TODAY.sql   DATABASE_NAME

#check wehter mysqldump command is successfull or not
if [ $? -eq 0 ]; then
# echo "backup mysqldump Command executed successfully.Exit Status $? "
backupStatus="backup mysqldump Command executed successfully"
else
# echo "backup mysqldump Command executon failed  with exit status $?.Please check logs."
backupStatus="backup mysqldump Command executon failed check with exit status $?.Please check logs at /var/mybackups/backuplogs/mysqlDumperr.log."
fi

#Check the size of backup directory
BACKUP_DIR_SIZE=`du -h  /var/mybackups/`

#start mail script

tempBody=tempBody.txt

SUBJECT="$backupStatus  on date :$(date)"
EMAILTO="COMMA_SEPARATED_EMAILID"
MAILBODY="Database backup satus :$backupStatus \n\n All backup directory size is :$BACKUP_DIR_SIZE"
#ATTACHMENT='/var/mybackups/backuplogs/mysqlDumperr.log'

#write a mail body to tempBody.txt
echo -e "$MAILBODY">>$tempBody

#give a tempBody.txt as a mail body to mutt command
#use below this if want to send mail with attachment and mutt is installed
#mutt -s "$SUBJECT" -a "$ATTACHMENT" $EMAILTO < $tempBody

#send status mail
mailx -s "$SUBJECT"  $EMAILTO < $tempBody


#clean temp files
/bin/rm $tempBody

#end of mail script


*Note: Also change the script as per your requirement



Also check below links :