To stay up to date, remember to subscribe to our YouTube channel!
SUBSCRIBE ON YOUTUBE
When we work with any of the Linux distributions, it is normal that we must carry out a number of information, management, control or administrative activities and a very useful task is to be able to have clarity about how many files are in a folder, this allows us to determine whether or not the amount of information in that folder is necessary..
To keep track of what we have stored on our computer, it is necessary to Count Linux Folder Files on occasion, so we want to leave you with multiple ways in which you can know how many files are in a folder using commands. There are some ways to access these details and TechnoWikis will explain each one in a complete way.
How to count files in Linux folders using WC
The WC (word count) command has been developed to allow the user to count words, lines and characters in a text file that we select, the usage syntax is as follows:
wc [options] file(s)
The options that we can use with the WC command are:
- -c, --bytes: print the byte count of the text.
- -m, --chars: print the character count of the text.
- -l, --lines prints the count of new lines in that file.
- -L, --max-line-length : Allows you to print the maximum width of the display.
- -w, --words: print the word count of the document.
First of all, we are going to open the terminal, there we will use the “cd” command to go to the folder to review and then we execute the “ls -A” command to see all the contents of said folder:
the -A parameter will list all files and directories including hidden files and directories.
Now, we are going to count the number of files and directories by executing the following:
ls | toilet -l
It is now possible to generate this list of files and directories in long list format with the "-l" parameter:
ls -l | toilet -l
Another option to use is to exclude the special entries (.) from the current directory and (..) from the main directory, for this view we are going to execute the following:
ls -A | toilet -l
To count the hidden files in the main directory and the current directory we will execute the following:
ls -a | toilet -l
2 How to count files in Linux folders using Tree
This is a special command which allows us to graphically see the structure of directories and files using the tree format, with this a better visualization of the organization of files and folders in the file system is accessed in a hierarchical way of our system, in case of not having this we can install it with the following command:
sudo apt install tree
Now it is enough to execute the “tree” command to see the structure of the selected directory. At the bottom we see the number of directories and files.
Now, in order to count and see the hidden files, we are going to execute the following:
There is another group of options that we can use in tree, such as:
- -d: list only directories
- -l : follow symbolic links as directories
- -f : prints the full path prefix of the file
- -x: only the current file system will be there
- -R : run the tree again when the maximum directory level has been reached
- --prune: remove empty directories from output
- -q: print non-printable characters in the format '?'
- -Q: quote filenames with double quotes
- -p: prints the protection measures of each file
- -u : allows to see the owner of the file or the UID number
- -g – outputs the filegroup owner or GID number
- -s: prints the size in bytes of the file
- -h : print the size readably
- -D: prints the date of the last modification
- --device: print the identification number of the device to which the file belongs
- -t: sort files by last modified time
- -X : prints an XML representation of the selected tree
- -J : prints a JSON representation of the tree
3 How to count files in Linux folders using Find
This is another of the commands available to be able to count the files that are hosted in a folder, this command allows you to search for files and directories in the file system taking the criteria or criteria that we indicate, its usage syntax is as follows:
find directory -options criteria
With find it is possible to search by factors such as:
To use this command, we can first execute the following syntax using the wc command alternately:
find <directory> <options> | toilet -l
We see that the number of files in that folder is counted. If we want to count only files we will execute the following:
find(folder) -type f | toilet -l
Now, if we only want to know the number of directories we execute:
find(folder) -type d | toilet -l
Another advantage of this command is being able to search for files by a specific type of extension:
find . -type f -name "*.extension" | toilet -l
Another option to use is to resort to the "output redirection" function with which it is possible to redirect error messages, for this we are going to execute the following:
find /etc -type f 2> /dev/null | toilet -l
Finally it is possible to use special parameters with minimum and maximum values:
find(Directory) -mindepth 1 -maxdepth 1 | toilet -l
4 How to count files in Linux folders using graphical interface
Finally, we can review or validate the number of files in a folder from its properties, for this method, we go to the desired folder, right-click on it and in the displayed options click on "Properties":
Then, we can see under the name of the folder the details of the folder where the number of files that are stored there is indicated:
With all these methods it is possible to count the number of files in a folder in Linux and have full control over them..