3

I'd like to get a custom output from the tree command, but unlike this question, I don't have a fixed format. I'd like to be able to give the command the format in an argument (for instance perhaps -f=y, -f=yaml,-f=xml,-f=~/myformat.fmt).

Obviously this is a huge undertaking, but I feel it would be a good way to get to explore how some of the linux commands work under the hood, along with stretching my programming skills.

Where should I start if I want to edit (and I presume compile etc) 'native' Linux commands? Are they baked in?

AncientSwordRage
  • 1,774
  • 1
  • 20
  • 27

2 Answers2

2

No, Linux commands aren't 'baked' in. A large number are part of GNU coreutils, but each one is still a separate program within this package and can ultimately be compiled on its own.

Of course the tree command isn't anything to do with GNU, its project page can be found here - http://mama.indstate.edu/users/ice/tree/. The source code is available to download and contains build instructions (I assume you are already familiar with make and gcc). A source package will also be available for your distro and may include additions/patches not available in the upstream version (but also may be an older version). Your distro will have instructions on how to compile this. In most cases if you want to code on a package, the upstream source will be the best one to go with though.

Looking at the changelog for tree though, I see that XML output was added in version 1.6.0 (-X option) and HTML has always been available. There are plenty programs already available to convert between the various markup languages. xml2yaml is a specific one that would give you yaml. Also pandoc is the most comprehensive one I am aware of and has support for custom formats (although creating one is a programming task in itself).

If you still want to go ahead and try develop these features directly for tree though, I would recommend contacting the developer and asking if they would like to incorporate them. If they are interested and you can provide patches which are of a high enough quality, it is very likely that they would eventually be incorporated into the real program. From what is in the program already, it is quite likely that he would be interested in adding yaml or json output (on the other hand though, he may feel that the program has enough feature bloat already).

A custom format seems more ill advised to me though, as it sounds like a large undertaking for such an otherwise simple tool, unless there is a very simple way to implement it. You could like with the libraries of a larger project like pandoc for conversions, but again this is ill advised for a basic tool.

Graeme
  • 34,027
1

On Debian, Ubuntu, Mint and other distributions using Dpkg and APT to manipulate packages:

  • dpkg -S /path/to/file looks for the installed package containing the specified file, e.g.

    dpkg -S /usr/bin/tree
    dpkg -S $(which tree)
    
  • apt-file search /path/to/file looks for the package in the distribution containing the specified file, e.g.

    apt-file search /usr/bin/tree
    
  • A few commands are built-in, i.e. baked into your shell. Their source code is part of the shell. Use type to find whether a command is built in.

    $ type cd
    cd is a shell builtin
    $ type tree
    tree is /usr/bin/tree
    

The command tree is in the package called tree. You can download and unpack the source code for this package with

apt-get source tree
dpkg-source -x tree_*.dsc

In this case, modifying the source code is not the easiest way. It may be a worthwhile exercise if you want to do some C programming. To achieve the objective, using a higher-level language such as Perl, Python or Ruby will be less work.

  • I've found perl and ruby solutions. I may try all three in the end – AncientSwordRage Jan 27 '14 at 07:44
  • Being able to download source packages on APT systems depends on having the relevant deb-src entries in the sources.list, which AFAIK are commented out by default on Ubuntu. Also, apt-cache show will give a homepage field for most packages, it is at least a good idea to check this for a newer by unpackaged version of the software before any attempt to alter the source code. – Graeme Jan 27 '14 at 17:14