Find exe files in linux

How can I find only the executable files under a certain directory in Linux?

For anyone wanting to do this on a Mac (tested on OS X 10.9.5): ls -l | egrep ‘^[^d]..x..x..x.*$’ The above will list all executables (for all/user and group) in the current directory. Note: The -executable option does not work on a Mac hence the above workaround.

@techfoobar: The question is ambiguous: Does it mean files that contain executable code, or does it mean files that have executable permission? But even if we assume that executable permission is what is wanted (as the majority of the responses seem to), the question doesn’t say world-executable. Your solution will find files (and also fifos, sockets, symlinks, etc.) that have world execute permission, but not 750 ( -rwxr-x— ), which is still executable to some users.

8 Answers 8

Checking for executable files can be done with -perm (not recommended) or -executable (recommended, as it takes ACL into account). To use the -executable option:

If you want to find only executable files and not searchable directories, combine with -type f :

find DIR -executable -type f 

a shebang doesn’t mean they’re executable. it tells us only which interpreter to use. and by linux definition “executable files” are files with the executable (x) bit set

What version of find supports that type for -type? man find lists b, c, d, p, f, l, s and D on my system.

If you have an old version of find (probably before 4.3.8) which lacks -executable use find . -perm /u=x,g=x,o=x.

Use the find’s -perm option. This will find files in the current directory that are either executable by their owner, by group members or by others:

I just found another option that is present at least in GNU find 4.4.0:

This should work even better because ACLs are also considered.

