How to count Files and Directories in Linux

Hey folks, here we have a set of tricky commands that will help you to count the number of files and directories in a directory on Linux.

If you run out of disk space on your Linux system, you will need to find which directory contains thousands of files.

Once you find some old or unused files or directories on your system, you can delete them using the rm command.

Files and Directories can be counted using several commands such as ‘ls’, ‘egrep’, ‘echo’, ‘wc’, ‘tree’ and ‘find’. But to get this, we need to combine at least two commands.

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

To demonstrate this, we have created a total of 16 files and 2 directories. Also, included 21 examples for better understanding.

1) Counting files and directories in Directory with tree command

The tree command with the -a option will count all together (files and directories) recursively. 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 thousands of files and folders in a directory. To make it precise, 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 a Directory with 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

2.a) Counting only files in a Directory

The below ls command counts the number of files in the given directory with combination of the grep & wc commands:

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

5

Alternatively, this can be done using the ‘egrep’ command without the wc command as shown 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.

2.b) Counting all files (including hidden) in a Directory

The below ls command counts all files, including files hidden in the given directory:

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

6

Alternatively this can be done using the egrep command without the wc command, as shown below:

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

6

2.c) How to count only Directory

The below ls command will only count the number of folders/directories in a given 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

2.d) How to count files recursively in Directory

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

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

15

2.e) How to count files recursively except hidden files in a Directory

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

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

14

2.f) How to count only Folders recursively in a Directory

The below ls command counts only folders in the current directory recursively:

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

2

3) How to count files recursively in Directory with find command

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

$ 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

3.a) How to count only Folders recursively in Directory

The below find command recursively counts only the folders in the current directory:

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

3

3.b) How to count a specific file extension in Directory

The below find command recursively counts all files based on the extension in the current directory. For instance, let’s count the list of files with the extension '.sh'.

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

7

3.c) Counting files owned by a specific user

The below find command recursively counts all files owned by a specific user, including files hidden in the given directory:

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

16

3.d) Counting only directories owned by a specific user

The below find command recursively counts all folders owned by a specific user in the given directory:

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

3

3.f) Counting files owned by a specific group

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

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

15

3.g) Counting only directories owned by a specific group

The below find command recursively counts all folders owned by a specific group in the given directory:

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

3

3.h) Counting files and directories owned by a specific user

The below find command recursively counts all files and directories owned by a specific user, including files hidden in the given directory:

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

20

4) How to count files and directories in Directory using echo command

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

$ echo * | wc

      1       7      44

5) Counting Files, Directories, & Link Files in a Directory

The below find command recursively counts all files and directories in the given directory, 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

6) Counts entire Linux system files

The below find command counts all files (including hidden files) in the entire Linux system:

# find / -type f | wc -l
69769

6a) Counts entire Linux system directories

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

# find / -type d | wc -l
8819

7) Counting Files, Directories, Link Files in Linux

The below find command recursively counts all files and directories on the entire Linux system, 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 :

Closing Notes

In this guide, we have shown you several examples to count files, directories and link files in a directory on Linux.

If you have any questions, please feel free to add your comments below, and we will address them at the earliest. Happy Learning!

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *