How to check if a regular file is executable or not on Linux

I don’t know which executables in Linux? I want to check all executables in a directory. For this, I came across two teams. first command:

 find . -executable -type f

and another way to do it:

 find . -perm -111 -type f

I want to know which one is the exact way to find all executables in a directory? Why does the first command show JPG files as executable and the second not?

+4
source share
3 answers

Open a terminal and change the directory to the location you want to view.

Then enter: ls -l

The results will look like this:

drwxr-xr-x   5 user  group   170 Oct 29 15:32 A_Folder
-rwxr--r--   1 user  group   187 Feb 27 20:45 A_File

All that does not , starting with d, is a file.

"x", . , cd .

, : user/group/other

, exec, - .

:

  • ( ) , exec (rwx)
  • (- ) (r -)
  • ( ) (r -)
+8

, (user/group/other), . , :

find . -type f -perm +111 -depth 1

, -111 +111. , -depth 1.

, , , . -perm , , -executable NFS. :

(2), , , , .

, . -perm, (-executable, , OS X), , NFS, , .

, , ls -l, . find, :

find . -type f -perm +111 -depth 1 -exec ls -l {} \;
+5

, , . , , . , , , , .

  • GNU userland, find . -executable .
  • - , , test -x "$file". [ -x "$file" ] bash [[ -x $file ]].
  • (, OS X), find . -exec test -x {} \; -print -executable ( ).

find ... -permchecks for actual file permissions that do not necessarily match the actual permissions of the current user. For example, a file may be executed by you, but if it is not executable for everyone, it will not match. Similarly, you can create a template with -perm, which will correspond to a file executed by someone else, but not by you. Perhaps that is why you see a dichotomy between find -perm -111and find -executable.

Consider the following example:

$ whoami
smithm5
$ mkdir foo && cd foo && touch a b c
$ chmod u+x a b && chmod +x c && sudo chown root b && ls -l
total 0
-rwxr--r--    1 smithm5  staff     0 Mar  1 21:03 a # Executable by owner
-rwxr--r--    1 root     staff     0 Mar  1 21:03 b # Executable by owner (who is not me)
-rwxr-xr-x    1 smithm5  staff     0 Mar  1 21:03 c # Executable by everyone
find * -exec test -x {} \; -print # List all files I can execute
a
c
$ find * -perm -111 # List all files owner, group and other can execute
c
$ find * -perm -100 # List all files owner can execute, whichever owner that is
a
b
c
+4
source

Source: https://habr.com/ru/post/1529614/


All Articles