How To Count The Number Of Files And Directories in a Directory on Linux

Hey folks, today again we came up with a set of tricky commands that help you to count the number of files and directories in a directory on Linux.

Some commands help us to calculate them. But to get this, at least we need to combine two commands.

It counts files or directories or symbolic links or specific user created files and directories.

Also, allow us to count them recursively.

The ls, egrep, echo, wc, tree and find commands count them.

21 examples are included in this article to prove in detail.

To demonstrate this in detail, 21 examples are included in this article.

To demonstrate this, we have created a total of 16 files and 2 directories, including a hidden file and a symbolic file.

1) How to Count Files and Directories Recursively in a Directory on Linux Using the tree Command

The tree command with the -a option will count all together. The below example shows everything in detail.

$ tree -a /home/daygeek/test

/home/daygeek/test
├── 1.txt
├── 2g
│   ├── file-copy-rsync.sh
│   ├── file-copy-scp.sh
│   ├── file-copy.sh
│   ├── magi
│   │   └── file-copy.sh
│   ├── output.txt
│   ├── ovh.sh
│   ├── passwd-up1.sh
│   ├── passwd-up.sh
│   └── server-list.txt
├── 2.txt
├── 3.txt
├── daygeek -> /home/daygeek/test/2g/output.txt
├── .magi.txt
├── test
└── test1.txt

2 directories, 16 files

The above output could be awkward if there are a thousand of files and folders in a directory. To precise this, use the tree and tail command together.

$ tree -a /home/daygeek/test | tail -1

2 directories, 16 files

Remove the -a option to exclude hidden files in the tree command output. The Hidden files come with a dot (.) Prefix.

$ tree /home/daygeek/test | tail -1

2 directories, 15 files

2) How to Count Files and Directories in the Current Directory on Linux Using the ls Command

The ls command is the most basic command used by everyone in the Linux system.

The below ls command will count the number of files and directories in the current directory.

$ ls -l /home/daygeek/test | wc -l

8

3) How to Count Only Files in the Current Directory on Linux Using the ls Command

The ls command below counts the number of files in the current directory with the help of the grep command and the wc command.

$ ls -l /home/daygeek/test | grep ^- | wc -l

5

Alternatively this can be done using the egrep command without the wc command, see details below.

$ ls -l /home/daygeek/test | egrep -c '^-'

5

Details :

  • ls : list directory contents
  • -l : Use a long listing format
  • /home/daygeek/test : Directory path
  • | : control operator that send the output of one program to another program for further processing.
  • egrep : print lines matching a pattern
  • -c : General Output Control
  • '^-' : This respectively match the empty string at the beginning and end of a line.

4) How to Count All Files Including Files Hidden in the Current Directory on Linux Using the ls Command

The ls command below counts all files, including files hidden in the current directory.

$ ls -la /home/daygeek/test | grep ^- | wc -l

6

Alternatively this can be done using the egrep command without the wc command, see details below.

$ ls -la /home/daygeek/test | egrep -c '^-'

6

5) How to Count Only Folders in the Current Directory on Linux Using the ls Command

The below ls command will only count the number of folders/directories in the current directory.

$ ls -l /home/daygeek/test | grep ^d | wc -l

1

Also, you can use the wildcard (*) option to count directories in the current directory.

$ pwd
/home/daygeek/test

$ ls -ld */ | wc -l
1

6) How to Count All Files Including Files Hidden in the Current Directory Recursively on Linux Using the ls Command

The ls command below counts all files, including files hidden in the current directory recursively.

$ ls -laR /home/daygeek/test | egrep -c '^-'

15

7) How to Count All Files Excluding Files Hidden in the Current Directory Recursively on Linux Using the ls Command

The ls command below counts all files, Excluding files hidden in the current directory recursively.

$ ls -lR /home/daygeek/test | egrep -c '^-'

14

8) How to Count Only Folders in the Current Directory Recursively on Linux Using the ls Command

The ls command below counts all folders in the current directory recursively.

$ ls -lR /home/daygeek/test | egrep -c '^d'

2

9) How to Count All Files Including Files Hidden in the Current Directory Recursively on Linux Using the find Command

The find command below counts all files, including files hidden in the current directory recursively.

$ find /home/daygeek/test -type f | wc -l

15

Details :

  • find : search for files in a directory hierarchy
  • -type : File is of type
  • f : regular file
  • wc : It’s a command to print newline, word, and byte counts for each file
  • -l : print the newline counts

10) How to Count Only Folders in the Current Directory Recursively on Linux Using the find Command

The find command below counts all folders in the current directory recursively.

$ find /home/daygeek/test -type d | wc -l

3

11) How to Count Only Specific File Extension in the Current Directory Recursively on Linux Using the find Command

The find command below counts all files based on the extension in the current directory recursively.

$ find /home/daygeek/test -name "*.sh" | wc -l

7

12) How to Find All The Files Owned By a Particular User Using find Command on Linux

The find command below counts all files owned by a particular user, including files hidden in the current directory recursively.

$ find /home/daygeek/test -type f -user daygeek | wc -l

16

13) How to Find All The Folders Owned By a Particular User Using find Command on Linux

The find command below counts all folders owned by a particular user in the current directory recursively.

$ find /home/daygeek/test -type d -user daygeek | wc -l

3

14) How to Find All The Files Owned By a Particular Group Using find Command on Linux

The find command below counts all files owned by a particular group, including files hidden in the current directory recursively.

$ find /home/daygeek/test -type f -group daygeek | wc -l

15

15) How to Find All The Folders Owned By a Particular Group Using find Command on Linux

The find command below counts all folders owned by a particular group in the current directory recursively.

$ find /home/daygeek/test -type d -group daygeek | wc -l

3

16) How to Find All The Files and Folders Owned By a Particular User Using find Command on Linux

The find command below counts all files and folders owned by a particular user, including files hidden in the current directory recursively.

$ find /home/daygeek/test -user daygeek | wc -l

20

17) How to Count Files and Directories in the Current Directory on Linux Using the echo Command

The below echo command will count the number of files and directories in the current directory, including symbolic files. In the output below, the center value 7 represents the number of files and directories in the current directory.

$ echo * | wc

      1       7      44

18) How to Count Number of Files, Folders, Symbolic and Hard links in the Current Directory Recursively on Linux Using the find Command

The find command below counts all files and directories in the current directory recursively, including normal files, folders, symbolic links and Hard links files.

$ find /home/daygeek/test -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c
      3 dirs
     15 files
      1 symlinks

19) How to Count All Files Including Files Hidden on the Entire Linux System Using the find Command

The find below command counts all files on the entire Linux system.

# find / -type f | wc -l
69769

20) How to Count All Folders on the Entire Linux System Using the find Command

The find below command counts all folders on the entire Linux system.

# find / -type d | wc -l
8819

21) How to Count Number of Files, Folders, Symbolic and Hard links on Entire Linux System Using the find Command

The find command below counts all files and directories on the entire Linux system recursively, including normal files, folders, symbolic links and Hard links files.

# find / -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c
   8779 dirs
  69343 files
     20 hardlinks
  11646 symlinks

References :

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...