'How to view source code to built in linux commands? [duplicate]

To my knowledge, when I run a command like ping or grep or ls, the Linux OS will search for these command names in the directory /bin; I also know most Linux commands are written in C.

I just wanted to view how ping or ls is written in the actual C. But when I cat a file with cat /bin/ping, it spits out garbage data because it's in binary format.

So is there a way to see the commands written in C?



Solution 1:[1]

It helps to know which package provides the command. For example, on many (but not all!) Linux distributions, the ls command is provided by the GNU coreutils package. You can find the sources for those commands by following links from the coreutils web page.

The ping command is provided by the iputils package; you can find the souces here.


Most distributions provide a mechanism for figuring out what package owns a file. For example, on Ubuntu, we can use dpkg-query, like this:

$ dpkg-query -S /bin/ls
coreutils: /bin/ls

The corresponding command for Red Hat/Fedora/CentOS/etc is:

$ rpm -qf /bin/ls
coreutils-8.32-33.fc35.x86_64

If you know the package that provides the files in which you are interested, you can often find the sources by looking for the corresponding source package provided by your distribution. For Ubuntu, you can go to https://packages.ubuntu.com/ and look up packages by name; if you search for iputils, you would end up here, and you will find links to download the sources used to build that package.

For Fedora packages, you can go to https://packages.fedoraproject.org/ and search for the package. In this case, you would need to read the spec file (e.g., this one for iputils) to figure out where to find the sources.

If you are using something other than Ubuntu or Fedora, there will probably be a similar mechanism available. Most distributions also provide command line tooling for downloading the source packages by name.

Solution 2:[2]

When you say cat /bin/ping, you are trying to look at the binary code for the command that has been compiled from the C source. You should be looking for the C source code for the command. Please refer to How do I read the source code of shell commands? for more information.

Solution 3:[3]

there are some ways to analyze the binary command in linux, including:

  1. file,determine the file type.
  2. ldd,Print shared object dependencies.
  3. ltrace,A library call tracer.
  4. Hexdump,Display file contents in ASCII, decimal, hexadecimal, or octal.
  5. strings,Print the strings of printable characters in files.
  6. readelf,Display information about ELF files.
  7. objdump,Display information from an object file.
  8. strace,Trace system calls and signals.
  9. nm,List symbols from object files.
  10. gdb,The GNU debugger.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 larsks
Solution 2 unxnut
Solution 3 muru