LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find command (http://www.linuxquestions.org/questions/linux-newbie-8/find-command-924625/)

congos 01-19-2012 10:09 AM

find command
 
how can i find the number of executable files in a folder/directory and display them on the terminal
this is my code below i do not know why its not working

Code:

#!/bin/bash
To=/home/elgin19/d2
cd "To"
find .type f -name "*.sh" | ls -l | wc -l


crabboy 01-19-2012 10:21 AM

You are close:
Code:

START_DIR=/home/elgin19/d2
find $START_DIR -name '*.sh' -type f | wc -l

that will find all the .sh files in your starting directory and recursively search down.

jhwilliams 01-19-2012 10:22 AM

Code:

find . -type f -executable | wc -l

Snark1994 01-19-2012 10:25 AM

Well, a couple of things. If you've set "To=" then your "cd" line should be

Code:

cd $To
Secondly, there should be a space between the '.' and the 'type', and it should be '-type' not 'type' (you could even just write

Code:

find $To -type f -name "*.sh" | ...
)

Also, you're not finding executable files, you're finding files with a .sh extension - but as long as you're okay with that, then that's alright.

However, if you're just listing, you should use 'ls' on its own:

Code:

ls -l ${To}/*.sh | wc -l

David the H. 01-19-2012 10:26 AM

Here are a couple of links about using find. You may find them useful:

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

tronayne 01-19-2012 10:26 AM

You probably want to use the -perm option.

If you look at the manual page for find, the EXAMPLES section has a number of sample uses of -perm (you can search in the manual page by entering /perm to find the first, then n to find the next and so on).

If you use the more-or-less standard default (022), an executable file will be mode 755, so you might use
Code:

find . -type f -perm 755
to get a list of all executable files; see the manual page for others.

Hope this helps some.

congos 01-19-2012 12:05 PM

its working but how do i find their total,im trying to use
Code:

wc -l
but its not working

crabboy 01-19-2012 12:27 PM

Could you explain what you see?

wc -l should count the number of lines that were reported by the find command. If find found 4 .sh files you should see 4.

wc -l alone will not show anything because it is waiting for input, text piped in from find you should see what I described above.


All times are GMT -5. The time now is 02:56 AM.