Shell: count the number of files in a folder..complicated

Cloturernapasdesensfigure Posted messages 117 Status Membre -  
UnGnU Posted messages 1468 Status Contributeur -
Hello,

I would like to place an order, or a series of orders, or create a script to count the number of files in a directory, and if the "ls" fails, display an error. I want to emphasize that I want to execute ls and display an error if it fails, not to determine in advance whether ls will fail.

For testing, I created:
A folder named aa containing a single file named "h h"
A folder named bb containing no files

I use ls $folder | wc -l to know the number of files.

Problem: if "ls" fails, the wc -l run in parallel still returns 0.

GNU-bash-4.1.2 $ ls
aa bb
GNU-bash-4.1.2 $ ls aa |wc -l
1
GNU-bash-4.1.2 $ ls bb |wc -l
0
GNU-bash-4.1.2 $ ls cc | wc -l
ls: cannot access cc: No such file or directory
0
GNU-bash-4.1.2 $


I tried to store the result of "ls" in a variable, but in that case I lose the last line break of the result of "ls", and wc -l gives an incorrect result.

Do you have a better idea? How would you solve this problem?
Thank you :)

5 réponses

zipe31 Posted messages 34620 Registration date   Status Contributeur Last intervention   6 499
 
Hello,

Maybe not the most optimized solution:

ls cc >/dev/null && ls cc | wc -l


--
_______________________________ ☯ Zen my nuggets ☮ ______________________________
Make a gesture for the environment, close your windows and adopt a penguin… 🐧
0
Cloturernapasdesensfigure Posted messages 117 Status Membre 5
 
:)
I thought about it for a moment but didn't test it since it involves running ls twice and I didn't like that
But I have to acknowledge its effectiveness

GNU-bash-4.1.2 $ ls aa>/dev/null && ls aa | wc -l || echo ERROR
1
GNU-bash-4.1.2 $ ls bb>/dev/null && ls bb | wc -l || echo ERROR
0
GNU-bash-4.1.2 $ ls cc>/dev/null && ls cc | wc -l || echo ERROR
ls: cannot access cc: No such file or directory
ERROR
GNU-bash-4.1.2 $


I’ll take it!
Thank you!

(I will try to adapt it also with a find command to actually count the number of files including any files with a "\n" in the name. If there's no simpler dedicated command for that)
0
Cloturernapasdesensfigure Posted messages 117 Status Membre 5
 
Hello again,
I would still like to find a proper solution.

I'm struggling.
I've tried many ways, unsuccessfully, here is my last (awful) attempt:
if list="$(find aa -type f -print0)"; then od -c <<<"$list" | fgrep -o '\0' | wc -l; else echo "ERROR"; fi

I really thought I was trying to do something simple, right? Count the number of files in a directory properly.
Do you see another way to do it, please?

My difficulties:
- passing through a temporary variable loses the information of the last line break and all NULL binary characters.
(I would like to avoid creating temporary files just for that)
- placing commands between "|" in a command within $( ) makes it so that I can't tell if there was an error during the execution of the first command, even with $PIPESTATUS.
0
karirovax Posted messages 3584 Status Membre 215
 
Hello,

find /path/ -type f is for files, use -type d for directories.
0
karirovax Posted messages 3584 Status Membre 215
 
Example for the current directory if you want to know how many directories:

find . -type d -name "*" | wc -l
0