-1

I wants to get the names of execute files in some directory in Linux. How can I do it?

I tried to use opendir like this:

dir = opendir(directoryName);

I need to get only the names of the execute files. I programming in C.

thanks :)

user3593549
  • 1
  • 1
  • 3
  • 1
    There are thousands of examples using [`opendir`](http://man7.org/linux/man-pages/man3/opendir.3.html) and related functions if you just search a little. – Some programmer dude Oct 25 '14 at 09:34
  • I search a lot and didn't found anything. Can you help me? – user3593549 Oct 25 '14 at 09:35
  • 2
    One of the first hits in my favorite search engines was [this old SO question](http://stackoverflow.com/questions/3554120/open-directory-using-c). Read that example, read [the manual pages](http://man7.org/linux/man-pages/), and try to make something yourself. ***If*** you have problems with ***your own*** program, then come back here and ask a new question, that shows your complete attempt and a verbose description of your problems with that code. – Some programmer dude Oct 25 '14 at 09:39
  • after opening, use readdir and stat to get file access rights and determine if it has execution bit set. – Jean-Baptiste Yunès Oct 25 '14 at 09:40
  • You probably mean *executable files*. "execute file" is bad English! Please edit your question to improve it. – Basile Starynkevitch Oct 25 '14 at 10:37

1 Answers1

0

You should define what you mean by executable files.

That could be any file with its execute bit (it is the owner, group, or other) set. Then test with access(2) & X_OK and/or use stat(2).

That could also be only ELF executables. See elf(5); then the issue might be to check that a file could indeed be executed, which might be difficult (what about missing library dependencies? or ill-formed ELF files?). Maybe use libelf (and/or libmagic to do the equivalent of file(1) command).

To scan recursively a file tree, use nftw(3); to scan just a directory use opendir(3) & readdir(3) (don't forget the closedir!), then you'll probably need to build the complete file path from each directory entry (perhaps using snprintf(3) or asprintf(3))

See also Advanced Linux Programming

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547