Check if file is executable linux

How to check if a file is executable in Bash [duplicate]

I am trying to build a condition that check if file has execute access bit set. I can’t use grep and find. I tried something with checking the «x» letter in the ls -l command but this is wrong in many cases.

for val in `ls $1` do if [[ "`ls -l $1/$val`" -eq *w* ]] then rm $1/$val fi done 

2 Answers 2

There is no need to parse the output of ls to see if a file is executable. Shell provides the built-in -x check for that. Using -x feature, your loop could be re-written as:

for file in "$1"/*; do [[ -x "$file" ]] && rm -- "$file" done 

«$1″/* , you mean (to handle spaces in the directory name). And rm — «$file» , if we want to handle the case where $1 is a directory name that starts with a dash.

if [ -x "$file" ]; then # do something fi 

You can get many more options of file testing using man :

~]# man test . -x FILE FILE exists and execute (or search) permission is granted 
 ~]# find $1 -type f | while IFS='' read -r -d '' p; do if [ -x "$p" ]; then echo "removing $p"; rm "$p"; fi; done 

find command gets all the files (including . ) in the directory given by $1 . while reads each of these output, if then checks individual files for executable permission with -x .

EDIT

After some comments, here is a swifter example:

find "$1" -type f -executable -exec rm -- <> \; 

Источник

How to check whether a file is executable or not? [duplicate]

I want to check the file is executable or not through the command prompt so that I cannot open the file whether it is executable or not.

I don’t think this is a duplicate. First, the question is a different one. But most important, ls -l won’t help if you want to automate this. Also you need to know if you are the owner, belong to the group or to everyone else. And then it gets really tricky.

2 Answers 2

[ -r file ] tests if a file is readable.
[ -w file ] tests if a file is writeable.
[ -x file ] tests if a file is executable.

if [ -x file ]; then ./file else echo "File is not executable" fi 

Try it with a simple example:

#!/bin/bash touch testfile test -x testfile && echo true || echo false # --> false chmod +x testfile test -x testfile && echo true || echo false # --> true rm testfile 

The recommended tool to determine a file type is file .

Читайте также:  Exit error codes linux

Example of an executable file.

$ file c.sh a.out c.sh: Bourne-Again shell script, ASCII text executable a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=a741679fcc57d5dafa76025d943090fdf614b7e2, not stripped 

doesnt give me the needed information: touch test; file test; —> test:empty chmod +x test; file test —> test: empty

You are wrong. From the man pages: chmod — change file mode bits . Of course, if you use chmod +x you can make an executable. If you test it then you make a circular reasoning. But without changing the file you can determine the file type with the file command, or as writen in the man pages: file — determine file type .

Of course your circular reasoning works perfectly if I use your bash script and line 5 will complete the circular reasoning. If you don’t like to read man pages, I just copied the head titles here for you, but you can get fancy information about determining a file type here: lifewire.com/file-linux-command-unix-command-4097142

Источник

Shell Script to check if file is of type Unix Executable File

I am writing a Shell Script where user will pass the path to JAVA_HOME as a parameter. I wanted to check whether the JAVA_HOME path is valid or not. So how can I check whether the input path passed is a exe file ( Unix Executable File ) or not ? Suppose if users passed /usr/bin/java — which is valid, if user passes like /usr/bin/test.txt ( some text file )- which is not valid. So how can I check whether the file is of type Unix Executable File ? I tried with -x , guess that is not the rite check , since it returns true for /usr/bin also .

3 Answers 3

Easiest way is to use the file command.

$ file /usr/bin /usr/bin: directory $ file /usr/bin/java /usr/bin/java: Mach-O universal binary with 2 architectures /usr/bin/java (for architecture x86_64): Mach-O 64-bit executable x86_64 /usr/bin/java (for architecture i386): Mach-O executable i386 $ 

You can just grep the output of file for a suitable string, e.g. «executable».

Читайте также:  Alpine linux ssh root

The man page for test is quite clear that just -x isn’t going to do it

-x file True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched.

But if you combine this with -f and -r

-f file True if file exists and is a regular file.

-r file True if file exists and is readable.

you will know if the file has the requisite properties to be an executable.

There remains the possibility that a user could have set these properties on a non-executable, but that would clearly be a user error.

Man page excerpt here taken from the Mac OS 10.5 versions. You may seem something slightly different on other systems (Linux in particular).

Источник

Check if a file is executable

I am wondering what’s the easiest way to check if a program is executable with bash, without executing it ? It should at least check whether the file has execute rights, and is of the same architecture (for example, not a windows executable or another unsupported architecture, not 64 bits if the system is 32 bits, . ) as the current system.

Neither ls -la nor stat give information about the supported architecture, which is actually the part of the question I’m most interested in. I faced an error because a executable hadn’t been compiled for my architecture, and I would like to create a script to avoid that in the future.

@FatalError How could I parse the file output to check whether the executable is compliant with my architecture ?

7 Answers 7

Take a look at the various test operators (this is for the test command itself, but the built-in BASH and TCSH tests are more or less the same).

You’ll notice that -x FILE says FILE exists and execute (or search) permission is granted.

BASH, Bourne, Ksh, Zsh Script

if [[ -x "$file" ]] then echo "File '$file' is executable" else echo "File '$file' is not executable or found" fi 
if ( -x "$file" ) then echo "File '$file' is executable" else echo "File '$file' is not executable or found" endif 

To determine the type of file it is, try the file command. You can parse the output to see exactly what type of file it is. Word ‘o Warning: Sometimes file will return more than one line. Here’s what happens on my Mac:

$ file /bin/ls /bin/ls: Mach-O universal binary with 2 architectures /bin/ls (for architecture x86_64): Mach-O 64-bit executable x86_64 /bin/ls (for architecture i386): Mach-O executable i386 

The file command returns different output depending upon the OS. However, the word executable will be in executable programs, and usually the architecture will appear too.

Читайте также:  Linux восстановить файловую систему флешки

Compare the above to what I get on my Linux box:

$ file /bin/ls /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped 
$ file /bin/ls /bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped 

In all three, you’ll see the word executable and the architecture ( x86-64 , i386 , or SPARC with 32-bit ).

Addendum

Thank you very much, that seems the way to go. Before I mark this as my answer, can you please guide me as to what kind of script shell check I would have to perform (ie, what kind of parsing) on ‘file’ in order to check whether I can execute a program ? If such a test is too difficult to make on a general basis, I would at least like to check whether it’s a linux executable or osX (Mach-O)

Off the top of my head, you could do something like this in BASH:

if [ -x "$file" ] && file "$file" | grep -q "Mach-O" then echo "This is an executable Mac file" elif [ -x "$file" ] && file "$file" | grep -q "GNU/Linux" then echo "This is an executable Linux File" elif [ -x "$file" ] && file "$file" | grep q "shell script" then echo "This is an executable Shell Script" elif [ -x "$file" ] then echo "This file is merely marked executable, but what type is a mystery" else echo "This file isn't even marked as being executable" fi 

Basically, I’m running the test, then if that is successful, I do a grep on the output of the file command. The grep -q means don’t print any output, but use the exit code of grep to see if I found the string. If your system doesn’t take grep -q , you can try grep «regex» > /dev/null 2>&1 .

Again, the output of the file command may vary from system to system, so you’ll have to verify that these will work on your system. Also, I’m checking the executable bit. If a file is a binary executable, but the executable bit isn’t on, I’ll say it’s not executable. This may not be what you want.

Источник

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