Ok this is strange. I am using this code,

ls *.prj

To list all the files with the .prj extension in the dir but I am getting this error,

bash: /bin/ls: Argument list too long

I eventually wish to get the count of files and I was using,

ls *.prj | wc -l

But even this command is giving the same error. Any idea where I am going wrong?

3 Answers 3


Use find command instead

 find . -name "*.prj"

You can also combine the commands with find

find . -name "*.prj" -exec COMMAND {} \;

Hope this helps.

  • that would be correct if all the files in the directory were having only .prj extn, but I have files with same names but different extensions and your commands lists all of them
    – Sam007
    Nov 15, 2012 at 20:29
  • but by doing ls *.prj you are tyring to list the files with extn .prj right?
    – devav2
    Nov 15, 2012 at 20:37
  • the answer is above
    – Sam007
    Nov 15, 2012 at 20:44
  • can we do this for multiple extensions too? Aug 7, 2020 at 3:51

Nothing, there is a limit on the number of argument bash can deal with. Do

ls | grep '\.prj$' | wc -l
  • Yup that works great, that is what I wanted. Let this 15 min rule pass and I will select this as the right answer
    – Sam007
    Nov 15, 2012 at 20:27

Parsing the output of ls is unreliable. It will probably work in your case, but ls mangles unprintable characters. Here is a fully reliable way of counting the files matching a certain extension. This shell snippet creates an array containing the file names, then prints the number of elements in the array.

shopt -s nullglob
echo ${#a[@]}
  • ok I tried this and echo is giving me value 17 but the actual value is 90419, which is correctly calculated by the other two commands above
    – Sam007
    Nov 15, 2012 at 21:46
  • 2
    @Sam007 Sorry, that was an error in my command, I used zsh syntax that gives a different result in bash. ${#a} in zsh calculates the length of the array, but in bash it gives the length of the first element, and you need ${#a[@]} to get the number of elements. Nov 15, 2012 at 21:50
  • Yup that is working
    – Sam007
    Nov 15, 2012 at 22:00
  • Even if there is not a file with that extension, it outputs 1. And the list contains *prj
    – muyustan
    Mar 16, 2020 at 5:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .