Reference number: CH000756
How can I see how many files or directories are in
a Linux directory?
Question:
How can I see how many files or directories are in a Linux
directory?
Answer:Linux has several different methods of determining how many files
and/or directories are in a Linux directory. Below we have listed
a few different methods of how you can display the number of files
and/or directories in Linux.
Use the tree command
The tree command is an easy and quick way to not only get a list
of the files and directories, but to get a report with how many files
and directories there are. Keep in mind if tree is run without any
additional options that it will not only list the amount of files
and directories in the current directory, but all subdirectories as well.
If you wish to list only the contents of the current directory,
you can use the below command.
tree -i -L 1
See our tree command page for
additional information about this command as well as other available
options.
Use echo in combination with wc
Users can also quickly see how many files and/or folders are in a
directory by using the echo command in combination with the wc
command. Below are few different examples of how this can be done.
Get a count of all files and directories in the current
directory
echo * | wc
Once the above command has been typed you will get an output similar
to the below example. In this example the "10" indicates
the amount of directories and files in the current directory.
1 10 104
Get a count of only the directories in the current directory
echo */ | wc
Get a count of just the files in a directory
Note: This example is assuming all the files in the directory
have extensions. If a file does not have an extension it would not
be counted.
echo *.* | wc
See our echo command page and/or
our wc command page for additional
information about each of these commands.
|