- Count Number of Files in a Directory in Linux
- Count number of files in directory in Linux
- Count number of files and directories (without hidden files)
- Count number of files and directories including hidden files
- Count number of files and directories including the subdirectories
- Count only the files, not directories
- Count only the files, not directories and only in current directory, not subdirectories
- How to Count the Number of Files in a Directory in Linux
- 1. Check with File Manager
- 2. Select the Files/Directories You Want to Count
- 3. Get More Advanced Statistics with the Properties Window
- 4. How to Count the Number of Files via the Terminal
- 5 comments
- Popular Posts
- How to count Files and Directories in Linux
- 1) Counting files and directories in Directory with tree command
- 2) How to count files and directories in a Directory with ls command
- 2.a) Counting only files in a Directory
- 2.b) Counting all files (including hidden) in a Directory
- 2.c) How to count only Directory
- 2.d) How to count files recursively in Directory
- 2.e) How to count files recursively except hidden files in a Directory
- 2.f) How to count only Folders recursively in a Directory
- 3) How to count files recursively in Directory with find command
- 3.a) How to count only Folders recursively in Directory
- 3.b) How to count a specific file extension in Directory
- 3.c) Counting files owned by a specific user
- 3.d) Counting only directories owned by a specific user
- 3.f) Counting files owned by a specific group
- 3.g) Counting only directories owned by a specific group
- 3.h) Counting files and directories owned by a specific user
- 4) How to count files and directories in Directory using echo command
- 5) Counting Files, Directories, & Link Files in a Directory
- 6) Counts entire Linux system files
- 6a) Counts entire Linux system directories
- 7) Counting Files, Directories, Link Files in Linux
- Closing Notes
Count Number of Files in a Directory in Linux
I presume you are aware of the wc command for counting number of lines. We can use the same wc command with ls command to count the number of files in a directory.
This task seems simple but could soon turn slightly complex based on your need and definition of counting files. Before I confuse you further, let’s see about various use cases of counting the number of files in Linux.
Count number of files in directory in Linux
Let me first show you the content of the test directory I am going to use in this tutorial:
You can see that it has 9 files (including one hidden file) and 2 sub-directories in that directory. But you don’t have to do it manually. Let’s count the number of files using Linux commands.
Count number of files and directories (without hidden files)
You can simply run the combination of the ls and wc command and it will display the number of files:
This is the output:
There is a problem with this command. It counts all the files and directories in the current directories. But it doesn’t see the hidden files (the files that have name starting with a dot).
This is the reason why the above command showed me a count of 10 files instead of 11 (9 files and 2 directories).
Count number of files and directories including hidden files
You probably already know that -a option of ls command shows the hidden files. But if you use the ls -a command, it also displays the . (present directory) and .. (parent directory). This is why you need to use -A option that displays the hidden files excluding . and .. directories.
This will give you the correct count of files and directories in the current directory. Have a look at the output that shows a count of 11 (9 files and 2 directories):
You can also use this command to achieve the same result:
Note that it the option used is 1 (one) not l (L). Using the l (L) option displays an additional line at the beginning of the output (see ‘total 64’ in the directory output at the beginning of the article). Using 1 (one) lists one content per line excluding the additional line. This gives a more accurate result.
Count number of files and directories including the subdirectories
What you have see so far is the count of files and directories in the current directory only. It doesn’t take into account the files in the subdirectories.
If you want to count the number of files and directories in all the subdirectories, you can use the tree command.
This command shows the directory structure and then displays the summary at the bottom of the output.
As you can see in the output, it shows that there are 7 directories and 20 files in total. The good thing about this result is that it doesn’t count directories in the count of files.
Count only the files, not directories
So far, all the solutions we have seen for counting the number of files, also take directories into account. Directories are essentially files but what if you want to count only the number of files, not directories? You can use the wonderful find command.
You can run this command:
The above command searched for all the files (type f) in current directory and its subdirectories.
Count only the files, not directories and only in current directory, not subdirectories
That’s cool! But what if you want to count the number of files in the current directory only excluding the files in the subdirectories? You can use the same command as above but with a slight difference.
All you have to do is to add the ‘depth’ of your find. If you set it at 1, it won’t enter the subdirectories.
Here’s the output now:
In the end…
In Linux, you can have multiple ways to achieve the same goal. I am pretty sure there can be several other methods to count the number of files in Linux. If you use some other command, why not share it with us?
I hope this Linux tutorial helped you learn a few things. Stay in touch for more Linux tips.
Источник
How to Count the Number of Files in a Directory in Linux
When you have a folder full of files (and sub-folders) you might need to find out how many files are there altogether. Counting files and folders one by one is certainly not an option, especially when there are less gruesome ways to do it, such as the following.
1. Check with File Manager
Let’s start with the easiest way. Just open the directory in a file manager and look at the status bar.
If you don’t see a status bar, check “View -> Status bar” in the menu to see if the status bar is enabled.
The disadvantage of this method is that it counts a folder as one item but doesn’t count the number of files in it. Actually, the figure you are getting for how many files are in the directory is the sum of the number of folders and the number of separate files in this directory. If you want to view them separately (e.g. the number of files or the number of directories only), you need to select them, and then the status bar will show the number of the files/directories in the selection only.
2. Select the Files/Directories You Want to Count
In addition to showing the number of all files and folders in a directory, File Manager will allow you to do more. For instance, if you want to count only files or only folders, or only a part of the files/folder in a directory, just select them, and the status bar will show the number of files/folders in the selection. For example, if I wanted to see the number of the JPEG files only, I would select them. The result is shown in the status bar.
3. Get More Advanced Statistics with the Properties Window
The status bar looks great if you don’t have a lot of files and folders, but if you do, there are better ways to count them. For instance, the below image is of a directory listing of one of my working folders.
You see, it’s only directories with lots of files in each of them. The File Manager doesn’t show this, but if I select them and right click to open “Properties,” the Properties window tells me how many there are.
I guess the output varies from one file manager to another because I do recall seeing a different output (like a separate number for files and for folders), so what you get depends a lot on the file manager you are using.
4. How to Count the Number of Files via the Terminal
If the simple ways of counting files and folders don’t work for you, or if you are a terminal person, the good news is there are numerous ways to get some data about your files and folders via the terminal. For instance, if you simply want a number, use this:
This command returns just the number of files/folders. To count files recursively, use this:
I noticed that there is a difference in the numbers I get via the Properties window and via the console, and I assume this is because the console doesn’t count the directories or something.
There are other commands for directory listings such as list commands you can use to get a file number, among other things, but I am not going to discuss them. These commands come with many parameters, and the exact syntax varies from one Linux shell to the next. If you are interested in them, check the documentation of the shell you are using.
I am a fulltime freelancer who loves technology. Linux and Web technologies are my main interests and two of the topics I most frequently write about.
5 comments
You can also install the tree command if you want to count the number of files or folders in a tree. Use ‘sudo apt install tree’ to install it.
Regarding the command line methods …
1. This is not a correct count as ‘ls -l’ also prints a ‘total ‘. The correct count is therefore the count minus one.
2. It reports only the number of files because that is what you have told it to do ‘… -type f …’.
If you want it to list both directories and files (and everything else) just do ‘find | wc -l’, that will recursively ‘find’ all filesystem entities from the folder you are working in and down in the tree.
(If you want to find only directories ‘find -type d’)
“I noticed that there is a difference in the numbers I get via the Properties window and via the console, and I assume this is because the console doesn’t count the directories or something.”
The console command is only listing files due to the “-type f” flag on find, this means find files of type “regular file” (i.e: no links, directories, sockets etc).
Also, the command “ls -l’” does not list hidden files. You need the -a flag. This will affect the count. Your GUI file manager may or may not be set to show them.
Thank you all for the clarifications, me and the prompt are not exactly best friends 🙂 and honestly I didn’t check the commands in great detail, I thought it was more of a bug in either the GUI, or the shell, or both. And the gazillion of 1 and 2 letter options each command has is simply more than what my brain can hold. 🙂
Comments are closed.
Popular Posts
RedMagic 6S Pro Review: Gaming Is Serious Business.
How to Boot to Recovery Mode (Safe Mode) in Ubuntu
Ubuntu Software Center Not Working? Here Are the Fixes
How to Stress Test a Graphics Card on Linux
How to Mount a Windows Share Folder on Linux
How to Mount Your iPhone as an External Drive in Ubuntu
How to Fix Ubuntu Freezing in VirtualBox
How to Fix «Repository Does Not Have Release File» Error
How to Combine PDF Files on Windows and Linux
How to Reset the Root Password in Linux
8 Reasons to Switch from Windows to Linux
Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.
Источник
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.
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:
Remove the ‘-a’ option to exclude hidden files in the tree command output. The Hidden files come with a dot (.) prefix.
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.
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:
Alternatively, this can be done using the ‘egrep’ command without the wc command as shown below:
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:
Alternatively this can be done using the egrep command without the wc command, as shown below:
2.c) How to count only Directory
The below ls command will only count the number of folders/directories in a given directory:
Also, you can use the wildcard (*) option to count directories in the current directory:
2.d) How to count files recursively in Directory
The below ls command counts all files, including files hidden in the current directory recursively:
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:
2.f) How to count only Folders recursively in a Directory
The below ls command counts only folders in the current directory recursively:
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:
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:
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’ .
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:
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:
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:
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:
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:
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.
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.
6) Counts entire Linux system files
The below find command counts all files (including hidden files) in the entire Linux system:
6a) Counts entire Linux system directories
The below find command counts all folders on the entire Linux system:
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:
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!
Источник