Linux- Finding the size of a directory, finding the disk free space

$ du // This gives you a list of directories that exist in the current directory along with their sizes.
$ du app/ // This gives you a list of directories that exist in the specified directory app/ along with their sizes. 

$ du -h //  Better output, '-h' stands for human readable format. So the sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.

$ du -ah // Lists file size also and in human readable format. 

$ du -c // This gives only total size of the directories and the grand total.

$ du -ch | grep total  // This gives the total size 

$ df 
$ df   // This shows the free space in kilobytes 
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                40316280   5077616  34829228  13% /
udev                   1474708         0   1474708   0% /dev
tmpfs                  1482684      9024   1473660   1% /dev/shm
tmpfs                  1482684       712   1481972   1% /run
/dev/sda6             40316280   5077616  34829228  13% /
tmpfs                  1482684         0   1482684   0% /sys/fs/cgroup
tmpfs                  1482684         0   1482684   0% /media
/dev/sda8             46003208   9172500  34493840  22% /home
/dev/sda6             40316280   5077616  34829228  13% /tmp
/dev/sda6             40316280   5077616  34829228  13% /var/tmp
/dev/sda8             46003208   9172500  34493840  22% /home

$ df -h // With human readable format 

$ df -h | grep /dev/sda8 
/dev/sda8              44G  8.8G   33G  22% /home
/dev/sda8              44G  8.8G   33G  22% /home
$ df -h | grep /dev/sda8 | cut -c 41-43 // This command gives the usage percentage of /dev/sda8 
22%
22%

Reference: http://www.codecoffee.com/tipsforlinux/articles/22.html 

Can’t login to mysql as a root?

One day when I tried to login to mysql it is not permitting me to login as a root. Then I found the following solution to get rid of from this issue.

First you stop the server:

$ sudo /etc/init.d/mysql stop

Edit the cnf file:

$ sudo vim /etc/my.cnf

> Add the following line to section [mysqld]:
skip-grant-tables

Start Mysql:

$ sudo /etc/init.d/mysql start

Reset Root Password:

$ mysql -e "update user set password = old_password('newpassword') where user = 'root'" mysql

Kill MySQL:

$ sudo kill -9 PROCESS_ID

Create an init-file:

$ vim ~/mysql-init

> Add the following lines:

EOF
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';
EOF

Run MySQL with the init file:

$ mysqld_safe --init-file=~/mysql-init &

Remove the mysql-init file:

$ rm ~/mysql-init

Restart MySQL:

$ sudo /etc/init.d/mysql restart

Login as root!
References:
1) http://www.ipreferjim.com/2011/06/cant-login-to-mysql-as-root/

Linux: How to split large files into several parts

HJSplit

HJSplit for Linux is a freeware file splitter for Linux with full graphical user-interface.
Download ‘HJSplit’ from Freebytesoftware

Unix Split command

$ split -l 1000 my_file.txt     # Split the file named my_file.txt into several files with 1000 lines 
$ split -b n my_file.txt	# Split a file into pieces n bytes in size. 

Create, write and read a file and also print the first line of a file in ruby

Create a file: Open a file in write mode

ruby-1.9.2-p290 :058 > f = File.open(“my_file.txt”, “w”)
=>

Write something to that file

ruby-1.9.2-p290 :059 > f.write(“This is the first line of my file\n”)
=> 34

Close the file

ruby-1.9.2-p290 :060 > f.close
=> nil

Open the file for reading

ruby-1.9.2-p290 :061 > f = File.open(“my_file.txt”, “r”)
ruby-1.9.2-p290 :062 > f.read
=> “This is the first line of my file.\n”

Print the first line of a file

ruby-1.9.2-p290 :063 > lines = IO.readlines(“my_file.txt”)
ruby-1.9.2-p290 :063 > puts lines.first
This is the first line of my file.

A useful link about ruby File manupulation is given below

Open ods, xls, xlsx files in ruby by using ‘roo’

Install ‘roo’ gem
$ sudo gem install ‘roo’
take the ruby console
$ irb
for example take an xlxs file and try to open

ruby-1.9.2-p290 :003 > require ‘roo’
if you get an error like
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:6: warning: already initialized constant VERSION
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:7: warning: already initialized constant LIBPATH
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:8: warning: already initialized constant PATH
LoadError: no such file to load — zip/zipfilesystem
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo/openoffice.rb:3:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:71:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from (irb):3
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `’
ruby-1.9.2-p290 :004 >

Then install the ‘rubyzip2’ gem

$ gem install rubyzip2

and open the file
ruby-1.9.2-p290 :003 > file = Excelx.new(“/home/prescience-toshiba/Documents/abhilash.raw.xlsx”)

output:
=> {[1, 1]=>”Name”, [1, 2]=>”Email Id”, [1, 3]=>”Alternate Number”, [1, 4]=>”Date of Birth”, [1, 5]=>”Mobile No.”, [1, 6]=>”Functional Area”, [1, 7]=>”Area of Specialization”, [1, 8]=>”Industry”, [1, 9]=>”Resume Title”, [1, 10]=>”Key Skills”, [1, 11]=>”Work Experience”, [1, 12]=>”Current Employer”, [1, 13]=>”Previous Employer”, [1, 14]=>”Current Salary”, [1, 15]=>”Level”, [1, 16]=>”Current Location”, [1, 17]=>”Preferred Location”, [1, 18]=>”Course(Highest Education)”, [1, 19]=>”Name”, [1, 20]=>”Email Id”, [1, 21]=>”Alternate Number”, [1, 22]=>”Date of Birth”, [1, 23]=>”Mobile No.”, [1, 24]=>”Functional Area”, [1, 25]=>”Area of Specialization”, [1, 26]=>”Industry”, [1, 27]=>”Resume Title”, [1, 28]=>”Key Skills”, [1, 29]=>”Work Experience”, [1, 30]=>”Current Employer”, [2, 1]=>”Rahel Robert”, [2, 2]=>”rtrobert@yahoo.com”, [……………………..