Monday, August 31, 2009

Fixing Wired Network (eth0) Ethernet

While using Debian based systems(like Ubuntu), I often found that network manager just got into the way whenever I wanted to fix an internet connection. So, first thing to do is to kill the associated processes (kill -9) after you find the process ID by using ps. Or you can totally get rid of these packages (usually network-manager, network-manager-gnome in GNOME or knetworkmanager in KDE).

Then, edit /etc/network/interfaces file.

$ sudo vi /etc/network/interface

auto eth0

iface eth0 inet dhcp

again assuming that we want to have a DHCP based connection.

Then restart the network connection by issuing the following command:

$ sudo /etc/init.d/networking restart

Friday, August 14, 2009

Determining Bits of CPU and OS (32/64-bits)

In order to determine whether OS/CPU is 32-bit or 64-bit,
you can use this shell script.


#!/bin/bash
echo -n "Running "

RES=`uname -a | grep 64`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo -n "operating system on a "

RES=`cat /proc/cpuinfo | grep " lm "`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo "machine"




Sample Run:

$ ./os_cpu.sh
Running 64-bit operating system on a 64-bit machine

Wednesday, August 5, 2009

Let Linux Speak Time and Day for You

I have written a single line command that tells time of the day:

$ espeak "Time is `/bin/date \"+%H
hours %M minutes %S seconds\"`"




You need not to type this command every time, just run the

following commands only one time:


$ echo "alias speak_time='espeak \"Time is \`/bin/date \"+%H hours %M minutes %S seconds\"\`\"'" >> ~/.bashrc

$ source ~/.bashrc




Now, you will be able to run speak_time any number of time at any time, and you will get exact time. :-)


$ speak_time



PS: Requirement of above command is "espeak"


$ sudo apt-get install espeak


For speaking day, just add following alias in your .bashrc file

alias speak_day='espeak "Today is `/bin/date \"+%A, %d %B 20%y\"`"'

Monday, July 27, 2009

Mount Windows Shared Folder onto Linux Directory

Suppose you want to mount Windows shared folder (say shared_folder) on windows machine (IP address 10.226.194.169) on a folder (say /local/smbshare/shared_folder), you just need to give following
command:


$ sudo mount -t smbfs -o username=domainname
\\yourname,password=yoursecretpassword -o gid=users,dmask=777,fmask=777,rw //10.226.194.169/shared_folder /local/smbshare/shared_folder

You can verify whether the above command is successful or not, by using:
$ mount | grep shared_folder

If the above command shows some output, it means mount is successful :)

Sunday, July 12, 2009

Mounted Filesystem Information

For getting information of a partition (filesystem) which is mounted on a directory on Linux System, I have created a C program. Its listing is given as below:





/*
* =====================================================================================
*
* Filename: file_system_stat.c
*
* Description: Get file system statistics of mounted partition
*
* Version: 1.0
* Created: Thursday 09 July 2009 10:48:02 IST IST
* Revision: none
* Compiler: gcc
*
* Author: Mitesh Singh Jat (mitesh)
*
* =====================================================================================
*/

#include <stdio.h>
#include <sys/statfs.h>

int file_system_stats(const char *pathp)
{
struct statfs sfs;
int retval;
if (pathp == NULL)
{
fprintf(stderr, "file_system_stats: cannot find stats of NULL path\n");
return (-1);
}
retval = statfs(pathp, &sfs);
if (retval < 0)
{
fprintf(stderr, "file_system_stats: cannot find file system\n");
fprintf(stderr, " stats of partition %s\n", pathp);
return (-1);
}
printf("\n=== Filesystem Stats of '%s' ===\n", pathp);
printf("Optimal Transfer Block Size = %ld\n", sfs.f_bsize);
printf("Total data blocks = %ld\n", sfs.f_blocks);
printf("Total free data blocks = %ld\n", sfs.f_bfree);
printf("Total file nodes = %ld\n", sfs.f_files);
printf("Total free file nodes = %ld\n", sfs.f_ffree);
printf("=================================================\n\n");
return (retval);
}

int main(int argc, char *argv[])
{
int i;
int retval = 0;
if (argc <= 1)
{
fprintf(stderr, "Usage: %s <path> [more_paths...]\n", argv[0]);
retval = file_system_stats("./");
return (retval);
}

for (i = 1; i < argc && retval >= 0; ++i)
{
retval = file_system_stats(argv[i]);
}
return (retval);
}



Compilation:
$ gcc -Wall -o file_system_stat file_system_stat.c

Sample Runs:
--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 505)
$ ./file_system_stat
Usage: ./file_system_stat <path> [more_paths...]


=== Filesystem Stats of './' ===
Optimal Transfer Block Size = 4096
Total data blocks = 9690330

Total free data blocks = 3956097

Total file nodes = 2449408

Total free file nodes = 2315870

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(0 : 506)
$ ./file_system_stat /boot

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 507)
$ ./file_system_stat /boot /mnt/extra

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================



=== Filesystem Stats of '/mnt/extra' ===

Optimal Transfer Block Size = 4096

Total data blocks = 21235766

Total free data blocks = 7269970

Total file nodes = 5357568

Total free file nodes = 5357279

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(1 : 508)
$ ./file_system_stat /this_is_not_a_partition
file_system_stats: cannot find file system

stats of partition /this_is_not_a_partition





Note: By the way, the block size of a filesystem can be found using commands given at here or here.

Wednesday, July 1, 2009

Removing Files With Unusual Characters

Suppose, some one (miscreant) got access to your computer and
creates files, which you may not be able to delete or you will end
up deleting all the files/subdirectories of the directory on which
these files are created.

Some examples of such dangerous file names are:
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

Now, how are you going to delete above files?

(1) * : If one tries
$ rm *
or
$ rm -r *
This will delete all the files(& all subdirectories) in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(2) -rf *: If one tries
$ rm -rf *
This will delete all the files & all subdirectories in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(3) -rw-r--r--: If one tries
$ rm -rw-r--r--
rm: invalid option -- w
Try `rm ./-rw-r--r--' to remove the file `-rw-r--r--'.
Try `rm --help' for more information.
Solution:
$ rm ./-rw-r--r--
Other Solution
$ rm -- -rw-r--r--

Note: So we need to be vigilant, while using 'rm' command.

Friday, June 26, 2009

Finding Block Size of Filesystem

In order to, find block size of a filesystem on Linux,

$ sudo tune2fs -l /dev/sda1 | grep -i 'block size'
Block size: 4096

OR

$ echo "mitesh"> test && du test | awk '{print $1}' && rm -f test
4K