6

I have a number of files showing up green when I run ls. I understand these are executables, and I understand that one can make a file executable with chmod. But they are .csv and .pdf files. I don't understand how one could 'execute' a comma-separated text file or a PDF. So:

  • How can they actually be 'executable'?
  • And how would I execute them?
  • And what would happen when I did?
5
  • 7
    Most probably happened while you copied those files from a FAT32 or NTFS, normal thing. Commented May 29, 2020 at 14:06
  • Interesting. I think 'why' warrants it's own question. Has it already been asked? Commented May 29, 2020 at 14:10
  • 7
    Permission to execute does not mean that the file is an "executable". Most likely executing the file will give an error. See also and also.
    – pLumo
    Commented May 29, 2020 at 14:20
  • @LinuxSecurityFreak Yup. Happens as a matter of course when I copy photos from an SD card. Commented May 30, 2020 at 5:23
  • Incidentally, directories normally need to have the executable bit set. See unix.stackexchange.com/q/21251/88378
    – PM 2Ring
    Commented May 30, 2020 at 16:17

3 Answers 3

9

This is just a question of permissions. If a file has execute permissions, that just means users are allowed to execute it. Whether they will be successful is another matter. In order for a file to be executed, the user executing it must have the right to do so and the file needs to be a valid executable. The permissions shown by ls only affect the first part, permission, and have no bearing on the rest.

For instance:

$ cat file.csv
a,silly,file
$ chmod a+x file.csv
$ ls -l file.csv
-rwxr-xr-x 1 terdon terdon 13 May 29 15:22 file.csv

This file now has execute permissions (see the 3 x in the permissions string -rwxr-xr-x). But if I try to execute it, I will get an error:

$ ./file.csv 
./file.csv: line 1: a,silly,file: command not found

That is because the shell is trying to execute the file as a shell script, and there are no valid shell commands in it, so it fails.

4
  • In your example, you shell succeeded at launching a sh interpreter to interpret that file, and that sh interpreter succeeded at interpreting that file (interpreting all the lines in that file as shs do interpret and run code as they read it) as it was in valid sh syntax, but it failed to find the a,silly,file command. As that was the only line in that file, the exit status was 127, so a failure exit status. Commented May 29, 2020 at 16:01
  • 2
    @StéphaneChazelas yes, exactly. Am I saying something that would contradict that? The shell is trying to execute the "commands" found in the "script", as it should, and fails since there is no valid command.
    – terdon
    Commented May 29, 2020 at 16:13
  • 3
    terdon, I think @StéphaneChazelas point is that the csv file was actually executed. The fact that the csv file caused an error during its execution does not negate that the csv was executed. This seems a matter of semantics as you could also argue that it is only the sh interpreter that is executing. Suffice it to say, your primary point of the file permission is separate from it being a file able to be executed is correct. Coming up with an example that no-one on the internet will respond with "Well actually..." is difficult when the definition of what is executing is so blurry. Commented May 30, 2020 at 2:48
  • run my executable CSV. The first line is #!/bin/bash. Commented May 30, 2020 at 4:39
7

The green in your ls display means that the files have execute permissions, which doesn't necessarily mean that they can be executed in any way.

If you accidentally set the execute permission, or if it was set due to an import from a file system without support for the basic linux permission model, you can remove the execute bit with

chmod a-x -- "$filename"

and that file will no longer show as "executable".


Note that other colors in the file listing may be set based on file extension; but in this case it is likely just the permission bits.

2

The accepted answer perpetuates myths and a poor understanding of how "executing" a file happens in Unix.

./cmd in the shell does more than just trying to execute ./cmd: if executing ./cmd fails with ENOEXEC (as it does for any executable file which is not a valid binary or doesn't start with a shebang, including a valid shell script) then the shell will either a. read and interpret itself the file as a shell script or b. execute /bin/sh or another shell and pass it ./cmd as its first argument.

The shebang mechanism (#! /path/to/interpreter), though implemented by most Unix systems, is not standard. Also, many systems like Linux are also able to "execute" files starting with other "magic" sequences (or even based on their file extension, MSDOS-like).

For instance, you can have "executable" pdf files on Linux via binfmt_misc:

$ chmod +x 2503.pdf
$ ./2503.pdf
./2503.pdf: line 1: fg: no job control
./2503.pdf: line 1051: warning: here-document at line 2 delimited by end-of-file (wanted `/Linearized')
[... a lot of crazy errors in the same vein ...]

$ su -c 'echo :pdf-zathura:M::%PDF-1.::/usr/bin/zathura: > /proc/sys/fs/binfmt_misc/register'
$ ./2503.pdf
[start zathura and display the pdf file]

You must log in to answer this question.

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