Thursday, August 16, 2007

Execute shell commands from a C program

Heres a C Program that use system() function to
execute shell commands:

See man system for more information:

[root@linuxbox C_Programs]# cat test.c
printf ("Files More than 10 MB !!\n");
system("find . -size +10000k");
return 0;

[root@linuxbox C_Programs]# gcc test.c

[root@linuxbox C_Programs]# ./a.out
Files More than 10 MB !!

Wednesday, August 15, 2007

Programming C under Unix

Writing a test Program in C (Unix):
[root@linuxbox C_Programs]# cat test.c

printf ("Hello Sriram!!\n");

return 0;


Executing the above :
[root@linuxbox C_Programs]# gcc test.c

[root@linuxbox C_Programs]# ls
a.out test.c

[root@linuxbox C_Programs]# ./a.out
Hello Sriram!!

Find command tips ...

Finding Max space used by Top 10 Files in MB:

[root@linuxbox Desktop]# find /tmp -type f | xargs ls -msa
| sort -rn | awk '{size=$1/1024;printf("%dMb %s\n", size,$2);}'
| head

10Mb /tmp/openldap-devel-2.3.34-0.fc7.i386.rpm,
6Mb /tmp/newtonslaw.wav
4Mb /tmp/openldap-clients-2.3.34-0.fc7.i386.rpm,
3Mb /tmp/ntfs-3g-1.516-1.fc7.i386.rpm,
2Mb /tmp/fuse-2.6.5-1.fc7.i386.rpm,
1Mb /tmp/orbit-root/bonobo-activation-server-ior,
0Mb /tmp/gconfd-root/lock/ior,
0Mb /tmp/orbit-root/bonobo-activation-register.lock,

Finding files which are more than 10 MB:

find /tmp -name "*" -size +10000k

Finding files modified 3 days ago but less than 5 days:

find /tmp -type f -mtime +2 -mtime -5

Find files that were accessed exactly 3 days a go
find /tmp -type f -atime 3

-atime +7: All files that were last accessed more than 7 days ago
-atime 7: All files that were last accessed exactly 7 days ago
-atime -7: All files that were last accessed less than7 days ago

To replace recursive in *.html files, with multiline support:
find ./dir/ -name *.html -exec perl -pi -e 'undef $/;

Update the modification date for all files in the
current directory and below:
find . -exec touch {} \;

Remove files in a directory that were created more
than x number of days ago (great for managing Maildir).
find ./ -ctime +x | xargs | rm -f -r

This should delete files older than 7 days, and
print the name of each file it deletes.
find ./ -ctime +7 -name "*.zip" -exec rm -f {} \; -ls

# If you want to be prompted before it deletes a file:
find ./ -ctime +7 -name "*.zip" -exec rm -i {} \; -ls

This will list the files with names ending in .c
and containing the word hello:

find . -type f -name '*.c' -exec grep -l hello {} /dev/null \;

Find all the files recursively that contain foo:
find . | xargs grep foo

a,c,d are the file names that contain foo

Mounting Windows NTFS partition under Fedora 7

I have a dual boot Fedora 7 + Windows 2000 Pro

When I tried mounting the NTFS Partition

# mount /dev/hda1 /mnt/win
mount: unknown filesystem type 'ntfs'

I got the above message.

Then this is what I did to Mount windows NTFS partition
Under Fedora Release 7(Moonshine):

Install the below packages from Fedora core 7 DVD

rpm -ivh fuse-2.6.5-1.fc7.i386.rpm
rpm -ivh fuse-libs-2.6.5-1.fc7.i386.rpm
rpm -ivh ntfs-3g-1.516-1.fc7.i386.rpm

[root@linuxbox tmp]# fdisk -lu

Disk /dev/sda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders,
total 78140160 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 63 51199154 25599546 7 HPFS/NTFS
/dev/sda2 51199155 51407999 104422+ 83 Linux
/dev/sda3 51408000 78140159 13366080 8e Linux LVM

My Windows NTFS partition is /dev/sda1

I create a directory
#mkdir /mnt/win_c/

Then Mount it:
[root@linuxbox tmp]# mount -t ntfs-3g /dev/sda1 /mnt/win_c/

[root@linuxbox win_c]# pwd

[root@linuxbox win_c]# ls
arcldr.exe hawkz.exe RECYCLER
arcsetup.exe hiberfil.sys S4uDi.exe
boot.ini MSDOS.SYS Sriram
citianywhere.log My Music swsetup
citrix.log NTDETECT.COM System Volume Information
Documents and Settings pagefile.sys YServer.txt
Ebooks Program Files