7 Programmer-friendly Linux Commands

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
11 Min Read
Linux Commands for Programmers

This tutorial is for all C/C++ and Java developers who write code on Linux platforms. Here we brought essential Linux commands programmers can use to debug, find, and fix defects in their programs and applications.

Essential Linux Commands Programmers Should Know
Linux Commands for Programmers

Why a Special Linux Guide for Programmers?

Most programmers use GDB or other profiling tools like Valgrind for debugging and fixing their code. But there are several common issues that either these tools can’t solve or the programmer prefers searching for easier methods.

Let’s talk about a few of these pain points that Linux commands can easily address.

One of the common issues is when a program fails to open a file for writing for an unknown reason. However, the programmer ensured the file was present, its path was correct, and permissions were okay. Still, the program is not able to open it.

In another case, the application loading a shared object (SO) file, ran without any error but not yielding the desired output. The programmer made some fixes in the app and added a few debug logs. But the problem remained after the execution. Also, not a single line of logs surfaced during the last run.

Must Read: Linux Commands Cheat Sheet for New Users

Another common issue programmers face while linking a program with a library is the undefined symbols error during the build process.

Linux Commands for Programmers

There could be several other scenarios where a programmer needs to look up ways other than GDB. That’s where the below Linux commands would be useful in resolving many runtime issues.

“Nm” Command

This command displays the list of symbols from object files like shared (.so files)/static (.a files) libraries and executables.

There is a list of options that you can use with the nm command, but we’ll cover the ones you need.

If you like to search whether a library contains any undefined function, consider running the following command.

$ nm -u <library_name> | grep "<function_name>"

#library_name -> provide the name of the library.
#function_name -> specify the name of the symbol or function.

To search for global or external symbols, check out the below example.

$ nm -g <obj_file> | grep "<external_func>"

#obj_file -> provide the name of the object file.
#external_func -> specify the name of the external symbol or function.

“ObjDump” Command

Every C/C++ program translates into assembly code during execution. The <objdump> command can help you see how your code would look after disassembly.

If you’ve compiled the program in debug mode, follow the below command to print the disassembly in a readable format.

$ objdump -d -M intel -S <obj_file>

#1. The -d flag is to begin the disassembly process.
#2. The -M intel option is to print the output in Intel assembly format.

“Ldd” Command

The Ldd command is a life-save for programmers in various situations on Linux.

Consider – A program fails due to missing libraries and you are tasked to find the exact issue. It could be because of many reasons like the following.

  • Insufficient permission,
  • Wrong path, or
  • The library is missing.

The <ldd> command will display the shared library dependencies of your program. It’ll also print the path of the library files in its output.

$ ldd <obj_file>

#<obj_file> is the name of your executable along with its path.

Here are a few things to remember while digging out the root cause of the library loading error.

  • It’s the job of the dynamic loader program (ld.so) to find and load libraries for a program to run.
  • The loader primarily searches its cached database (/etc/ld.so.conf) which stores a pre-compiled list of lib files.
  • You can update the cached entries using the <ldconfig> command. On the other hand, you can also enter directly into the </etc/ld.so.conf> file.
  • However, you can also use the two environment variables for specifying the dynamic library search path.
    1. LD_PRELOAD –  A list of specific libraries to be loaded before others.
    2. LD_LIBRARY_PATH – It’s a list of directories to search when loading libraries in a program.

Question Bank: Basic Linux Questions and Answers

“Addr2line” Command

Another handy tool for programmers is the Addr2line command on Linux.

Probably, you have seen a situation when you get hold of a hex value (a memory address) that could lead you to the actual problem. But you don’t know how to proceed with that information.

In such a case, you can use the <addr2line> command. It can translate a memory address into a filename with a line number.

$ addr2line -e <your_program> <mem_addr>

#your_program is the name of your program.
#mem_addr is the address you like to interpret.

This command is quite useful when you have to analyze a crash report but don’t have access to the core dump and the debugger. So it is one of the key Linux Commands programmers can use with a tiny piece of information like an address.

Let’s take an example of a C program that prints the address of a function at runtime.

#include <stdio.h>

void test_proc() {
  printf("%pn", &test_proc);
}

int main(void) {
  test_proc();
  return 0;
}

Compile and run the above code. It will give you the address of the “test_proc” method. The <%pn> is a floating-point format specifier that will get you the correct address in hex format.

Check out the below demo of the <addr2line> command.

$ gcc -ggdb address.c -o address

$ ./address
0x204236

$ addr2line -e address 204236
address.c:3

“Lsof” Command

It is one of the simplest Linux commands that help programmers check if a process has opened any file.

Check the open state of a file.

$ lsof <path/filename>

List files opened under a directory.

$ lsof +D /home/test/

Show files opened by a user.

$ lsof -u techbeamers

Display files opened by a process.

$ lsof -p <process_id>

List processes listening on a particular port.

$ lsof -i :8080

Show all TCP/UDP connections.

$ lsof -i tcp; lsof -i udp;

“Readelf” Command

This command is an extension to the <objdump> command. It fetches more details about ELF files.

The most common usage of the <readelf> command is as follows.

$ readelf --symbols ./elfdemo
    
  5 Symbol table '.dynsym' contains 10 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
         1: 6000000000000de0     0 OBJECT  GLOBAL DEFAULT  ABS _DYNAMIC
         2: 0000000000000000   176 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2 (2)
  ...

The following command will fetch the list of all dynamically linked dependencies of a binary. Run this as shown below.

$ readelf -d elfdemo | grep NEEDED

0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Question Bank: Linux Questions One Should Know for Interview

“Od” Command

This is one of the special Linux commands built to help programmers check if a file contains special chars.

When a file contains non-printable characters, then you can use the <od> command to display output in the default octal format.

It is a useful command for debugging the scripts containing special chars. Let’s show the content of a file in octal format.

$ od -b /usr/lib/libc.so
0000000 057 052 040 107 116 125 040 154 144 040 163 143 162 151 160 164
0000020 012 040 040 040 125 163 145 040 164 150 145 040 163 150 141 162
0000040 145 144 040 154 151 142 162 141 162 171 054 040 142 165 164 040
...

Now, display the content of a file in char format.

$ od -c /usr/lib/libc.so
0000000   /   *       G   N   U       l   d       s   c   r   i   p   t
0000020  \n               U   s   e       t   h   e       s   h   a   r
0000040   e   d       l   i   b   r   a   r   y   ,       b   u   t    
0000060   s   o   m   e       f   u   n   c   t   i   o   n   s       a
...

Show content after skipping some bytes.

$ od -j7 -c /usr/lib/libc.so
0000007   l   d       s   c   r   i   p   t  \n               U   s   e
0000027       t   h   e       s   h   a   r   e   d       l   i   b   r
0000047   a   r   y   ,       b   u   t       s   o   m   e       f   u
...

Show limited bytes in the output.

$ od -N7 -c /usr/lib/libg.a
0000000   !   <   a   r   c   h   >
0000007

One more, let’s display the content in decimal integer format.

$ od -i demoapp
0000000   261051569   261182643   261313717   261444791
...

Related Guides:
Basic Linux Commands for Beginners With Examples
Learn to Implement C++ Class in C Programming

Before You Leave

We tried to bring in the 7 most useful Linux commands that any programmer can use in his routine debugging tasks. In the future, you’ll see us adding more commands to this guide.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

Enjoy learning,
TechBeamers.

Share This Article
Subscribe
Notify of
guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments