Home > System Tutorial > LINUX > Find the files you need quickly! ——Use Linux find and wc commands to count the number of files

Find the files you need quickly! ——Use Linux find and wc commands to count the number of files

PHPz
Release: 2024-02-11 12:54:24
forward
1052 people have browsed it

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 - 打印文件绝对路径。
Copy after login

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
Copy after login

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:

快速找到你需要的文件!——使用Linux find和wc命令统计文件数量
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
Copy after login

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template