Counting Number of Directories in a Specific Directory

How to count number of files in each directory?

Assuming you have GNU find, let it find the directories and let bash do the rest:

find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done

Counting number of directories

Since you are writing and reading the variable counter within the same block of code, namely the for loop, you need to establish delayed expansion; otherwise you always get the value present before the block is executed:

@echo off
setlocal EnableDelayedExpansion

set "folder=%~1"
if not defined folder set "folder=%cd%"

set /A counter=0

for /D %%a in ("%folder%\*") do (
echo folder !counter!: %%~nxa
SET /a counter=!counter!+1
)

endlocal

Anyway, set /A does not require explicit variable expansion, you could simply write set /A counter=counter+1, or even simpler, set /A counter+=1, so you do not need delayed expansion for this. For echoing the value !counter! in the loop however, you still need delayed expansion though.


There is an even easier approach to count the number of folders, using dir /B /A:D to return a plain folder list, piped by | into find /C to cound the number of received lines:

dir /B /A:D "%folder%\*" | find /C /V ""

You can use a for /F loop to capture the result and store it in a variable:

for /F %%a in ('
dir /B /A:D "%folder%\*" ^| find /C /V ""
') do (
set "counter=%%a"
)

Note the escaped pipe ^| needed herein.

How to count only the number of directories from a path

your stat() call will fail, since you are not in the correct directory. This you can solve by either changing the current directory, or generate full paths and give to stat as argument.

Some Unixes, you can optimize away the stat call by looking at struct dirent, the d_type field

int listdir(char *dir) {
struct dirent *dp;
struct stat s;
DIR *fd;
int count = 0;

if ((fd = opendir(dir)) == NULL) {
fprintf(stderr, "listdir: can't open %s\n", dir);
}
chdir (dir); /* needed for stat to work */
while ((dp = readdir(fd)) != NULL) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
#ifdef _DIRENT_HAVE_D_TYPE
switch (dp->d_type)
{
case DT_UNKNOWN:
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode)) count++;
break;
case DT_DIR:
count++;
break;
}
#else
stat(dp->d_name, &s);
if (S_ISDIR(s.st_mode)) count++;
#endif
}
closedir(fd);
return count;
}

How can I get the directory count of a given directory in bash?

I guess this will handle all cases:

find . -maxdepth 1 -type d -exec echo \; | wc -l

For each dir I print an empty newline... then count the newlines. Sadly wc does not work on null terminated strings, but we could remove all except zeros and count them:

find . -maxdepth 1 -type d -print0 | tr -cd '\0' | wc -c

As to your script, you are getting the error, because you need to enclose to comment in $( .. ) if you want to save it's output into a variable. Bash is space aware, I mean a=1 is assigment, and a = 1 is run program named a with arguments = and 1. Bash interprets the line: var=1 bash -c 'echo $var' as first it sets var=1 then runs the program bash with arguments -c and 'echo $var'. Try this:

assetid=$(ls -l /home/user/Desktop/folder | grep -c "^d")

But don't parse ls output. ls is for human readable nice colored output, it's better to prefer using different utilities in batch/piped scripts.



Related Topics



Leave a reply



Submit