Welcome Reprint! When reproduced, please indicate the source:http://blog.csdn.net/nfer_zhuang/article/details/42582425
Introduction
Usage scenarios:
My QQ group in the recent chat is in full swing, also led to my QQ buffer folder size in the crazy rise, when the QQ client will prompt users to clear the cache folder after 500M. The largest part of the buffer folder is a variety of picture files, including pictures in various formats. Before deleting, I want to follow the picture format and then back up to other folders. This time I need to do a statistic:
- Pictures with several formats (suffix names) under the current folder
- How many files are in each format of a picture
General Usage:
Open the File Manager and go to the folder to be counted
- Sort By type
Select all files that specify the suffix name, and in the File Manager, the status bar has a count of the number of files selected, recorded
- Then select all the files for the next suffix and count and record until all the statistics are complete
Sentence script usage:
Find-type f-name "* *" | cut-f3-d '. ' | Sort | Uniq-c -I (recursively finds all subfolders under the current folder) or <pre name= "code" class= "plain" >find-maxdepth 1-type f-name "* *" | cut-f3-d ' .' | Sort | Uniq-c -I (Find only the current folder without recursion)
The above script decomposition steps are:
- Find all file name types in *. * format
- Truncate the name of the suffix in the File name section
- After sorting the suffix names and counting
Note: Since my QQ is running in the window system, the above script is executed through Cynwin.
The Find Command section explains
First look at the description in the Man Handbook of Find:
-maxdepth levels
Descend at most levels (a non-negative integers) levels of directories below the command line arguments.
-type C
File is of type C:
D Directory
F Regular File
-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern.
Here's what I'm doing:
- Find only under the current folder, so use the-maxdepth 1 parameter (Note that if you have a-maxdepth parameter, you must put it before all other parameters, otherwise waring information will appear)
- To find only files, without needing to care about folders, use the-type f parameter (note that there will be file types in other formats, such as linked files, block files, etc.) on Linux, and that the shortcuts on the window System are in the format of general files.
- Only files with a suffix are filtered out, so use the-name "* *" parameter (Note that using the-name "*. *" parameter will also filter out the. Name and name.), so filter * * * and "." There are characters before and after the file, you can use-regex "\./.+\. + "parameters, specific function please Baidu" find regular expression ")
Cut Command Section Description
In the same vein, take a look at the description in Cut's Man Handbook:
-F,--fields=list
Select only these fields; Also print any line this contains no delimiter character, unless the-s option is specified
-D,--delimiter=delim
Use DELIM instead of the TAB for field delimiter
The contents of the output after the find command above are in the following format:
./a.png
./b.jpg
...
So, here we just need a second "." After the content, so use the-d '. ' parameter specifies the use of '. ' As a delimiter, and then use-F3 to explicitly output the contents of the third field, that is, all the suffix names.
The sort command and the Uniq command section explain
The sort command is well understood, which is to sort the preceding output in order to operate with the Uniq command.
Let's focus on the use of the Uniq command, which describes the role of the Uniq command in this manual:
Uniq-report or omit repeated lines
The approximate function is to find successive lines of repetition and report them. Note that Uniq is concerned with: 1. Must be consecutive, so sort before use, and the action object of the command is row, so do not use this command if it is a repeating word within a statistical paragraph.
Let's look at the description of the-C and-I parameters for Uniq:
-C,--count
Prefix lines by the number of occurrences
-I.,--ignore-case
Ignore differences in case when comparing
Here, our requirement is to count the number of occurrences of all duplicate rows (suffix names), so the-c parameter is used, and the window system is case insensitive, so the-I parameter is used.
Note: There is a post dedicated to the Uniq command "example of Linux command line Uniq" Using the example demonstrates the use of each parameter, you can go to a view.
Summarize
This one-sentence script uses the following knowledge:
- The-maxdepth,-type,-name parameters of the Find command
- The-F,-D parameter of the Cut command
- Sort command
- The-C of the Uniq command,-i parameter
The number of files with various suffix names in the statistics folder of the script series