Count the Number of Executable Files in Bash

BASH - counting the number of executable files

Just remove the if structure and the echo's

#!/bin/bash
To="home/magie/d2"
cd "$To"
find . -type f -perm 755

find . -type f -perm 755 | wc -l

Counting number of executable files in every variable inside PATH

Have you considered using the find command? See this link. On GNU-based versions of find, something like the following should replace your ls call and while loop.

find $directory -type f -executable -print | wc -l

If you are worried about traversing subdirectories, you can use the maxdepth flag, i.e.

find $directory -maxdepth 1 -type f -executable -print | wc -l

For BSD-versions, again, see the link.

bash - using recursion - counting the executable files in dir

If you really want to use recursion (which is a bad idea in Bash): first, don't call your script recursively. Instead, call a function recursively. This will be more efficient (no forking overhead). Trying to fix your syntax:

#!/bin/bash

shopt -s nullglob

count_x_files() {
# Counts number of executable (by user, regular and non-hidden) files
# in directory given in first argument
# Return value in variable count_x_files_ret
# This is a recursive function and will fail miserably if there are
# too deeply nested directories
count_x_files_ret=0
local file counter=0
for file in "$1"/*; do
if [[ -d $file ]]; then
count_x_files "$file"
((counter+=count_x_files_ret))
elif [[ -f $file ]] && [[ -x $file ]]; then
((++counter))
fi
done
count_x_files_ret=$counter
}

count_x_files "$1"
echo "$count_x_files_ret"

How to find the executable files in the current directory and find out their extensions?

#!/bin/bash                                                                                                                                                    

for file in `find /bin` ; do
if [ -x $file ] ; then
file $file
fi
done

and even better to do

find /bin -type f -perm +111 -print0 | xargs -0 file

How to count number of times a file was executed on linux

I don't know exactly how to watch a file for execution, but you can construct something with inotify watching how many times it is opened:

You could have a script like that:

#! /bin/bash

EXEC_CNT=0
FILE_TO_WATCH=/path/to/your/file
while inotifywait -e open "$FILE_TO_WATCH"
do
((EXEC_CNT++))
echo "$FILE_TO_WATCH opened $EXEC_CNT times"
# Or to store in a file:
# echo "$FILE_TO_WATCH opened $EXEC_CNT times" >> "$FILE_TO_WATCH.log"
done

In case of a network share, this script must be runned on the computer that share its file system.

counting only executable file in linux not all the file which have executable permission

You can always use some find command with options

This would print only executables files in the /tmp location

find /tmp -perm +111 -type f | wc -l</ br>

For Directories

find /tmp -perm +111 -type d | wc -l</ br>


Related Topics



Leave a reply



Submit