To list all the files with the .png extension in the dir
ls | grep '\.prj$' | wc -l
Parsing the output of ls
is unreliable because 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.
a=(*.prj)
echo ${#a[@]}
Source:http://askubuntu.com/questions/217742/list-files-of-particular-extension
How to get a linux directory listing for files beginning with a certain letter that doesn’t descend into subdirectories?
Suppose you are in a directory that contains many files and many subdirectories.
You want to get a directory listing of all the files beginning with “Screen”. How do you list only the files and directory names in your current directory?
You type ls -d Screen*
Additionally, ls -d Screen*/ will only show the directories.
Another interesting solution with the flexibility of regular expressions be:
ls | grep ^Screen
however, if you add additional parameters to ls
it will break. E.g. “ls -l | grep ^Screen
” , but not all parameters. ls -a | grep "^\."
would print all “hidden” files.
ls -ld
: It will give the list of directories, without descending into subdirectories.
Example:
ls -ld Screen*
This command will provide a listing of the files and directories which start with Cust
.