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.

How can I execute a file without giving myself execute permissions (with chmod u+x) for it?

If I try and tick the 'Allow executing file as program' checkmark, the checkmark is immediately removed.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Check file ownership

Please do this before anything further (unless you are sure you own the file).

Check and make sure that you own the file which you are trying to execute with one of the following methods.

Graphical

  1. Right click the file.
  2. Click 'Properties'.
  3. Click the 'Permissions' tab.
  4. Ensure that the 'Owner' field says your username. If it doesn't see "Change file ownership" below

Command Line

  1. Execute this command in a terminal

    [ ! -O "/path/to/file" ] && echo "You don't own the file"
    
  2. If it prints "You don't own the file", see "Change file ownership" below.

Change file ownership

Execute this command in a terminal

sudo chown $USER:$(id -gn $USER) "/path/to/file"

Executable Files

An answer I found from a comment by Lekensteyn on an answer for a question about chmod on NTFS partitions which I think deserves it's own question and answer, full credit to Lekensteyn.

Use this command for executable files (substituting /path/to/executable with the correct path):

  • 64 bit executable files:

    /lib64/ld-linux-x86-64.so.2 /path/to/executable
    
  • 32 bit executable files:

    /lib/ld-linux.so.2 /path/to/executable
    

If the above doesn't work (or raises file not found errors), try using this before the above command

cd "$(dirname /path/to/executable)"

All the above commands will not work for text based scripts (Bash, Python, Perl, etc.), see below.

Check if a program is 64 or 32 bit

Use this command to find out if a executable is 32 (x86) or 64 (x86-64) bit

objdump -f "$file" | grep '^architecture' | cut -d, -f1 | sed 's/architecture: //'

If it says i386:x86-64, then it's 64 bit. If it says i386 only, then it's 32 bit.


Scripts

For text based scripts (Bash, Python, Perl, etc.), you should use the command specified in the first #! line in the file.

For example, if the first line of the file is

#!/usr/bin/env python3

then run these commands in a terminal (substituting /path/to/file with the correct path)

cd "$(dirname /path/to/file)"       # Not strictly necessary, see section below

# Replace '/usr/bin/env python3' with the first line without the front #!
/usr/bin/env python3 /path/to/file  # Use './file' if you want

Java .jar files

For Java executable jar's, you can simply use these commands (substituting /path/to/jar with the correct path):

cd "$(dirname /path/to/jar)"   # Not strictly necessary, see section below
java -jar /path/to/jar

When you don't need cd "$(dirname /path/to/file)"

These are possible circumstances where you wont need to use cd "$(dirname /path/to/file)" before running the program with any method: If at least one is true, you wont need cd first.

  • The program doesn't perform any file operations itself (example: it only uses apt-get)
  • The program uses cd (or equivalent) to change to an absolute path before doing any file operations (example: cd "$(dirname "$0")")
  • The program doesn't reference any relative paths (paths starting with ./ or starting with no slash)

If unsure, add cd "$(dirname "$0")" (or equivalent) to the top of the script (if applicable) or use cd "$(dirname /path/to/file)" anyway.

share|improve this answer
    
Anyone who wants to script this and make it to cover all the possibilities (if you use MS filesystems a lot) is welcome to. I might do it myself, but don't count on it. (+1 this comment if you want this, enough support and I might do it) –  minerz029 Oct 15 '13 at 6:54
add comment

If it's a shell script, you can "source" it from another shell script, i.e.:

#!/bin/bash
# This script runs another script indirectly, by sucking in its contents and evaluating inline:

source /path/to/script/which/lost/its/exec/flag "$@"

The "$@" appends the command line parameters, if needed.

Having said that, this is probably not the best solution for the underlying problem, but we don't know enough about that problem to offer alternatives.

share|improve this answer
    
I don't see how this is particularly different from bash script $@ –  minerz029 Oct 15 '13 at 6:04
    
It's not fundamentally different in operation. Most likely I don't understand what problem you're trying to solve -- I assumed that you wanted a point-and-click way to execute a script that is (for whatever reason) not executable. "Make one that's executable and have it source the other" is one solution to that problem. If that's the wrong problem... well :) –  Stabledog Oct 15 '13 at 16:20
add comment

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.