Command Line

Command Line Tools

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 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 >

How to print a bash variable along with other text

A general programmer tends to use a simple dollar sign ($) before each variable. However its gets difficult if a variable hash to be echoed along with some other as shown below.

An attempt to print “redtape” fails using the following method as bash attempts print value of “COLORtape” variable.

bash# COLOR=red
bash# echo $COLORtape
bash#

In such cases, braces should be used to protect the bash variable. The following example does that.

bash# COLOR=red
bash# echo ${COLOR}tape
redtape
bash#

How to delete a file whose name starts with minus character

Sometimes an untended paste operation of screen dump results in unwanted files. If a file name starts with a minus (-) character, it is not possible to delete the file using traditional command as anything that starts with a minus (-) character is treated as one of the command line options.

For example, look at the following file “-myfile.txt“.

[sara@techpulp ~]# ls
-myfile.txt
[sara@techpulp ~]#

Traditional way fails to remove the file.

[sara@techpulp ~]# rm -myfile.txt
rm: invalid option -- 'm'
Try `rm --help' for more information.
[sara@techpulp ~]#

You can use “–” option to stop “rm” command to stop expecting further command More >