Linux

How to find size of code and data segments of a program

The Linux provides a command “size” which can show the sizes of various segments of an executable program.

The following example shows how it can be used:

[neo@techpulp ~]# size /usr/lib/libopensync.so.1.0.0
text       data        bss        dec        hex    filename
387800       3516         28     391344      5f8b0    /usr/lib/libopensync.so.1.0.0
[neo@techpulp ~]#

As you can see the “size” command displays size of “text”, “data” and “bss” segments. The above example used a shared library. However the same can be run against a program as well.

[neo@techpulp ~]# size /bin/bash
text       data        bss        dec        hex    filename
729668      19416      More >

How to print all lines of a data file in reverse order

The command “tac” (reverse of cat command) which prints contents of a file in reverse order in terms of lines. It can also concatenate multiple files while printing in reverse order.

Sample data file:

[neo@techpulp ~]# cat data.txt
line1: Mango
line2: Apple
line3: Banana
line4: Papaya
[neo@techpulp ~]#

Output of “tac” command on the same data file:

[neo@techpulp ~]# tac data.txt
line4: Papaya
line3: Banana
line2: Apple
line1: Mango
[neo@techpulp ~]#

How to convert spaces in a file to TABs and vice versa using unexpand and expand commands

The Linux core utils package provides two commands called “expand” and “unexpand” to help converting TAB characters to space characters in a file and vice versa.

To convert TAB characters in to space characters:

expand data1.txt

To convert space characters in to TAB characters:

unexpand data2.txt

How to find MAC address of a linux network interface using SIOCGIFHWADDR ioctl

The following C programming example retrieves all network interfaces in a Linux system and their IP addresses and MAC addresses. The name of network interface and IP address are retrieved using SIOCGIFCONF ioctl while the hardware address of each individual interface is found using SIOCGIFHWADDR ioctl.

/* iflist.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int get_iface_list(struct ifconf *ifconf)
{
  int sock, rval;

  sock = socket(AF_INET,SOCK_DGRAM,0);
  if(sock < 0)
  {
    perror("socket");
    return (-1);
  }

  if((rval = ioctl(sock, SIOCGIFCONF , (char*) ifconf  )) < 0 )
  perror("ioctl(SIOGIFCONF)");

  close(sock);

  return rval; More >

How to loop or iterate through the output of ls command in a bash script

If your original purpose is to iterate through the list of files and directories present in the current directory, you don’t even have to run ls command for that.

for i in *; do
   echo "$i"
done

Alternately you can use ls to get the list of files and directories as shown below.

for i in `ls`; do
   echo "$i"
done

Other way of writing the above example is:

for i in $(ls); do echo "$i"; done;

The ls command can be used to select files of certain pattern. The following example selects only files will extension “.sh”.

for i in $(ls *.sh); do echo More >