Count number of files within a directory in Linux?
this is one:
ls -l . | egrep -c '^-'
Note:
ls -1 | wc -l
Which means:ls
: list files in dir
-1
: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
|
: pipe output onto...
wc
: "wordcount"
-l
: count l
ines.
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¦
) redirectsfind
command's standard output towc
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
Count number of files in several folders with Unix command
A possible solution using xargs
is to use the -I
option, which replaces occurrences of replace-str
(%
in the code sample below) in the initial-arguments with names read from standard input:
find . -maxdepth 1 -type d -print0 | xargs -0 -I% sh -c 'echo -n "%: "; find "%" -type f | wc -l'
You also need to pass the find
command to sh
if you want to pipe it with wc
, otherwise wc
will count files in all directories.
Another solution (maybe less cryptic) is to use a one-liner for
loop:
for d in */; do echo -n "$d: "; find "$d" -type f | wc -l; done
Counting number of files in a directory with an OSX terminal command
You seem to have the right idea. I'd use -type f
to find only files:
$ find some_directory -type f | wc -l
If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth
flag:
$ find some_directory -maxdepth 1 -type f | wc -l
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.
Related Topics
How to Export the Variable Through Script File
Signing Windows Application on Linux-Based Distros
How to Get the Source Code for the Linux Utility Tail
Bash Capturing Output of Awk into Array
Does Gcc Have Any Options to Add Version Info in Elf Binary File
How to Read the Source Code of Shell Commands
Setting the Vim Background Colors
How to Get the Difference (Only Additions) Between Two Files in Linux
Bash Script: Using "Script" Command from a Bash Script for Logging a Session
Just Black Screen After Running Qemu
How to Determine If a Detached Pthread Is Alive
Signed Executables Under Linux
How to Undo Strip - I.E. Add Symbols Back to Stripped Binary
Changing Environment Variable of a Running Process
How to Remove All .Svn Directories from My Application Directories
How to Delete All Lines in a File Starting from After a Matching Line