Loading....

How to use the  shell command to count files in a directory (directory can have one or more subdirectories)?
And you want to output the results as follows:
(Can display the count of files in a directory)

17 ./test/dir1
12 ./test/dir2
16 ./test/dir3/dir4/dir5
15 ./test/dir3/dir4/dir6
23 ./test/dir3/dir4/dir7


Maybe you can use ls and wc command.But today I will introduce you a alternative way to reach this goal-Use find and uniq command.Maybe that is more easy to understand and clear to use,I think.

Let's check this command:


find . -type f -printf %h"\n" | sort | uniq -c

The first part of the command acually list the leading directories of all the files in current directory.Like the file "/usr/bin/yes" only shows its directory "/usr/bin"。That's controled by the -printf %h"\n" option of the find command.

And finally  the command uniq merge the duplicate lines to the unique.And the -c option shows the repeated counts.So the count number represents the file counts in the directory at the same time.

Reference URI:https://ubuntu-sky.blogspot.com/2009/04/shell.html

Have not found what you want?? Try to search by Google

Quote URI


Share/Save/Bookmark

Related Articles

Comments: 0 comments

Post a Comment

Archives

Recent Entries

Recent Comment

Powered By Blogger