LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Store into a file the list of executable files of a system directory (https://www.linuxquestions.org/questions/linux-newbie-8/store-into-a-file-the-list-of-executable-files-of-a-system-directory-628167/)

jianelisj 03-15-2008 12:35 AM

Store into a file the list of executable files of a system directory
 
How can i store into a file the list of executable files of a system directory?

My solution is:

if test -f $1 and test -x $1 then
echo $1
else
echo "message"
fi

int a file e.g. test

it runs correctly if i write test filename (only for 1 file),
but i need a solution for all the files of a system directory.

How can i recognize a system directory?

i would appreciate any help

matthewg42 03-15-2008 01:38 AM

When you post code, please do so using [code] tags. This inproves readability to preserving whitespace and using a fixed width font.

You can iterate over files like this:
Code:

for thisfile in *; do
    if test -f "$thisfile" -a -x "$thisfile"; then
        echo "executable file:            $thisfile"
    else
        echo "not executable or not file: $thisfile"
    fi
done

You can modify the pattern * to examine only some sub-set of files in the directory, e.g.
Code:

for thisfile in *.txt; do
  ...

...to examine only files with a name ending in ".txt".

konsolebox 03-15-2008 03:41 AM

you can also use find for that .. do 'man find' and take a look at the examples for 'find . -perm'

here's an example statement to list the executable files in a directory:
Code:

find $dir -type f -perm /111 -maxdepth 1

Tinkster 03-17-2008 12:26 PM

Start reading your training material, do your own homework.


All times are GMT -5. The time now is 09:35 PM.