This only works on a newer version of find. The one that comes by default with CentOS gives the error find: invalid mode /u=x,g=x,o=x’`

I know the question specifically mentions Linux, but since it’s the first result on Google, I just wanted to add the answer I was looking for (for example if you are — like me at the moment — forced by your employer to use a non GNU/Linux system).

Tested on macOS 10.12.5

from the man page -perm +mode This is no longer supported (and has been deprecated since 2005). Use -perm /mode instead.

I have another approach, in case what you really want is just to do something with executable files—and not necessarily to actually force find to filter itself:

for i in `find -type f`; do [ -x $i ] && echo "$i is executable"; done 

I prefer this because it doesn’t rely on -executable which is platform specific; and it doesn’t rely on -perm which is a bit arcane, a bit platform specific, and as written above requires the file to be executable for everyone (not just you).

The -type f is important because in *nix directories have to be executable to be traversable, and the more of the query is in the find command, the more memory efficient your command will be.

Читайте также:  Мониторинг сетевых устройств linux

Anyhow, just offering another approach, since *nix is the land of a billion approaches.

(0) Which do you prefer, arcane and correct or intuitive and flawed? I prefer correct. (1) innaM’s answer, featuring find -perm , finds files that have any execute permission bit set. (2) By contrast, this answer finds only files for which the current user has execute permission. Granted, that might be what the OP wants, but it’s unclear. … (Cont’d)

(Cont’d) … (3) For clarity, you might want to change `…` to $(…) — see this, this, and this. (4) But don’t do for i in $(find …); do … ; it fails on filenames that contain space(s). Instead, do find … -exec … . (5) And, when you do work with shell variables, always quote them (in double quotes) unless you have a good reason not to, and you’re sure you know what you’re doing.

@scott OK, I stand corrected 🙂 I read the -perm argument as requiring all three, not one of them. Also, thank you for the input on protecting shell arguments, that’s all stuff I wasn’t aware of.

@MarkMcKenna you have a typo in there: for i in find . -type f ; do [ -x $i ] && echo «$i is executable»; done ; you are missing the

part, which I use a dot(.)

A file marked executable need not be a executable or loadable file or object.

find ./ -type f -name "*" -not -name "*.o" -exec sh -c ' case "$(head -n 1 "$1")" in ?ELF*) exit 0;; MZ*) exit 0;; #!*/ocamlrun*)exit0;; esac exit 1 ' sh <> \; -print 

@DerMike, It is one of the ways to find executable in current directory, including .so files, even if a file is not marked executable it can discover.

@nonchip I strongly disagree. @OP did not ask what files were set to executable/+x, but what files were actually executable. The definition of what that means is left to the reader, but I would not consider portrait.png executable, even with a+x , and I would consider /usr/bin/grep an executable, even if it was accidentally changed to miss the x flag.

The downvotes you’ve gotten so far reflect the belief that you’re answering the wrong question with a skimpy explanation. I wonder why you consider .so files to be executable but not .o files, and why you consider OCaml scripts to be executable but not shell scripts (or Awk, Perl, Python, etc.). Also, your answer has a typo. But THIS downvote is for the snarky, abusive comment.

As a fan of the one liner.

find /usr/bin -executable -type f -print0 | xargs file | grep ASCII 

Using ‘xargs’ to take the output from the find command (using print0 to ensure filenames with spaces are handled correctly). We now have a list of files that are executable and we provide them, one by one, as the parameter for the ‘file’ command. Then grep for the term ASCII to ignore binaries. Please substitute -executable in find command with what style you prefer (see earlier answers) or what works on your ‘NIX OS

I required the above to find files with eval in scripts owned by root, so created the following to help find priv escalation weaknesses where root user runs scripts with unsafe parameters.

echo -n "+ Identifying script files owned by root that execute and have an eval in them. " find / -not \( -path /proc -prune \) -type f -executable -user root -exec grep -l eval <> \; -exec file <> \; | grep ASCII| cut -d ':' -f1 > $outputDir"/root_owned_scripts_with_eval.out" 2>/dev/null & 

Источник

Читайте также:  Linux getting cpu info

Find All Files with Extension in Linux

Often, we find ourselves stuck when we have to find all files with the same or different extensions. This has most likely happened to various Linux users while using the terminal. It is one thing to search for a single file type or file, but what will you do when you want to find out all files simultaneously? This article comes to the rescue for our readers who have such a dilemma.

We can use various Linux utilities for finding or locating files on a file system, but searching all files or filenames with the same or different extensions can be difficult and require specific patterns or expressions. In the upcoming section of the article, we will understand the working, syntax, and execution of these utilities.

Find command

One of the most powerful file searching tools in the Linux system is the “find command.” It searches the entire directory for files and folders to get matched with the user’s expression and performs actions on these files. File permission, file size, type are some other factors based on finding files on Linux. Find command also be combined with other utilities such as sed or grep. Now, lets’ head towards the practical implication of find command.

Find command syntax:

Finding all files with a single extension:

To find all files with a file extension, write out its path to find a command with the options and expression specifying the extension. In the below-given example, we will find all files with the “.txt” extension.

“.” in this command denotes that this tool will find all the “.txt” files in the current directory.

Find “.exe” files in the same find command by adding the extension as “*exe.”

Configuration files are also an essential part of any file system that can be used for multiple purposes. Write out this command for searching configuration files in the current directory.




Finding files with multiple extension:

You can also add more than extension in your find command so that you can find several extension files easily and quickly.

The execution of below given command will retrieve files with extension “.sh” and “.txt”

Locate command

The locate command is a faster and better tool as compared with “find.” When a file is initiated, instead of searching it in the file system, locate utilize the database for the searching requirement. This database stores parts and bits of the information related to files and their addresses on your system.

locate command syntax:

Finding a file with a specific extension, such as “.conf,” which is considered in our case, adds the directory path where the process of searching files will occur.

Find configuration files in the present working directory by utilizing the below-given command.

Similarly, you can follow the syntax of locate command for finding all files with any specific extension such as “.txt.”

Conclusion:

This post covers two powerful yet simple utilities for you to find all files with the same or different extensions. We have provided you the fundamental concepts regarding the “find” and “locate” command and shown you how to utilize these two Linux command-line tools to find all files with several extensions.

Читайте также:  Unzipping windows files on linux

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

Источник

Find executable files recursively

I have got the directory called Test and a few directories inside it. Both Test and the directories inside it have executable files. I’d like to print them with ls . I’d use this command.

ls -l `find Test/ -perm /u=x,g=x,o=x -type f` 
find Test/ -executable -type f -exec ls -l <> \; 

try find . -exec -ls -l <> \+ instead of \; — doing that will cause only one ls command to be generated with multiple filename args, similar to how xargs works. read the find man page and search for -exec for details.

4 Answers 4

Not really, you can integrate the ls command with find,

find Test/ -type f -perm /u=x,g=x,o=x -exec ls -l <> \;

Actually -executable is not an equivalent of -perm /u=x,g=x,o=x . You might have files that is executable only by the group or others, which will not be displayed.

So, depends on your purpose, if you want files executable only by you, that’s okay to use -executable .

There is no need to use -exec , as find comes with a -ls flag.

$ find Test/ -perm /u=x,g=x,o=x -type f -ls 

-ls True; list current file in ls -dils format on standard output. The block counts are of 1K blocks, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used. See the UNUSUAL FILENAMES section for information about how unusual characters in filenames are handled.

Источник

Find command: Searching for executable files

What type of parameter/flag can I use with the unix find command so that I search executables? (if this question is better suited for another stackexchange forum, I welcome you telling me so) p.s. If you know of one, I’ve been looking for a detailed (non-beginner) tutorial/screencast about grep and/or find.

3 Answers 3

Portably, the following command looks for regular files that are executable by their owner:

With GNU find ≥4.3, you can use -executable instead of -perm -700 to look for files that are executable by you.

Rather unsurprisingly, -executable . From the man page:

Matches files which are executable and directories which are searchable (in a file name resolution sense). This takes into account access control lists and other permissions artefacts which the -perm test ignores

find /path -perm /u=x,g=x,o=x -type f 

It looks through /path , finds user, global and other executables that are regular files.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector