4

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.

marked as duplicate by pa4080, muru command-line Apr 10 '18 at 1:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @pa4080 There are some differences. The other question was "Help on terminal commands" which the best answer (which was mentioned in most of the answers... The Internet. This user was interested on getting "Help on terminal commands" without with no access to the internet. He mentioned in his question that he can already do it with the Internet. So he had the answers from the other question, which wouldn't work in the case of his immediate question. – L. D. James Apr 9 '18 at 17:46
  • By the title, I thought this was going to be a programming question. It might be clearer if you changed it to something like "How do I find out what command is run by a keyboard shortcut?", though that's still not quite what you're asking. – wjandrea Apr 9 '18 at 18:08
  • I am changing the title now, and clarified the question further. Please consider removing the duplicate task as I have read possible duplicates and still have no idea if what I am asking is possible and if possible, how – Özgen Eren Apr 9 '18 at 18:14
  • @ÖzgenEren With the edit, this is really two different questions: "How can I find out what command is run", and "How can I change the behaviour of a program" – wjandrea Apr 9 '18 at 18:18
  • I thought they are almost same, I can start two questions if you like but I think it would cause redundancy. Because first step of customizing a command is to read what it does. So answer is either <you find it like this, then you can change it as such> or <you find it like this but changing is not possible> or <you cannot find or change a command on your local computer> – Özgen Eren Apr 9 '18 at 18:20
1

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.

  • Thanks a lot for your answer, it was quite useful. I am quite interested in this, just to introduce myself to Linux world better so please forgive the follow up questions. As far as I understood from your answer, terminal commands are hold as binary files in the computer and I use apt-get source to get the source code they were compiled from, is that right? In this case if I wanted to modify something I would get the source code, change & recompile it, and replace the binary files using the same name? And is there a particular place in computer those binary files are stored? (is it /bin) – Özgen Eren Apr 9 '18 at 22:07
  • 1
    @ÖzgenEren, check the "Where is everything?" chapter I added to the answer. Yes, you're right about how you would work with apt-get source. You wouldn't really replace the binary directly, though - you get the package source, build it, and then install the custom package you built. The answer askubuntu.com/questions/28372/… has more on that. – AnoE Apr 9 '18 at 22:38
  • 1
    @ÖzgenEren, also check out the "Taking it to the next level" chapter. ;) – AnoE Apr 9 '18 at 22:42
  • This is gold, thank you! I will try my chances with the bootstrap :) with your answer I can probably already figure this out but still wanted to add: is there any way to locate config files as well? I am guessing most core linux commands dont have seperate files for configuration but have everything hardcoded and take arguments instead, but for example if there is an application and if I look for a config text file to change initial state, default arguments etc, rather than binary file, where do I look? – Özgen Eren Apr 9 '18 at 22:48
8

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>
    
    • Thanks a lot for taking the time for answering, it was pretty helpful. Could you also give an example on how to locate the command import on computer for example, and how to know details of this particular command (just so I have an example to begin with). Because I just checked the man command, and it describes how to call/use the function from terminal which is definitely useful but it would not help when I need to customize or debug something – Özgen Eren Apr 9 '18 at 15:49
    • @ÖzgenEren It's not clear to me the task or what you are trying to do with import. The manual shows me just about anything I could imagine doing with it. Outside fo the usage it shows, if the didn't include my objective, I might have to look for a different command. There are many thousands of commands on your computer. You might have to view a different one for the task. I would have to know a specific task, then look for the command to perform the task. If you specify what you want to do with the terminal command, import, that would help. – L. D. James Apr 9 '18 at 15:59
    • What I am looking for is not "achieve task X with command Y", what I am looking for is "given task X, how read source code, change small details such as where it takes the default values" – Özgen Eren Apr 9 '18 at 18:17
    • @ÖzgenEren Your clarification sounds vague. The answer would depend on the specific task. You can explore source code by downloading the source code. Add sources to your repository and specify the source code you want to download and explore. I would be glad to assist in giving you specific steps for downloading the specific source code you want to work with. You can use that step for most of your Ubuntu/Linux experience. – L. D. James Apr 9 '18 at 19:02
    • @ÖzgenEren I added a new section to the bottom to answer your new section in your question in reference to how to get and install the source of the applications. – L. D. James Apr 9 '18 at 19:15
    4

    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"
      
    • Thank you, that was the most useful answer so far. if nothing else comes up I will mark this as an answer – Özgen Eren Apr 9 '18 at 18:23
    • @ÖzgenEren It seems like you are changing your question from terminal commands to applications and including Gui Applications. My answer was very specific to your question. As your question changes, my answer (or other answers that you get) will also change. – L. D. James Apr 9 '18 at 18:47
    • @L.D.James I am not changing the question, I thought this answer was on point because he mentions how he approached this particular example, which gives me an idea on how people think in linux and what are the commands they use to figure their way out. I am trying to be as specific as possible by giving an example but as I clarified in the question what I want to know is how to read into a terminal command and customize it (applications are also terminal commands so I thought answer would be the same, if they are different I still take that as an answer although appreciate if both explained) – Özgen Eren Apr 9 '18 at 22:18
    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.

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