0

I'm trying to make a script that counts files/directories in a specific directory but I have a problem. Here's the script that works for a current directory (count.sh):

ARG3=${1:-d} #default value - count directories 
ls -l| grep ^$ARG3 | wc -l

I'd love to make a script that works for any directory, not only current one but I have no idea how I could make it. I've searched in google for a long time, unfortunately without success.

The script should be called like:

./count.sh  what_to_count(files or dirs)  selected_dir_to_count_in_it

Do you have any ideas how to do it?

1
#!/bin/sh
filetype=${1:-d}
find "$2" -type $filetype | wc -l
  • Oh, it's that simple? Well, thank you :) could you also explain why it counts 1 more directory than I normally see? Is it counting '.' (current dir) as well? – adek111 Nov 30 '16 at 19:03
  • It's your homework to find out. – Ipor Sircer Nov 30 '16 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.