Home > Article > System Tutorial > Find the files you need quickly! ——Use Linux find and wc commands to count the number of files
In Linux systems, we often need to find certain specific files, which may be to back up, organize or delete them. However, once we have a large number of files, manual search becomes difficult and time-consuming. Fortunately, Linux systems provide some very useful tools to help us quickly find the files we need and count them. Today, we will introduce how to use the find and wc commands to quickly find the files you need and count their number.
We will use the find command, which is used to search for files in a directory hierarchy, following are the options we use in the find command as shown below:
-type - 指定要搜索的文件类型,在上面的情况下,f 表示查找所有常规文件。 -print - 打印文件绝对路径。
Here are the options used in our wc command as follows:
-l – This option prints the total number of newlines, that is, the total number of absolute file paths output by the find command.
General syntax of find command
# find . -type f -print | wc -l $ sudo find . -type f -print | wc -l
“
PS: Use the sudo command to read all files in the specified directory, including files in subdirectories with superuser permissions, to avoid "Permission denied" errors, as shown in the screenshot below:
Find Number of Files in Linux”
Number of files in Linux
You can see that in the first command above, the find command does not read all the files in the current working directory.
Here are more examples showing the total number of regular files in the /var/log and /etc directories respectively:
$ sudo find /var/log/ -type f -print | wc -l $ sudo find /etc/ -type f -print | wc -l
Through the method introduced in this article, you can easily use the find and wc commands to find and count the number of files you need. Whether you are backing up data, organizing files, or deleting files, these two commands are extremely useful tools. In addition, when dealing with a large number of files, manual search will become very tedious and time-consuming, so these two commands can help you improve your work efficiency. In short, mastering these two commands will definitely make your Linux system management easier.
The above is the detailed content of Find the files you need quickly! ——Use Linux find and wc commands to count the number of files. For more information, please follow other related articles on the PHP Chinese website!