• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Online Courses
  • Shop
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / Linux / LPI Linux Essentials / Counting Files and Directories in Linux

Counting Files and Directories in Linux

August 10, 2020 by The Urban Penguin

counting files and directories in LinuxHow do you count the number of files or directories in Linux? In his blog we show you how to count files in a directory or the number of subdirectories. So when you are ready we shall begin! Although at first counting files and directories in Linux may not seem the most exciting topic you will be surprised just how much you can learn from these tasks. They are also well suited to those starting out in Linux with one or two elements that may be new even to seasoned Linux users. This all started from a question on my Facebook page. Even the answer can be quite simple there is more to investigate than you would at first think.

Firstly, if we want to be counting files and directories in Linux then the Linux command ls may be a great option Used in conjunction with the command wc we can count the number of items returned. The command ls is used to list directory content and wc is used for word count, used with -l it can count lines. Pipelining commands in fundamentals to UNIX and Linux

$ ls | wc -l

Whilst this is good we will not show hidden files or directories. Hidden files start with a dot. To list these we can use the option -a or -A with ls. To show all files we use -a and almost all files with -A. Yes almost all, we exclude the . and .. directories which are system links.

[tux@packstack ~]$ ls
dir1  dir2  dir3  dir4

[tux@packstack ~]$ ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  dir1  dir2  dir3  dir4

[tux@packstack ~]$ ls -A
.bash_logout  .bash_profile  .bashrc  dir1  dir2  dir3  dir4

[tux@packstack ~]$

If we want to count the output we are better not count the . and .. directory.

[tux@packstack ~]$ ls -A | wc -l
7

[tux@packstack ~]$

From the output we can see that we have a total of 7 items in the current directory.

If we want to count directories and files separately then we can use the GNU command find. To list files we can use the option -type f. Of course we could count the output as before.

[tux@packstack ~]$ find . -type f
./.bash_logout
./.bash_profile
./.bashrc

Listing directories is similar but we will see that we will include the current directory which we may not want.

[tux@packstack ~]$ find . -type d
.
./dir1
./dir2
./dir3
./dir4

To exclude the current directory from the count we can use the option -mindepth 1 to ensure that we start with the directories content and not the directory.[tux@packstack ~]$

find . -mindepth 1 -type d
./dir1
./dir2
./dir3
./dir4

So we can see that counting files and directories in Linux is not difficult but it can be even easier. Well at least counting directories. The hard link count for a directory can be used to show how many subdirectories there are in the directory. Each subdirectory has a link back to the parent. A directory start with a hard link count of 2 so just remove 2 from the current hard link count to see how many subdirectories.

[tux@packstack ~]$ ls -ld /etc
drwxr-xr-x. 112 root root 8192 Jun 8 15:03 /etc

The etc directory has 110 subdirectories on my system, 112 – 2 = 110

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: LPI Linux Essentials

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Online Instructor-led Courses
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
    • Bash Scripting Masterclass
    • Nftables Masterclass

© 2021 The Urban Penguin · All Rights Reserved