Windows command – dir/s , find , findstr

Loading

dir /s – 找檔案位置.
find , findstr – 搜尋檔案內容.

dir/s

尋找檔案的時候,很多目錄需要有 administrator 權限,執行 dir /s 時建議透過 command prompt (run as adminstrator) 來執行.

C:\Windows\system32>dir wmic.* /s
 Volume in drive C has no label.
 Volume Serial Number is 72E9-F01E

 Directory of C:\Windows\system32\wbem

29/09/2017  21:42           517,632 WMIC.exe
               1 File(s)        517,632 bytes

 Directory of C:\Windows\system32\wbem\en-US

30/09/2017  22:34            64,000 WMIC.exe.mui
               1 File(s)         64,000 bytes

     Total Files Listed:
               2 File(s)        581,632 bytes
               0 Dir(s)  35,499,151,360 bytes free

參數說明:
/S : Displays files in specified directory and all subdirectories.

find

搜尋檔案內容.

C:\Windows\system32>find /?
Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt or piped from another command.

下面範例使用 ipconfig 透過 | (pipe) 轉向,用以代替檔案內容(ipconfig 的輸出即是檔案內容),搜尋含有 IPv4 字串的行列.

C:\Windows\system32>ipconfig | find "IPv4"
   IPv4 Address. . . . . . . . . . . : 192.168.95.186

findstr

findstr 與 find 相比,主要它多了正規表示式 (Regular expression) 的使用方式(類似 Linux 下的 grep)

C:\Windows\system32>findstr.exe /?
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See "color /?"
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename
             Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz Word position: beginning of word xyz\>    Word position: end of word

For full information on FINDSTR regular expressions refer to the online Command Reference.

下面範例使用 ipconfig 透過 | (pipe) 轉向,用以代替檔案內容(ipconfig 的輸出即是檔案內容),搜尋含有 IPv4 字串的行列.

C:\Windows\system32>ipconfig | findstr "IPv4"
   IPv4 Address. . . . . . . . . . . : 192.168.95.186

可以搭配 . , * , ^ , $ , [class] , [^class] , [x-y] , \x , \<xyz , xyz\>  正規表示式 (Regular expression) 來使用.
下面範例利用正規表示式,來搜尋與數字 0~9 相關的字串.

C:\Windows\system32>ipconfig | findstr [0-9]
Ethernet adapter Ethernet0:
   Link-local IPv6 Address . . . . . : fe80::495a:5c08:6aaf:758d%5
   IPv4 Address. . . . . . . . . . . : 192.168.95.186
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.95.2
   IPv6 Address. . . . . . . . . . . : 2001:0:2851:fab4:28e5:15e5:3f57:a045
   Link-local IPv6 Address . . . . . : fe80::28e5:15e5:3f57:a045%2

其實用法與 grep 使用正規表示式類似 – https://benjr.tw/97395

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料