MQ-Jr
MQ-Jr
unixcop Admin

How to Count Number of Files and Sub-directories inside a Directory

- Advertisement -

Hi guys, In this article we will show you how to count the number of files and sub-directories in a directory using a tree command.

Count number of files and directories with tree command:

  • The tree command will work even if no arguments are given as shown below:
tree -iLf 1
Count Number of Files
Count Number of Files
  1. “-i”  : enables tree to print out indentation lines.
  2. “-L”  :specifies the level of depth of the directory tree to be displayed, which in the case above is 1.
  3. “-f”  :makes tree print the full path prefix for every file.
  • If you want to view the same information with no arguments for the depth level 2:
tree -iLf 2
  • Also if you want to view the same information but for a specified directory.

For example: /etc/systemd/

tree -iLf 1 /etc/systemd/

Sample output:

[root@rhel-pc ~]# tree -iLf 1 /etc/systemd/
/etc/systemd
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/resolved.conf
/etc/systemd/system
/etc/systemd/system.conf
/etc/systemd/user
/etc/systemd/user.conf

2 directories, 6 files
[root@rhel-pc ~]# 
  • To view the info for the previous directory but with depth level 2
tree -iLf 2 /etc/systemd/

Sample output:

[root@rhel-pc ~]# tree -iLf 2 /etc/systemd/
/etc/systemd
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/resolved.conf
/etc/systemd/system
/etc/systemd/system/basic.target.wants
/etc/systemd/system/bluetooth.target.wants
/etc/systemd/system/dbus-org.bluez.service -> /usr/lib/systemd/system/bluetooth.service
/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
/etc/systemd/system/dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
/etc/systemd/system/dbus-org.freedesktop.ModemManager1.service -> /usr/lib/systemd/system/ModemManager.service
/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
/etc/systemd/system/dbus-org.freedesktop.timedate1.service -> /usr/lib/systemd/system/timedatex.service
/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target
/etc/systemd/system/default.target.wants
/etc/systemd/system/dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants
/etc/systemd/system/display-manager.service -> /usr/lib/systemd/system/gdm.service
/etc/systemd/system/getty.target.wants
/etc/systemd/system/graphical.target.wants
/etc/systemd/system/local-fs.target.wants
/etc/systemd/system/multi-user.target.wants
/etc/systemd/system/network-online.target.wants
/etc/systemd/system/printer.target.wants
/etc/systemd/system/remote-fs.target.wants
/etc/systemd/system/snap.remmina.ssh-agent.service
/etc/systemd/system/sockets.target.wants
/etc/systemd/system/sysinit.target.wants
/etc/systemd/system/syslog.service -> /usr/lib/systemd/system/rsyslog.service
/etc/systemd/system/sysstat.service.wants
/etc/systemd/system/systemd-timedated.service -> /dev/null
/etc/systemd/system/timers.target.wants
/etc/systemd/system/var-lib-snapd-snap-bare-5.mount
/etc/systemd/system/var-lib-snapd-snap-core-11993.mount
/etc/systemd/system/var-lib-snapd-snap-core18-2284.mount
/etc/systemd/system/var-lib-snapd-snap-core20-1270.mount
/etc/systemd/system/var-lib-snapd-snap-discord-131.mount
/etc/systemd/system/var-lib-snapd-snap-gnome\x2d3\x2d28\x2d1804-161.mount
/etc/systemd/system/var-lib-snapd-snap-gnome\x2d3\x2d38\x2d2004-87.mount
/etc/systemd/system/var-lib-snapd-snap-gtk\x2dcommon\x2dthemes-1519.mount
/etc/systemd/system/var-lib-snapd-snap-remmina-5130.mount
/etc/systemd/system/var-lib-snapd-snap-snapd-14295.mount
/etc/systemd/system/var-lib-snapd-snap-spotify-56.mount
/etc/systemd/system/var-lib-snapd-snap-telegram\x2ddesktop-3544.mount
/etc/systemd/system/vmtoolsd.service.requires
/etc/systemd/system.conf
/etc/systemd/user
/etc/systemd/user/dbus-org.bluez.obex.service -> /usr/lib/systemd/user/obex.service
/etc/systemd/user/default.target.wants
/etc/systemd/user/sockets.target.wants
/etc/systemd/user.conf

20 directories, 30 files
[root@rhel-pc ~]# 
- Advertisement -

As you can view from the output above, after listing all the files and sub-directories, tree shows you the total number of directories and files in the directory you specified and the values will not the same when the depth level is different.

Conclusion

That’s it…..

In this article we illustrated how to count the number of files and sub-directories in a directory.

terminal tools

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook

x