Thursday, July 16, 2009

Wonderful Find Command

"find" is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user.

Here are a few combinations which one can use to get useful results using find command.

* Find all HTML files starting with letter 'a' in your current directory (Case sensitive):

Code: find . -name a\*.html

* Find files which are larger than 5 MB in size:

Code: find . -size 5000k -type f

Here the ' ' in ' 5000k' indicates greater than and k is kilobytes. And the dot '.' indicates the current directory. The -type option can take any of the following values:
f - file
d - directory
l - symbolic link
c - character
p - named pipe (FIFO)
s - socket
b - block device

* Find all empty files in your directory:

Code: find . -size 0c -type f

* Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:

Code: find . -empty -maxdepth 1 -exec rm {} \;

* To search for a html file having the text 'Web sites' in it, you can combine find with grep as follows:

Code: find . -type f -iname \*.html -exec grep -s "Web sites" {} \;

* Compress log files on an individual basis:

Code: find /var -iname \*.log -exec bzip {} \;

* Find all files which belong to user lal and change its ownership to ravi:

Code: find / -user lal -exec chown ravi {} \;

* xargs command can also be used instead of the -exec option as follows:

Code: find /var -iname \*.log | xargs bzip –

* Find all files which do not belong to any user:

Code: find . –nouser

* find files of size between 700k and 1000k:

Code: find . \( -size 700k -and -size -1000k \)


* How about getting a formatted output of the above command with the size of each file listed ?:

Code: find . \( -size 700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null


* You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:

Code: find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

Regards
Nobs

Monday, July 6, 2009

How to Reset a Linux Box root password.

If the linux box protected with root password only.

Do a hard reboot on the machine and click on ‘e’ button to edit the boot like when you get the grub menu. Scrol down to the line which starts with kernel and hit ‘e’ again to edit and add ‘single’ (with out quotes) at the end and hit Enter. Now use ‘b’ to boot then you will get single user mode from where you can change the server root password.

If the linux box is protected with a password for single user mode.

Few system require root password to boot into ‘single mode’ in that case try to edit the gurb boot line and add ‘init=/bin/bash’ at the end of kernel line. You will be redirected to a bash shell instead of init.

Now you may want to mount the root paritition with read/write permission.

mount –no remount,rw /

Then reset the password.

If the Grub is protected with a root password, then try booting from a LiveCD and open a root shell. Execute fdisk –l to show the available disk partition. Mount the root paritition to /mnt

mount -o,rw /dev/hda1 /mnt

Make sure that you mount a root partition. Next change this partition as your root directory using following command

chroot /mnt

Now try to change the root password using passwd command.

Regards
Nobs

Increasing Mysql Packet Size

A communication packet is a single SQL statement sent to the MySQL server, a single row that is sent to the client, or a binary log event sent from a master replication server to a slave.

The largest possible packet that can be transmitted to or from a MySQL 5.1 server or client is 1GB.

When a MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues a Packet too large error and closes the connection. With some clients, you may also get a Lost connection to MySQL server during query error if the communication packet is too large.

So if you want to handle big packets, you must increase this variable both in the client and in the server.

The server's default max_allowed_packet value is 1MB. You can increase this if the server needs to handle big queries. For example, to set the variable to 16MB, start the server like this:

# mysqld --max_allowed_packet=16M

You can also use my.cnf to set max_allowed_packet. For example, to set the size for the server to 16MB, add the following lines in my.cnf file:

[mysqld]
max_allowed_packet=16M


You can also get strange problems with large packets if you are using large BLOB values but have not given mysqld access to enough memory to handle the query. If you suspect this is the case, try adding ulimit -d 256000 to the beginning of the mysqld_safe script and restarting mysqld.