0

I have a lot of executables (Win32/Dos) inside one of the folders in my Ubuntu machine. I want to find them all and print out the paths. I tried:

for folder in folders:

    print("Searching: " + str(folder))

    os.chdir(folder)
    for file in glob.glob("**/*.exe", recursive=True):
        print(file)

Sadly, this returns no output. What gives?

The folders are under Documents directory. Running as root is not an issue.

The question is not a duplicate of "how to find all windows executables recursively" rather "why isn't a perfectly correct code that works under Windows misbehaves in linux".

My hunch is the extension .exe isn't really reciprocated in Linux - maybe I should be looking at file header (or similar) to determine file type.

Thanks!

Jishan
  • 1,654
  • 4
  • 28
  • 62
  • 1
    What is `folders`? Possibly highly relevant, because did you not even get to see that "Searching.." string? – Jongware Feb 09 '20 at 11:43
  • @usr2564301 Yes - got that! Folder are under the `Documents` directory. Running as root is not a problem. – Jishan Feb 09 '20 at 11:44
  • 1
    -- I *think* your `os.chdir` is messing things up (see [this answer](https://stackoverflow.com/a/14798263)) but I couldn't get it to work and the first answer in the proposed duplicate worked immediately ... – Jongware Feb 09 '20 at 11:58
  • @usr2564301 The other solution you posted also doesn't work. – Jishan Feb 09 '20 at 11:59
  • 3
    Your hunch doesn't make sense to me. Linux does not thread .exe files any different than other files. However, be aware that Linux is case-sensitive (so your search won't find any `*.EXE`). Try standard debugging methods to find the problem. For example, print the result of `os.getcwd()` after `os.chdir()` to see if you're really checking the intended directories. Try to (temporarily) search for `'**/*'` instead of `'**/*.exe'` and see what it returns. – wovano Feb 09 '20 at 13:22

1 Answers1

0

You can check it with the file command:

# os.system for brevity use subprocess instead.
file_type = os.system("file --brief " + os.path.join(folder, current_file))

# check the file name now...

or read the first line in byte mode to grab some useful info like magic number in the header.

wovano
  • 4,543
  • 5
  • 22
  • 49