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.