Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've noticed that most of my commands/programs in the /bin directory are compiled. I wish to duplicate this non-readable code format myself. Can someone tell me why and/or how these programs get this way?

[green stamp bonus]: Isn't there a bash command to print all of the "human readable" text from a file?

share|improve this question
1  
Note that not all commands are compiled binaries. Your /bin and /usr/bin/ directories also contain commands that are written in scripting languages like sh, bash, perl, python, etc... Try running file /bin/* | less in a terminal. –  geirha Aug 21 '12 at 20:57

5 Answers 5

up vote 14 down vote accepted

The commands/programs in /bin are not non-readable, but they are, as you say, compiled. This means that they were originally written in C (or some other programming language) and have been compiled to machine code (binary) so the processor can execute the code. To do the same, simply write the program you need in C and run the gcc command to produce an executable file.

To print any human readable text in a files use strings < /bin/file,

share|improve this answer

There are 10 different kinds of people. Guess you belong to the second group... :P

There are only binaries in the bin directory (Binaries = Programms in Binary Code, just 1 and 0)

They are compiled to be machine readable. Machines know 1 and 0 or on and off, true or false, nothing else.

This is done by a compiler. There are more than one compiler. I can think of the Gnucc compiler, or the Java-Compiler, or a Delphi-Compiler... Different compilers for different programming languages.

Try grep. Good Luck!

share|improve this answer
    
Not all commands are compiled binaries... –  geirha Aug 21 '12 at 20:58
    
correct, there are also bash scripts and symlinks in bin directory. Sorry. –  mondjunge Aug 22 '12 at 7:30

To compile Bash programs into binaries ( I'm assuming you're asking about Bash because your question is tagged bash) you should use the program Shc. You can download it from here. After you unpack the archive, compile and install shc with cd shc-3.8.9;make;sudo make install. Now, run shc -f filename, where filename is the path to the file you want to make into a non-readable code format. Shc will create two files: filename.x.c and filename.x. You can get rid of filename.x.c, filename is the non-readable file.

share|improve this answer
    
There's not much point in compiling bash scripts. mywiki.wooledge.org/GreyCat/CompilingBad –  geirha Aug 21 '12 at 20:59

Introduction

When a program is compiled from say C or C++ source code, a compiler such as gcc (see below) translates that code into the native binary language of the CPU, so that the program can be executed. Thus human readable material (source code) is turned into a non-human readable format (machine readable binary code).

Binaries and the Filesystem

If you enter echo $PATH, you will see a locations of all the executables on the system, both compiled binaries and scripts:

/home/mike/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

In the filesystem hierarchy key system tools such as cat and getty are present in /bin and /sbin respectively, and other tools such as dpkg and chroot are present in /usr/bin and /usr/sbin respectively. If you compile a program by downloading the source code, configuring the build, and the installing it, your program by default should go in /usr/local/bin and its other files in the /usr/local hierarchy.

Compilers

Probably one of the most well known tools used to compile binaries is gcc, which is known as the GNU Compiler Collection and earlier as the GNU C compiler; the version shipped with Ubuntu 12.04 is gcc 4.6.3. As it notes in the offical GNU manual,

GCC is an integrated distribution of compilers for several major programming 
languages. These languages currently include C, C++, Objective-C, Objective-C++, 
Java, Fortran, Ada, and Go.

Compiling a binary

The three commands ./configure, make and sudo make install (or sudo checkinstall) are often used together in order to compile a program.

Running ./configure, amongst other things, checks the version of gcc installed and prepares the build environment. The makefile is one of the most important things generated from running ./configure as it directs the build when make is run. The GNU make manual details the process in great depth. In sum, the rules in the makefile determine the main goal (to create an executable) so that the source code .c files will be made into compiled files (.o files) in order to generate the target executable file.

Resources

Two great resources are these Ubuntu compilation guides here and here, which contain some great advice and information for those new to compiling. Other resources such as the GNU gcc manual and the make manual I have mentioned already. For more information on C and creating a simple little C program, see chapter 22 of Rute and chapter 24 of the Linux Command Line.

share|improve this answer

One Option: If you want to see what's in the compiled programs use a tool like objdump to dump out the object code, for example:

objdump -d /bin/ls | less
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.