Linux count files number

How to count number of files in each directory?

Are you looking for a way to count the number of files in each of the sub-directories directly under ./ ?

How’s this an off-topic question?? I would like to see close-voters comments with reason! If this is off-topic then where does this belong to? super user? I don’t think so..

voted to reopen it. There may be other answers that could be useful in many situations (including script programming, which is the reason I reached this question).

21 Answers 21

This prints the file count per directory for the current directory level:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr 

By far the best (and most elegant) solution if one wants to list the number of files in top level directories recursively.

This has two problems: It counts one file per directory more than there actually is and it gives a useless line containing the size of the current directory as «1 size«. Both can be fixed with du -a | sed ‘/.*\.\/.*\/.*/!d’ | cut -d/ -f2 | sort | uniq -c . Add | sort -nr to sort by the count instead of the directory name.

I’d like to point out that this works in OSX, too. (Just copy-pasting Linux advice into an OSX shell usually doesn’t work.)

it fetches unneeded size by du -a . Better way is using find command. but main idea is exactly the same 🙂

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" "$" "$dir" done 

Its just a slighly different version from the above, so: ( hint: its sorted by name and its in csv) for x in find . -maxdepth 1 -type d | sort ; do y= find $x | wc -l ; echo $x,$y; done

Great one! Putting it into a single line (so it’s confortable for direct usage in shell): find . -type d -print0 | while read -d » -r dir; do files=(«$dir»/*); printf «%5d files in directory %s\n» «$» «$dir»; done

I needed to get the number of all files (recursively count) in each subdirectory. This modification gives you that: find . -maxdepth 1 -type d -print0 | while read -d » -r dir; do num=$(find $dir -ls | wc -l); printf «%5d files in directory %s\n» «$num» «$dir»; done

Читайте также:  Пропал звук kali linux

@Kory The following will do it: find . -maxdepth 1 -type d -print0 | while read -d » -r dir; do num=$(find «$dir» -ls | wc -l); printf «%5d files in directory %s\n» «$num» «$dir»; done | sort -rn -k1

@OmidS Great oneliner, but $dir should be inside quotes in your first comment to correctly handle dir names with whitespaces. : find . -maxdepth 1 -type d -print0 | while read -d » -r dir; do num=$(find «$dir» -ls | wc -l); printf «%5d files in directory %s\n» «$num» «$dir»; done

find . -type f | cut -d/ -f2 | sort | uniq -c 
  • find . -type f to find all items of the type file , in current folder and subfolders
  • cut -d/ -f2 to cut out their specific folder
  • sort to sort the list of foldernames
  • uniq -c to return the number of times each foldername has been counted

Perfect. And can be extended to count over subdirectories by replacing the field specifiers with a list of field specifiers. E.g,: find . -type f | cut -d/ -f2,3 | sort | uniq -c

You could arrange to find all the files, remove the file names, leaving you a line containing just the directory name for each file, and then count the number of times each directory appears:

find . -type f | sed 's%/[^/]*$%%' | sort | uniq -c 

The only gotcha in this is if you have any file names or directory names containing a newline character, which is fairly unlikely. If you really have to worry about newlines in file names or directory names, I suggest you find them, and fix them so they don’t contain newlines (and quietly persuade the guilty party of the error of their ways).

If you’re interested in the count of the files in each sub-directory of the current directory, counting any files in any sub-directories along with the files in the immediate sub-directory, then I’d adapt the sed command to print only the top-level directory:

find . -type f | sed -e 's%^\(\./[^/]*/\).*$%\1%' -e 's%^\.\/[^/]*$%./%' | sort | uniq -c 

The first pattern captures the start of the name, the dot, the slash, the name up to the next slash and the slash, and replaces the line with just the first part, so:

The second replace captures the files directly in the current directory; they don’t have a slash at the end, and those are replace by ./ . The sort and count then works on just the number of names.

Читайте также:  Узнать дисковое пространство linux

Источник

Count number of files within a directory in Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I am connecting via ssh to another host to access some data . Unfortunately a bunch of basic commands don’t seem to work on this host . If I use wc it returns «unrecognized command» . So I am looking for other options .

Use the tree command. It will give you the tree and at the bottom tell you how many files and directories there are. If you want hidden files also use tree -a .

@vanza «What exactly is the problem with wc» , what if a file has a \n in the file name? Yes, extremely unlikely! But still technically valid and possible.

1 Answer 1

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

No wait . I made a booboo . You are absolutely right Sajad Lfc . ls -1 dir | egrep -c » This returns the number of files in dir . Thanks .

@SajadKaruthedath ls -l . | egrep -c ‘^-‘ does not count hidden files. I suggest adding -a flag to ls .

@runios that’s because ls -l returns an additional line at the top adding up the file sizes for a total amount. You should use ls -1 and not the ls -l . Also if one wants hidden files but without the directories . and .. you should use ls -1A | wc -l

An effective native way without using pipe: du —inodes [root@cs-1-server-01 million]# du —inodes 1000001 ./vdb.1_1.dir 1000003 . [root@cs-1-server-01 million]#

Источник

Find the number of files in a directory

Is there any method in Linux to calculate the number of files in a directory (that is, immediate children) in O(1) (independently of the number of files) without having to list the directory first? If not O(1), is there a reasonably efficient way? I’m searching for an alternative to ls | wc -l .

Читайте также:  Linux mint команды консоли

ls | wc -l will cause ls to do an opendir(), readdir() and probably a stat() on all the files. This will generally be at least O(n).

Yeah correct, my fault. I was thinking of O(1) and O(n) to be same, although I should know it better.

8 Answers 8

readdir is not as expensive as you may think. The knack is avoid stat’ing each file, and (optionally) sorting the output of ls.

avoids aliases in your shell, doesn’t sort the output, and lists 1 file-per-line (not strictly necessary when piping the output into wc).

The original question can be rephrased as «does the data structure of a directory store a count of the number of entries?», to which the answer is no. There isn’t a more efficient way of counting files than readdir(2)/getdents(2).

One can get the number of subdirectories of a given directory without traversing the whole list by stat’ing (stat(1) or stat(2)) the given directory and observing the number of links to that directory. A given directory with N child directories will have a link count of N+2, one link for the «..» entry of each subdirectory, plus two for the «.» and «..» entries of the given directory.

However one cannot get the number of all files (whether regular files or subdirectories) without traversing the whole list — that is correct.

The «/bin/ls -1U» command will not get all entries however. It will get only those directory entries that do not start with the dot (.) character. For example, it would not count the «.profile» file found in many login $HOME directories.

One can use either the «/bin/ls -f» command or the «/bin/ls -Ua» command to avoid the sort and get all entries.

Perhaps unfortunately for your purposes, either the «/bin/ls -f» command or the «/bin/ls -Ua» command will also count the «.» and «..» entries that are in each directory. You will have to subtract 2 from the count to avoid counting these two entries, such as in the following:

expr `/bin/ls -f | wc -l` - 2 # Those are back ticks, not single quotes. 

The —format=single-column (-1) option is not necessary on the «/bin/ls -Ua» command when piping the «ls» output, as in to «wc» in this case. The «ls» command will automatically write its output in a single column if the output is not a terminal.

Источник

Оцените статью
Adblock
detector