TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: 7 Programmer-friendly Linux Commands
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

7 Programmer-friendly Linux Commands

Last updated: Feb 25, 2024 10:34 am
By Meenakshi Agarwal
Share
11 Min Read
Essential Linux Commands Programmers Should Know
Linux Commands for Programmers
SHARE

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.

Contents
1. The "nm" command to find symbols2. The "objdump" command to disassemble a program3. The "ldd" command to display dynamic dependencies4. The "addr2line" command to map an address to a location in the program5. The "lsof" command to identify open files6. The "readelf" command to display ELF information7. The "od" command to display octal dump

Also Read: Learn to Implement C++ Class in C Programming

Essential Linux Commands Programmers Should Know
Linux Commands for Programmers

Most of the 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 made sure that 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 is loading a shared object (SO) file, running 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.

Also Read: Linux Commands Cheat Sheet

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

So there could be a number of other scenarios where one needs to look up ways other than GDB. That’s where the set of below Linux commands programmers would find useful in resolving many runtime issues.

Essential Linux Commands Programmers Should Know

1. The "nm" command to find symbols

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 or not, then 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.

2. The "objdump" command to disassemble a program

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, then 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.

3. The "ldd" command to display dynamic dependencies

When a program fails due to missing libraries, then you have 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.

There are a few things that a programmer should 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 –  It’s a list of specific libraries to be loaded before any other libraries.
    2. LD_LIBRARY_PATH – It’s a list of directories to search when loading libraries in a program.

Master Linux: Basic Linux Questions and Answers

4. The "addr2line" command to map an address to a location in the program

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 which can translate a memory address into a filename and the 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 very 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;
}

When you compile and run the above code, it will leave you with 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

5. The "lsof" command to identify open files

It is one more cool Linux command that can help a programmer check if a process has opened any file.

Check the open state of a file.

$ lsof <path/filename>

Show files opened under a directory.

$ lsof +D /home/test/

Show files opened by a user.

$ lsof -u techbeamers

Show files opened by a process.

$ lsof -p <process_id>

Show processes listening on a particular port.

$ lsof -i :8080

Show all TCP/UDP connections.

$ lsof -i tcp; lsof -i udp;

6. The "readelf" command to display ELF information

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

To get the list of all dynamically linked dependencies of a binary, run the following command.

$ readelf -d elfdemo | grep NEEDED

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

Evaluate Your Skills: Linux Questions One Should Know for Interview

7. The "od" command to display octal dump

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 very useful command for debugging the scripts containing special chars.

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

Show 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

Show content in decimal integer format.

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

Summary – Essential Linux Commands for Programmers

We tried to bring in 7 key Linux commands that any programmer can use in his routine debugging tasks. Also, going forward, we’ll be adding more commands to this post.

Till then, try all these commands and share your experience.

All the Best,

TechBeamers.

You Might Also Like

Basic Linux Commands for Beginners With Examples

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

20 Bash Script Code Challenges for Beginners with Their Solutions

Difference Between Git Repository and Directory

TAGGED:Basic Commands for Beginners
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
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 MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article C# Programming Questions and Answers for Beginners C# Programming Interview Questions for Freshers
Next Article C# Questions - For, While Loops and If Else Statements. 15 C# Questions on For, While and If Else Statements

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x