-1

I spend a lot of time downloading things that I am going to execute, without a doubt, after having downloaded them. Out of the 900-ish files in my download folder right now, roughly a third is stuff that I did download and then execute because it's the reason I downloaded them for in the first place.

Every time though, I have to do the little tiring dance of chmod +x'ing them, which is a complete waste of time as I am not downloading them just for the fun of it. Right now I have a dozen small scripts I am going to download in the next half hour and I shudder to the thought of having to chmod them all.

What can I do to make Linux either:

  • ignore the x permission altogether and just do what I tell it to do without questions and without second-guessing me, as the owner, administrator, eternal deity etc etc, of my own, personal computer?
  • automatically mark downloaded files as executable if they match an ELF, shell, python, etc.. file signature?
7
  • Part of the answer depends on how you download the files; e.g. http, wget, git, etc. There are probably a hundred different options. But you haven't indicated that you download these files in the same way... Personally, I would try to script a solution using the Linux install command. install gives you a lot of flexibility; it can copy, create directories, change group & owner, change mode (i.e. make the file executable), etc. You could even make the script a function that's defined in ~/.bashrc.
    – Seamus
    Sep 30 at 23:41
  • 5
    "I shudder to the thought of having to chmod them all" - you do know about wildcards, don't you? At its simplest and most naive, chmod +x *, where the * will match everything in the current directory Oct 1 at 8:22
  • 1
    obviously I know about wildcards - but in my workflow I will download file A, work with it for 5 minutes, push a new commit, download the updated version built by my CI server, download file B, work on it, etc etc - it's not like I download all the files at the same time. Or when I'm doing some research, I will try various different software - again, I try one, it works - good, it doesn't - I continue my research and have to go through the same dance every time. Oct 3 at 14:59
  • If you know it's always an ELF binary, you could invoke the ELF loader directly, like /lib64/ld-linux-x86-64.so.2 /path/to/download --switches --and other args. Similarly, if you know it's a script with a particular interpreter, you could invoke that, like python /path/to/download.py --with --args Nov 13 at 4:11
  • Were you chmoding every file individually? You can use wildcards for that: chmod +x *.sh will mark all files ending with .sh as executable.
    – td211
    Nov 13 at 4:49

5 Answers 5

3

This is perhaps not a good idea for security and future readers should think carefully about whether it's right for them. However you have described a pretty specific use case for this, so I'll offer an option.

Probably the easiest thing you could do is setup a background script to monitor your ~/Downloads directory with inotify-wait (Ubuntu inotify-tools package) and immediately change file permissions on the file as it's been closed by whatever program has written it (IE after it's downloaded):

#!/bin/bash
inotify-wait -m ~/Downloads -e close_write --format %f | while IFS= read -r line ; do
 chmod u+x ~/Downloads/"$file"
done

Of course this will change every file added to your ~/Downloads directory, not just the ELF, shell, python scripts. For that you'd need to add in additional rules to determine the file name and be selective. For example you could use the file to identify the files you are interested in.

1
  • Interesting to note the OP did not say HOW they were downloading files (which makes me think they probably should not attempt something like this).
    – symcbean
    Nov 13 at 11:38
2

