Command Line
Command Line Tools
How to find size of code and data segments of a program
Aug 4th
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
Jul 14th
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
Jul 12th
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
Jan 13th
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 generate or print a sequence of numbers in bash
Jan 11th
The command “seq” can be used to generate a sequence of numbers in bash. The following is the syntax of “seq” command.
seq LAST seq FIRST LAST seq FIRST INCREMENT LAST
Look at the following to understand how it works.
Example 1:
[sara@sara-desktop ~]$ seq 5 1 2 3 4 5 [sara@sara-desktop ~]$
Example 2:
[sara@sara-desktop ~]$ seq 5 10 5 6 7 8 9 10 [sara@sara-desktop ~]$
Example 3:
[sara@sara-desktop ~]$ seq 0 10 50 0 10 20 30 40 50 [sara@sara-desktop ~]$
Generally the “seq” command prints one number per line as it assumes “\n” as the separator. However the character used as More >
How to print a bash variable along with other text
Jan 10th
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 convert a relative path to an absolute path in bash
Jan 9th
The “readlink” command can be used to convert a relative path to an absolute path. Although readlink primarily for reading value of a symbolic link, it works transparently on files and directories.
[liz@techpulp ~]# readlink ../../var/www/wiki/index.php /usr/share/mediawiki/index.php [liz@techpulp ~]#
Let us see how it behaves for an invalid path (i.e missing file or directory).
[liz@techpulp ~]# readlink ../../var/www/wiki/index.php1 [liz@techpulp ~]#
As you can see, readlink command doesn’t print anything if the file or directory is not present. Because our intention is to just convert a relative path to absolute path and not to worry about its existence, we can use “-m” option More >
How to delete a file whose name starts with minus character
Dec 21st
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 >
Dump binary file in different formats
Nov 26th
The “od” command can be used to dump a binary file at command line in Linux. Few examples are given below.
ASCII charcters: od -t c mydata.bin
Hex bytes: od -t x1 mydata.bin
Hex shorts: od -t x2 mydata.bin
Decimal shorts: od -t d2 mydata.bin
If you want a visual binary editor, you can try khexedit or xbindiff programs.


Recent Comments