Pages

Wednesday, August 31, 2011

Find useful useful unix command

1. To find all the .conf file in / and copy it to /backup

# find . -name '*.conf' -print -exec cp '{}' ~/backup \;

No need to explain the options above except -print and exec.

-print - It is allways true and has a side effect of printing.

{} - This will replace the name of the file found.

\; - Means end of the line

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

2. To find all the .txt files with odd characters( Contain upper and lowe case and numbers)

# find . -name '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup

-print tell find to use null character insted of white space.

----------------------------------------------------------------------------------------------------------
3. To find all the files across the symbolic links

# find . -follow -name '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup

-follow - This option help to find out the orgination of the symbolic links.

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

4. To find out all the.txt files case insensitively

# find . -follow -iname '*.txt' -print0 | xargs -i -0 mv '{}' ~/backup


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

5. To find out file modified more than +90 days

# find . -name '*.txt' -mtime +90 -print


-mtime - Takes argument to specify the time frame.

--------------------------------------------------------------------------------------------------------------
6. To print out the files modified more than 7 days and less than 14 days


# find /home -mtime +7 -a -mtime -14 -print

---------------------------------------------------------------------------------------------------------------
7. To find the files with java extension

# find . -name '*java*' -print

----------------------------------------------------------------------------------------------------------------
8. To find the java files in all the directories in /

# find / -type d -name '*java*' -print

----------------------------------------------------------------------------------------------------------------
9. To find out all the block device files in /dev

# find /dev -type b -name '*' -print

----------------------------------------------------------------------------------------------------------------
10. To find out charecter special file in /dev

# find /dev -type c -name '*' -print

----------------------------------------------------------------------------------------------------------------
11. To find out all the directories in /

# find / -tyde d -name 'dev' -print

----------------------------------------------------------------------------------------------------------------
12. To find out the all the named pipes in dev directory

# find /dev -type p -name '*' -print

----------------------------------------------------------------------------------------------------------------
13. to find out all the symbolc link in /

# find / -type l -name '*' -print

----------------------------------------------------------------------------------------------------------------
14. To find all the files above 3MB

# find / +3000K -print

----------------------------------------------------------------------------------------------------------------
15. Finding Files By content

# grep -i vasanth /etc/passwd
(This can be used only when we have the vicinity of the file.)

----------------------------------------------------------------------------------------------------------------
16. To find the word vasanth from files inside /etc/

# find /etc -name '*' -exec grep -Hi vasanth '{}' \;

( Use of exec command:- When predicates are true upto that point it will execute the grep command for all the files.

'{}' is where the filename is put when executing the command
The \; indicates the end of the command
-H print if grep command find soomething

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



----------------------------------------------------------------------------------------------------------------
17. To find


Sunday, August 28, 2011

SIMPLE SHELL SCRIPT TO BACKUP WHOLE MYSQL DATABASES AND KEEP ONLY TWO LATEST COPIES

#! /bin/bash
# Written by Vasanth.T.M, L2-Systems Engineer(*nix), Perfomix, Inc.


CKUPDATE=$(date +%d-%m-%Y)
BACKUPDIR=/mysqlbackup
DATABASES=$(mysql -u root -h localhost -pmysql -Bse 'show databases')


delete_old ()
{
echo Deleting old backup of backup of "$name"
name="$1"
find "$BACKUPDIR" -name "$name-*.sql.bz2" | sort | head -n -2 | xargs --no-run-if-empty rm -f
}

back ()
{
for GH in $DATABASES; do
echo "Creating mysql backup of $GH"
mysqldump -u root -pmysql $GH | bzip2 --compress --stdout > $BACKUPDIR/$GH-$CKUPDATE.sql.bz2
name=`basename $GH`
delete_old "$name"
done
}
back