I agree with Philip Couling and would go a step further: downloading files and then executing them without doing any inspection, validation or verification is somewhat reckless from a security perspective.  But, if you trust the source without reservation, and you’re just working on your own personal computer, it’s your decision.

  1. You ask what you can do to make Linux ignore the x permission altogether and just do what you tell it to do.  Well, if the file is a shell script, you can type

    sh   filename arg1 arg2 arg3
    or
    bash filename arg1 arg2 arg3
    If it’s a python script, you can type
    python  filename arg1 arg2 arg3
    or
    python2 filename arg1 arg2 arg3
    or
    python3 filename arg1 arg2 arg3
    as appropriate.  Notes:

    • These will fail if the script tries to call itself recursively in the normal way, i.e., assuming that the x bit is set.
    • I don’t know of any equivalent command for binary executable files, like ELF files.

    I really advise against downloading files and then executing them without even knowing what type they are.

  2. You shudder at the thought of having to chmod a dozen scripts.  I don’t understand.  Is typing the nine characters chmod +x  such a hardship?

    Do you know about aliases?  In Bash (i.e., in your ~/.bashrc), you can say

    alias '+x'='chmod +x'
    

    Then you can use

    +x filename(s)
    to chmod your files (less typing).  And, as roaima indicated, you can use wildcards (globs) with the chmod command, so +x *.sh works.

    To save one more character, use x as the alias name instead of +x.

  3. A combination of the above ideas: create a shell function

    run()
    {
            chmod +x "$1"  &&  "$@"
    }
    

    Then you can type

    run filename arg1 arg2 arg3
    to chmod your file and execute it in one command.  (You might want to do type run to check whether there is a command called run on your computer, and, if there is, use a different name.)

2
  • "I don’t know of any equivalent command for binary executable files, like ELF files.": ld.so (or /lib*/ld-linux-*.so*) can execute dynamic ELF binaries that don't have executable mode set (that's their default "interpreter"). Now for static linked binaries... no idea.
    – A.B
    Oct 2 at 14:25
  • > You shudder at the thought of having to chmod a dozen scripts. I don’t understand. Is typing the nine characters c h m o d   + x  such a hardship? Yes, it's honestly user-hostile when you have to do it pretty much every day and a complete waste of time. I'm already trusting the files the moment I click "download" on a website - for instance a lot of them are my own software that I build on github's CI server which I download from the GH release gui (and thus does not preserve permissions). Oct 3 at 14:57
0

I think the best thing to do is to keep things simple. If your downloading scripts to just test them out once or twice, why make them executable at all.

Just do this, shell cmd. Just invoke the shell the script uses. In this case a bash script.

bash DownloadedScript.sh

Leaving a lot of executable scripts that you're not using that much in your Downloads is not a good idea. Leaving them as not executable is better for system security.

2
  • 1
    What about if it's not a Bash script? In particular, if changing the interpreter is part of what's being tested. Perhaps it's a Perl script, or a Java JAR, or an ELF binary. Nov 13 at 4:07
  • @MartinKealey I know this trick works with python. For example python script.py. I have not tried it with Perl, java, or any other programming language. If there is a way to invoke a shell for that programming language, then this trick might work for other languages.
    – Cyberninja
    Nov 13 at 13:57
0

I run as root in a closed system, so security is not an issue.

I made a .Desktop file in KDE service menu labled "make executable" and right click (context menu for right handed mouse) to change perms.

======================================== [Desktop Entry] Type=Service ServiceTypes=KonqPopupMenu/Plugin MimeType=application/x-shellscript;application/x-ruby;text/x-python;application/x-executable Icon=application-x-shellscript X-KDE-Priority=TopLevel X-KDE-StartupNotify=false X-KDE-Submenu=Make executable - Copy to /usr/bin

Actions=me;cp;

[Desktop Action me] Name=Make executable Icon=system-run Exec=chmod +x "%f"

[Desktop Action cp] Name=Copy to /usr/bin Icon=password-copy Exec=kdesu -d --noignorebutton -c 'sudo cp "%f" /usr/bin/'

It also can copy to /usr/bin if you want.

Easy as pie.

Sorry, I was unable to understand the "code" aspect of posting. No offence intended. Will try to get it right.

Vic

0

For your particular use-case, where you are cyclically downloading from the same URL and running what are actually different versions, perhaps something like this:

while
    printf 'Press enter to download and run:'
    read -r
do
    curl "$URL" >| tmpexec &&
    chmod +x tmpexec &&
    ./tmpexec
done

(I hope this also side-steps the security concerns many would have about downloading from a random public site, which are not relevant here.)

1
  • I'm downloading from URLs but not necessarily the same (different versions, commits, software, etc). And obviously I want this to work from my browser's UI - I want to make my life simpler, not harder Nov 14 at 13:03

You must log in to answer this question.

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