Reputation: 11
I am using the below command to get the count of csv in a directory
ll *.csv|wc -l
But if there are no csv files it prints
ls: cannot access *.csv: No such file or directory
0
I need to store this count in a variable. If there are 10 csv files then it should store 10 and if there are no csv files it should store 0.
Upvotes: 0
Views: 2948
Reputation: 5231
This is a better solution:
find ./ -type f -name '*.csv' | wc -l
...it should be noted that file names containing new lines will skew the results (the same with ls
)
If your find
has the -printf
option (like GNU find), you can do this:
find -type f -name '*.csv' -printf '.' | wc -c
This both handles filenames with new lines ok, and may be faster (see here for more info).
Note that if reading from stdin (and not a file given as argument), wc -l
or wc -c
return numeric values, including 0
, if there is no new line for wc -l
or no characters (empty string) for wc -c
.
Upvotes: 3