In Windows Command Line (cmd), you can quickly find files (similar to the find command in Linux ).

Source: Internet
Author: User

In Windows Command Line (cmd), you can quickly find files (similar to the find command in Linux ).
For/r usage

Using the find command in Linux makes it very convenient to search for files. Is there a similar command in windows that can traverse directories and find files through file names? The answer is:

Windowsfor /rThe command hasfindThe command syntax is similar to the following:

Find/r directory name % variable name in (matching Mode 1, matching mode 2) do command

The matching mode can be wildcard similar:

  • *.jpg: All. jpg files
  • *test*: All files whose names contain test

Note:
1. At least 1 * is included in the matching mode.
2. The matching content is limited to the file name and does not match the directory name.

If you only search for and output file names, the command after do can be used@ Echo % variable name, That is:

For/r directory name % I in (matching Mode 1, matching mode 2) do @ echo % I
Common examples:
# List all files in the TestDir directory and all subdirectories in C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ B .jpg C: \ Users \ cashey \ Desktop \ TestDir \ c.png C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 1.txt C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 2.txt C: \ Users \ cashey \ Desktop \ TestDir \ src \ test. py # Find all txt files C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*. txt) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ doc \ 1.txt C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 2.txt# find all txt files and jpg files in the TestDir directory and all subdirectories C: \ Users \ cashey \ Desktop> for/r TestDir % I in (*. txt ,*. jpg) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ B .jpg C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 1.txt C: \ Users \ cashey \ Desktop \ TestDir \ doc \ 2.txt# find the file C whose file name contains test in the TestDir directory and all subdirectories: \ Users \ cashey \ Desktop> for/r TestDir % I in (* test *) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ src \ test. py
Notes

If the * sign is not included in the matching mode, the file name will not be exactly matched, but the matching mode will be spliced in all subdirectories, as shown below:

# In fact, the directory does not contain any file C: \ Users \ cashey \ Desktop> for/r TestDir % I in (abc) do @ echo % iC: \ Users \ cashey \ Desktop \ TestDir \ abcC: \ Users \ cashey \ Desktop \ TestDir \ doc \ abcC: \ Users \ cashey \ Desktop \ TestDir \ src \ abc

The above output is generally not what you expected. At least one * number is required in the mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.