Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Count Files in Directory in Linux - DesignLinux

designlinux 0 Comments

Although not very often, there are times when you need to find out how many files are in a given directory. For example, if you run out of inodes on your Linux system, you’ll need to find which directory contains thousands or millions of files.

In this article, we will show you several different ways to find the number of files in a directory in Linux.

Count Files in Directory #

The simplest way to count files in a directory is to list one file per line with ls and pipe the output to wc to count the lines:

ls -1 DIR_NAME | wc -l

The command above will give you a sum of all files, including directories and symlinks. If you want to count only files and not include the directories you could use the following:

ls -1p DIR_NAME | grep -v / | wc -l

The -p option tells ls to append slash (/) indicator to directories. The output is piped to the grep -v command that exclude the directories.

ls -1 also doesn’t count hidden files (dotfiles).

To have more control over what files are listed, you can use the find command instead of ls:

find DIR_NAME -maxdepth 1 -type f | wc -l

-type f option tells find to list only files (including dotfiles), and -maxdepth 1 limit search to the first-level directory.

Recursively Count Files in Directory #

To recursively count files in directory run the find command as follows:

find DIR_NAME -type f | wc -l

Another command that can be used to count files is tree that lists contents of directories in a tree-like format:

tree DIR_NAME

The last line of output will show the total number of files and directories listed:

15144 directories, 91311 files

Conclusion #

We have shown you how to count files in directory using the ls, find and tree commands.

If you have any questions or feedback, feel free to leave a comment.

find wc ls tree terminal

Related

Tags: find, ls, terminal, tree, wc

How to Check for Listening Ports in Linux (Ports in use)

Prev Post

How to Install TeamViewer on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 48
  • 605,798

DesignLinux.com © All rights reserved

Go to mobile version