Recursively Counting Files in a Linux Directory

Recursively counting files in a Linux directory

This should work:

find DIR_NAME -type f | wc -l

Explanation:

  • -type f to include only files.
  • | (and not ¦) redirects find command's standard output to wc command's standard input.
  • wc (short for word count) counts newlines, words and bytes on its input (docs).
  • -l to count just newlines.

Notes:

  • Replace DIR_NAME with . to execute the command in the current folder.
  • You can also remove the -type f to include directories (and symlinks) in the count.
  • It's possible this command will overcount if filenames can contain newline characters.

Explanation of why your example does not work:

In the command you showed, you do not use the "Pipe" (|) to kind-of connect two commands, but the broken bar (¦) which the shell does not recognize as a command or something similar. That's why you get that error message.

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

How can I count all the lines of code in a directory recursively?

Try:

find . -name '*.php' | xargs wc -l

or (when file names include special characters such as spaces)

find . -name '*.php' | sed 's/.*/"&"/' | xargs  wc -l

The SLOCCount tool may help as well.

It will give an accurate source lines of code count for whatever
hierarchy you point it at, as well as some additional stats.

Sorted output:

find . -name '*.php' | xargs wc -l | sort -nr

How to count all files inside a folder, its subfolder and all . The count should not include folder count

find . -type f | wc -l will recursively list all the files (-type f restricts to only files) in the current directory (replace . with your path). The output of this is piped into wc -l which will count the number of lines.

Count Files in directory and subdirectory in Linux using C

printf does not take an integer. It takes a format string and a variable list of arguments. Your problem can probably be fixed by changing:

    // Count Files
printf(countFilesRec(argv[1]));

to

   int fileCount = countFilesRec(argv[1]);
printf("File count = %d\n", fileCount);

The change stores the integer result of your function in a variable and then uses a suitable format string to print it.

Count all the files in a directory (including files in subdirectory) recursively

As noted in the comments, you need to return the incremented value.

  1. Change the function signature:

    int recorrido(const char *actual, int indent, int op, char *output, int numF)
  2. Change how the function calls itself:

    numF = recorrido(path, indent + 2, op, output, numF + 1);
  3. Return the modified value:


    if (! (dir = opendir(actual))) {
    return numF;
    }


    closedir(dir);
    return numF;
    }
  4. … and change how the function is called.

I also strongly suggest not mixing language (stick to English for code and comments!), and to spend time formatting your code cleanly and consistently (especially indentation and spacing). The readers of your code (including yourself!) will thank you — in fact, cleanly formatted code isn’t optional, it’s enforced without exception virtually everywhere.



Related Topics



Leave a reply



Submit