19

I have some doubts regarding *nix.

  • I don't know which type of executable file is ls, whether it is .sh or .ksh or any other kind of system executable if it is, what is that?

  • when I tried to see what is the source code of ls command looks like, it shows something unreadable, what method does *nix use to create these types of unreadable files and can I make my files similar to these files (like ls - unreadable).

2

2 Answers 2

31

You can determine the nature of an executable in Unix using the file command and the type command.

type

You use type to determine an executable's location on disk like so:

$ type -a ls
ls is /usr/bin/ls
ls is /bin/ls

So I now know that ls is located here on my system in 2 locations:/usr/bin/ls & /bin/ls. Looking at those executables I can see they're identical:

$ ls -l /usr/bin/ls /bin/ls
-rwxr-xr-x. 1 root root 120232 Jan 20 05:11 /bin/ls
-rwxr-xr-x. 1 root root 120232 Jan 20 05:11 /usr/bin/ls

NOTE: You can confirm they're identical beyond their sizes by using cmp or diff.

with diff
$ diff -s /usr/bin/ls /bin/ls
Files /usr/bin/ls and /bin/ls are identical
with cmp
$ cmp /usr/bin/ls /bin/ls
$ 

Using file

If I query them using the file command:

$ file /usr/bin/ls /bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x303f40e1c9349c4ec83e1f99c511640d48e3670f, stripped
/bin/ls:     ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x303f40e1c9349c4ec83e1f99c511640d48e3670f, stripped

So these would be actual physical programs that have been compiled from C/C++. If they were shell scripts they'd typically present like this to file:

$ file somescript.bash 
somescript.bash: POSIX shell script, ASCII text executable

What's ELF?

ELF is a file format, it is the output of a compiler such as gcc, which is used to compile C/C++ programs such as ls.

In computing, the Executable and Linkable Format (ELF, formerly called Extensible Linking Format) is a common standard file format for executables, object code, shared libraries, and core dumps.

It typically will have one of the following extensions in the filename: none, .o, .so, .elf, .prx, .puff, .bin

5
  • @lgeorget - See this Q&A for tips on using type vs. which: unix.stackexchange.com/questions/85249/…. It's generally best to just use type anywhere you're tempted to use which or whereis.
    – slm
    Mar 7, 2014 at 14:17
  • 3
    Additionally, for GNU tools, the source code is freely available, start your search at gnu.org/software Mar 7, 2014 at 14:55
  • For the identical test, you really should be using something like md5sum or sha1sum, not just checking file sizes and times.
    – Bob
    Mar 8, 2014 at 10:51
  • @Bob - Those files being identical had nothing to do with the Q so I showed a simple method. To compare the files I would've used a more apt tool such as cmp which is meant for comparing binary files. Also looking to see what package they're provided by would indicate if they're related or not. They're both part of coreutils-8.21-13.fc19.x86_64 and I just know they're identical from 20 years of dealing with Red Hat releases.
    – slm
    Mar 8, 2014 at 12:33
  • @Bob - see updates for examples on comparing files.
    – slm
    Mar 8, 2014 at 12:38
9

It is a binary executable (compiled into machine code, like most of the system). Shell scripts are more like "glue" to join parts together to quickly and flexibly create solutions out of existing stuff. That's the power of *nix.

You need the source code (c, sometimes c++, are the most common languages on *nix), not just the compiled executable. As it is open source, you can get the code for everything from online repositories (core utilities are usually from the gnu project). However, it's a bit tricky if you don't know how to use git or other version tracking systems.

Here is the ls.c file, if it helps: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

1
  • Oh and to clarify the question how to create binary applications: choose among countless programming languages (except for a couple that are interpreted or semi-interpreted, such as python, java, javascript,... which don't traditionally produce a standalone executable binary file). Then learn to use that language and how to compile it.
    – orion
    Mar 7, 2014 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .