1

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?

3
  • This is specific to the individual Linux distribution you're using -- each of them has a different way of packaging source code. There is no generic way to see the source for a given tool that works on every distro. (Also, in general, source code is not shipped alongside binaries when your OS is installed; you need to download it separately before it can be viewed). Commented Apr 30, 2022 at 20:39
  • It would probably make this question more answerable to specify a particular distro. (What you're asking to do is exceptionally easy on NixOS, but NixOS is not exactly a mainstream distribution) Commented Apr 30, 2022 at 20:41
  • Your Linux distribution will provide not only the installable package for your system, but also the source-package containing the source code from which it is built. You can simply look at the source package to ensure you have not only the original code, but any patches applied by your distribution in building the package. Commented Apr 30, 2022 at 21:15

3 Answers 3

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.

0

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.

-1

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.
1
  • 1
    There is no point in reverse-engineering an open-source program. It's open-source after all. Commented May 1, 2022 at 7:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.