6

I am trying to understand Linux and I am often solving my problems on a case by case basis without actually learning how to come up with the answer myself, which of course, I prefer not to do.

For example I have set a keyboard shortcut to select an area and take a screenshot of it. Everytime I select an area, it always defaults to /Pictures, and I would like to edit either the default directory or the quick dropdown choices it presents me.

I have found an answer as usual, however this time I would like to know how do people normally approach this if there was no answer. Surely there must be a logical set of steps to pinpoint how a terminal command works/where it takes its arguments.

For this particular case, I have looked online for which command Ubuntu uses to take screenshots, which turns out to be import.

So my question is, what is/are the approach(es) to

  1. Find where configuration files / location / code level information (like not how to call it but what it specifically does when it is called) of a terminal command

  2. How to find out which terminal command a particular action corresponds to. (Figuring out import is called when I took a screenshot for example)

In other words, how could I have figured this out myself if I had no access to internet, or no one knew the answer? :) I am more interested in the approach/logic, however import in this case would be a good example.

Edit:

I do not think my question is the same with the suggested possible duplicate because I am asking for a way to reach source code if that is possible. In other ways, I am asking for a way to read the source code of a command, and files it uses (for example maybe there is a config file for import function and if I could have found it I would have changed default-save-directory). To clarify even further, the file I am trying to find (if it is possible to find it) a code equivalent to the following pseudo code:

<when run>
change mouse pointer to + shape
..wait for a click..
on click -> wait for release..
on release -> take the area between click and release coordinates
read config file located at ./config and get default-save-directory variable
initialize path to default-save-directory
ask user where to save the image

This has to be saved somewhere in the operating system. "That is not possible because ..." (maybe it is in assembly level for example) is also an answer, but use man to get instructions on what this function does is not an answer because I want to understand how it works.

In yet another words, if a linux ninja was to modify import command on his local computer, where would he go, and say add a signature in the pictures saved with screenshot.

ozgeneral
  • 165
  • 1
  • 7

4 Answers4

9

Quick Answer:

Research the terminal command.

There's no absolute set rule of where the variables or details are stored. If you don't know the details of a terminal command, you'll have to look it up to be sure.

Most terminal commands, especially the commands that have lots of options, have manuals that are installed on the system by default, which makes this looking up easy. That also includes the shell for the terminal commands which in the case of Ubuntu is bash by default.

Before the internet was so popular for performing the not knowing that you refer to, people relied much on the libraries, books, and manuals. People had many books and manuals in their home. The went to the bookstore and library often. I used to spend many hours in the library, almost always had two or three books checked out for home research (or programming and commands). It's been around 20 years since I went to the library to do research. I do from time to time go to the library for some seminars, or meetings. I also belonged to two computer clubs, who shared information. Both clubs published monthly newsletters which included important computer and programming information.

If you don't have access to the Internet you can use the manuals that are already your computer for the terminal commands.

Thank of a terminal command and type this:

$ man [terminal command]

You can research bash and the import command (from your question) with:

$ man bash
$ man import

Both manuals will show you lots of information that you can research without the internet. They also include a glossary which you can use words from those manuals to do further research.

To know the details of the variables of a command, you'd have to research the particular command. Some of the same commands might even have various flavors or variations which might store some of it's variable or details in a different area depending on the Linux distribution or the maintainer of the specific command.

How to download the source code of Ubuntu's commands and applications:

You can download the source code of the commands by adding the source to your repository. You can find this by:

  • Goto Software & Updates ->
  • (click on the tab) Ubuntu Software ->
  • (put a check mark on) Source code

    Use this command to get the source package you want to download:

    $ sudo apt source <package name>
    
  • L. D. James
    • 25,444
    5

    Ubuntu actually uses gnome-screenshot, not import (at least on 14.04). How did I figure that out? Well first of all, import is quite old, so that tipped me off. But also...

    • man -k can be used to search the descriptions of terminal commands.

      I looked for some keywords related to screenshots, like "screenshot", "capture", "screencap", etc. Depending on the search term, this either brought up a few items or way too many. I saw a few commands keep coming up, mostly gnome-screenshot and shutter (which I installed).

      Where we're dealing with the GUI, it could also help to search the application files, e.g:

      find /usr/share/applications/ ~/.local/share/applications/ -iname "*screenshot*"
      grep -i "screenshot" /usr/share/applications/*.desktop ~/.local/share/applications/*.desktop
      
    • I opened gnome-system-monitor, sorted by Started, and watched to see if a command popped up when I took a screenshot. That's when I confirmed it was gnome-screenshot

      On the command line, you could use

      watch "ps -ef --sort=start_time | tail"
      
    wjandrea
    • 14,504
    2

    Finding the source

    Yes, you can easily find and read the source code of everything in the Linux world, except for some edge cases (like binary-only video card drivers).

    For example, to find out more about the ls command, google ls source code, which will give you one special google hit right on the top, which tells you that ls is part of the coreutils.

    The coreutils, which are GNU, not Linux, are the commands you use all day in most distributions. From that landing page, you easily can either download the whole tarball, or browse their git repository directly.

    For example, this is the most interesting directory (at this point in time, the link shows a fixed hash, so be sure to browse from the top again if you read this answer later): historic view of "src" in the master branch

    Similar approaches work for all other open source programs including the kernel itself.

    Where is everything?

    which ls shows where that specific binary is located.

    The Linux Filesystem Standard lists all the usual places where files are kept.

    Package management

    Depending on your distribution, you may more or less easily download source packages through your package manager (e.g., apt-get source $package), which should be even more painless. This has the obvious benefit that it is a) very easy and b) should return exactly the version of the sourcecode that your binary is actually using. Depending on the distribution of course.

    Finding stuff

    The meat of your question is how to find out about the behaviour of some piece of software. This is usually non-trivial if you just fetch the source. In extreme cases, i.e., if the software is really critical/essential for you, and you absolutely must get it to work, then this might be a viable route, but in general, you should be better off googling. I've done my fair share of (ab)using the source code of large packages, specifically when compiling software (often there are weird edge cases where libraries or compiler versions have slight mismatches), and I assure you it can be fulfilling, but is often a lot of work.

    If you do find a solution to your problem, be sure to let it flow back to the current maintainers via pull request, if you think it's worthwhile for others (and not just fixing the software to conform more to your personal wishes).

    As to how other people find out; often they do because they actually work on the software and are contacted through their bug tracker - knowing the code base already helps tremendously.

    Then there are, of course, your genuine hackers who actually like to deep dive into the source code for anything they stumble about.

    All in all, for any real problem, it is incredibly likely that someone had it already, and has posted about it, so googling is certainly not a bad approach. Using the correct key words can be an art in itself.

    Taking it to the next level

    All of this is nice and fun, but if you want to go real deep into the rabbit hole, you can bootstrap Linux from scratch. This website guides you through building a complete (albeit very small) Linux system completely from scratch, i.e., from source code. No ready-made binaries at all.

    AnoE
    • 464
    1

    man <command> is often an excellent place to start, read through it and see if you can find what you need.

    An often forgotten command is info, this always has additional information unfortunately for import it doesn't serve very well but for a test try man gpg, and it gives you a very brief overview then try info gpg you will see a lot more information.

    Some of the more obscure/older commands will have good info pages.

    Good luck.

    Pete
    • 21