Linux grep命令用於查找文件裏符合條件的字符串。是文本檢索中經常使用的工具之一。html
grep 指令在文件中查找可以匹配指定模式字符串的行。若是沒有指定文件名,或者文件名爲 - ,則從標準輸入設備獲取數據。默認會輸出匹配行。linux
grep will print lines matching a pattern. grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or 「-」 is given, grep searches standard input. By default, grep prints the matching lines.實驗文本獲取方式見sed篇其一。c++
使用方式以下git
grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]基礎用法匹配文本內容正則表達式
示例1查看文本中匹配 sed 字符的行。express
示例2表示若是咱們想包含大小寫,使用大小寫不敏感選項,查找內容就會包含 [sS][eE][dD] 。網絡
示例3展現了還能夠進行反向查找,也就是查找並打印不符合條件的行。ssh
示例4和5用於輸出匹配內容或不匹配內容的行的數量。工具
-G, --basic-regexp Interpret PATTERN as a basic regular expression. This is the default. -c, --count Suppress normal output; instead print a count of matching lines for each input file. -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f, search for all patterns given. This option can be used to protect a pattern beginning with 「-」. -f FILE, --file=FILE Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing. -i, --ignore-case Ignore case distinctions in both the PATTERN and the input files. -n, --line-number Prefix each line of output with the 1-based line number within its input file. -o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. -v, --invert-match Invert the sense of matching, to select non-matching lines. -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.cv@cv:~/myfiles$ grep 'sed' test.txt #example-1 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep -i 'sed' test.txt #example-2 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep -v 'sed' test.txt #example-3 NAME SYNOPSIS DESCRIPTION in a pipeline which particularly distinguishes it from other types of editors. -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed --follow-symlinks follow symlinks when processing in place -i[SUFFIX], --in-place[=SUFFIX] cv@cv:~/myfiles$ grep -c 'sed' test.txt #example-4 4 cv@cv:~/myfiles$ grep -c -v 'sed' test.txt #example-5 14示例6表示只匹配完整的單詞而不包括子串的形式。測試
示例7只顯示匹配到的地方,其餘部分忽略掉,當須要把匹配變量存入變量時比較有用。
示例8打印匹配行的同時顯示匹配行的行號。
示例9顯示以 sed 開頭的行,測試文本中沒有這樣的行,所以無輸出。
示例10顯示以 sed 開頭的匹配,這樣能夠排除 used 這樣開頭不符合的字符串,與 \bsed 效果相同。
示例11顯示以 sed 結尾的匹配,包含 used ,由於其最後的子串確實爲 sed 。
示例12和13都用來顯示徹底匹配 sed 的字符串,與上面的 -w 指令結果相同。
示例14能夠用於匹配多個不一樣的字符串模式,以下面的示例所示,加上第二個指令後會將原文第六行的 Sed 也顯示出來,不然匹配不到,由於首字母爲大寫。
cv@cv:~/myfiles$ grep -w 'sed' test.txt #example-6 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep -o 'sed' test.txt #example-7 sed sed sed sed sed cv@cv:~/myfiles$ grep -n 'sed' test.txt #example-8 2: sed - stream editor for filtering and transforming text 4: sed [OPTION]... {script-only-if-no-other-script} [input-file]... 6: Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an 7: editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep "^sed" test.txt #example-9 cv@cv:~/myfiles$ grep '\<sed' test.txt #example-10 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep 'sed\b' test.txt #example-11 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep '\bsed\b' test.txt #example-12 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep '\bsed\>' test.txt #example-13 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep -e 'sed' -e 'Sed' test.txt #example-14 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text示例15從給定模式文件中或去匹配模式,每行表示一個,結果一塊兒顯示出來。
pattern.txt sed Sed scripted cv@cv:~/myfiles$ grep -f pattern.txt test.txt #example-15 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed--color 的設置是爲了將檢索內容以彩色顯示出來。
A B C 的做用是同時打印匹配行的下NUM行。同時打印匹配行的上NUM行。同時打印匹配行的上下各NUM行。
示例1是使輸出匹配項爲彩色。
示例2是同時輸出匹配行的後兩行,3是前兩行,4是先後各兩行。
示例5是正則表達式中 | 的使用,在常規 grep 中應該使用反斜槓轉義。
--color[=WHEN] Surround the matched(non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto. -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. -B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches. -C NUM, -NUM, --context=NUM Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. For all the three command, with the -o or --only-matching option, this has no effect and a warning is given.cv@cv:~/myfiles$ grep --color=auto sed test.txt #example-1 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep -A2 'Sed' test.txt #example-2 Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors. cv@cv:~/myfiles$ grep -B2 'Sed' test.txt #example-3 sed [OPTION]... {script-only-if-no-other-script} [input-file]... DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an cv@cv:~/myfiles$ grep -C2 'Sed' test.txt #example-4 sed [OPTION]... {script-only-if-no-other-script} [input-file]... DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors. cv@cv:~/myfiles$ grep 'sed\|Sed' test.txt #example-5 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text文件名檢索
對 grep 而言更實用的部分是對文件的檢索。
-l 的做用是使檢索結果只保留文件名,而過濾掉在文件中匹配的位置,便於查看相關文件。
-r 的意思就是遞歸檢索,遇到文件夾就往文件夾下一級接着檢索,直到獨立的文件爲止。
-d 的做用是設置該命令檢索到文件時的處理方式,當設置爲 recurse 時,與直接使用 -r 效果相同。
示例1打印顯示該目錄下包含 sbin 的全部文件。
示例2使用 -d 實現了相同的結果,可是沒有直接使用-r簡潔。
示例3表示在檢索過程當中排除某個指定文件夾。
示例4表示排除多個文件夾時的寫法。
固然咱們也能夠將多個欲排除在檢索之列的文件名寫入一個文件中,而後使用 grep -lr --exclude-from=FILE 'sbin' /usr/include/* 的方式排除這些文件。
示例5展現了反選的結果,也便是該目錄下全部不包含給定模式字符串的文件的文件名。
示例6展現了利用文件名進一步過濾搜索結果的方式。
-l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. -L, --files-without-match Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match. -r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option. -d ACTION, --directories=ACTION Use ACTION to process directory. By default, ACTION is read, just as if the directories were ordinary files. If ACTION is skip, silently skip directories. If ACTION is recurse, read all files under each directory, recursively, following symbolic links only if they are on the command line. Equivalent to -r. --exclude-dir=DIR Exclude directories matching the pattern DIR from recursive searches. --exclude=GLOB Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally.cv@cv:~/myfiles$ grep -lr 'sbin' /usr/include/* #example-1 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/paths.h /usr/include/rpcsvc/nislib.h cv@cv:~/myfiles$ grep -l -d recurse 'sbin' /usr/include/* #example-2 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/paths.h /usr/include/rpcsvc/nislib.h cv@cv:~/myfiles$ grep -lr --exclude-dir='mpi' 'sbin' /usr/include/* #example-3 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/paths.h /usr/include/rpcsvc/nislib.h
cv@cv:~/myfiles$ grep -lr --exclude-dir={mpi,openmpi} 'sbin' /usr/include/* #example-4 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/paths.h /usr/include/rpcsvc/nislib.h cv@cv:~/myfiles$ grep -Lr --exclude-dir={mpi,openmpi} 'sbin' /usr/include/* #example-5 /usr/include/aio.h /usr/include/aliases.h /usr/include/alloca.h /usr/include/alsa/pcm_plugin.h /usr/include/alsa/pcm.h ...
cv@cv:~/myfiles$ grep -lr --exclude=*.h 'sbin' /usr/include/* #example-6
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc當咱們確切地知道須要檢索的一整行內容時,能夠使用 -x 排除掉其餘無關干擾,只顯示徹底匹配的行。
就至關於在匹配模式字符串前面添加了 ^ 後面添加了 $ 同樣。
使用 -F 能夠將引號內的匹配模板當作一個固定字符串。
示例1這種直接匹配時,會將中間的小數點當作能夠匹配任意一個字符的方式來工做。
示例2中,若是咱們就是想匹配中間是小數點的文件內容,則須要使用反斜槓轉義。
示例3表示還能夠使用 -F 選項,將後面的匹配模式固定爲一個字符串。
-x, --line-regexp Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $. -F, --fixed-strings Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.cv@cv:~/myfiles$ grep -r 'test.cpp' /usr/include/* #example-1 /usr/include/boost/config/compiler/sunpro_cc.hpp: // while processing ../test.cpp at line 0. /usr/include/boost/endian/detail/lightweight_test.hpp:// TODO: Should all test macros return bool? See BOOST_TEST_MEM_EQ usage in fp_exaustive_test,cpp /usr/include/boost/detail/named_template_params.hpp: // iterator_adaptor_test.cpp when using this method. /usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it... /usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp: // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp) /usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK. cv@cv:~/myfiles$ grep -r 'test\.cpp' /usr/include/* #example-2 /usr/include/boost/config/compiler/sunpro_cc.hpp: // while processing ../test.cpp at line 0. /usr/include/boost/detail/named_template_params.hpp: // iterator_adaptor_test.cpp when using this method. /usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it... /usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp: // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp) /usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK. cv@cv:~/myfiles$ grep -rF 'test.cpp' /usr/include/* #example-3 /usr/include/boost/config/compiler/sunpro_cc.hpp: // while processing ../test.cpp at line 0. /usr/include/boost/detail/named_template_params.hpp: // iterator_adaptor_test.cpp when using this method. /usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it... /usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp: // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp) /usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK.當有多個文件待搜索時,默認顯示匹配項的文件名。
當只有一個文件待搜索時,默認不顯示文件名。
-H, --with-filename Print the file name for each match. This is the default when there is more than one file to search. -h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.cv@cv:~/myfiles$ grep -rF -Hn --exclude-dir={mpi,openmpi} 'sbin' /usr/include/* #example-1 /usr/include/c++/5/streambuf:838: __copy_streambufs_eof(basic_streambuf<char>* __sbin, /usr/include/c++/5/streambuf:843: __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin, /usr/include/c++/5/bits/streambuf.tcc:116: __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin, /usr/include/c++/5/bits/streambuf.tcc:122: typename _Traits::int_type __c = __sbin->sgetc(); /usr/include/c++/5/bits/streambuf.tcc:132: __c = __sbin->snextc(); /usr/include/c++/5/bits/streambuf.tcc:139: __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin, /usr/include/c++/5/bits/streambuf.tcc:143: return __copy_streambufs_eof(__sbin, __sbout, __ineof); /usr/include/c++/5/bits/ostream.tcc:120: operator<<(__streambuf_type* __sbin) /usr/include/c++/5/bits/ostream.tcc:124: if (__cerb && __sbin) /usr/include/c++/5/bits/ostream.tcc:128: if (!__copy_streambufs(__sbin, this->rdbuf())) /usr/include/c++/5/bits/ostream.tcc:139: else if (!__sbin) /usr/include/paths.h:39: "/usr/bin:/bin:/usr/sbin:/sbin" /usr/include/paths.h:59:#define _PATH_SENDMAIL "/usr/sbin/sendmail" /usr/include/rpcsvc/nislib.h:273:extern nis_error __nisbind_create (dir_binding *, const nis_server *, /usr/include/rpcsvc/nislib.h:276:extern nis_error __nisbind_connect (dir_binding *) __THROW; /usr/include/rpcsvc/nislib.h:277:extern nis_error __nisbind_next (dir_binding *) __THROW; /usr/include/rpcsvc/nislib.h:278:extern void __nisbind_destroy (dir_binding *) __THROW; cv@cv:~/myfiles$ grep -rF -hn --exclude-dir={mpi,openmpi} 'sbin' /usr/include/* #example-2 838: __copy_streambufs_eof(basic_streambuf<char>* __sbin, 843: __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin, 116: __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin, 122: typename _Traits::int_type __c = __sbin->sgetc(); 132: __c = __sbin->snextc(); 139: __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin, 143: return __copy_streambufs_eof(__sbin, __sbout, __ineof); 120: operator<<(__streambuf_type* __sbin) 124: if (__cerb && __sbin) 128: if (!__copy_streambufs(__sbin, this->rdbuf())) 139: else if (!__sbin) 39: "/usr/bin:/bin:/usr/sbin:/sbin" 59:#define _PATH_SENDMAIL "/usr/sbin/sendmail" 273:extern nis_error __nisbind_create (dir_binding *, const nis_server *, 276:extern nis_error __nisbind_connect (dir_binding *) __THROW; 277:extern nis_error __nisbind_next (dir_binding *) __THROW; 278:extern void __nisbind_destroy (dir_binding *) __THROW;-q 表示不輸出任何信息,只要找到一個匹配就被認爲是成功退出。
-s 表示不輸出關於文件不存在或文件不可讀的錯誤信息。
-q, --quiet, --silent Do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
-s, --no-messages Suppress error messages about nonexistent or unreadable files.cv@cv:~/myfiles$ grep -lr 'sbin' /usr/include/* #example-1 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h /usr/include/paths.h /usr/include/rpcsvc/nislib.h cv@cv:~/myfiles$ grep -lrq 'sbin' /usr/include/* #example-2 cv@cv:~/myfiles$ grep -l 'sbin' /usr/include/* #example-3 grep: /usr/include/alsa: Is a directory grep: /usr/include/arpa: Is a directory grep: /usr/include/asm-generic: Is a directory grep: /usr/include/c++: Is a directory ... grep: /usr/include/pango-1.0: Is a directory /usr/include/paths.h grep: /usr/include/pixman-1: Is a directory ... cv@cv:~/myfiles$ grep -ls 'sbin' /usr/include/* #example-4 /usr/include/paths.h其餘示例。
示例1表示從 ifconfig 中提取網絡 IP 地址
示例2和3用於顯示所需進程的狀態。
cv@cv:~/myfiles$ ifconfig enp6s0 | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' #example-1 inet addr:10.249.152.52 Bcast:10.249.159.255 Mask:255.255.248.0 cv@cv:~/myfiles$ ps aux | grep 'ssh' #example-2 root 1645 0.0 0.0 65512 4964 ? Ss 10月24 0:00 /usr/sbin/sshd -D root 17684 0.0 0.0 94928 6988 ? Ss 11:22 0:00 sshd: cv [priv] cv 17780 0.0 0.0 94928 4476 ? S 11:22 0:00 sshd: cv@notty cv 17781 0.0 0.0 12880 1936 ? Ss 11:22 0:00 /usr/lib/openssh/sftp-server root 20338 0.0 0.0 94928 6608 ? Ss 16:31 0:00 sshd: cv [priv] root 20341 0.0 0.0 94928 6888 ? Ss 16:31 0:00 sshd: cv [priv] cv 20437 0.0 0.0 95236 4956 ? S 16:31 0:01 sshd: cv@pts/8 cv 20470 0.0 0.0 94928 4344 ? S 16:31 0:00 sshd: cv@notty cv 20475 0.0 0.0 12880 2020 ? Ss 16:31 0:00 /usr/lib/openssh/sftp-server cv 21422 0.0 0.0 15960 1020 pts/8 R+ 20:59 0:00 grep --color=auto ssh cv@cv:~/myfiles$ ps aux | grep 'ssh' | grep -v 'grep' #example-3 root 1645 0.0 0.0 65512 4964 ? Ss 10月24 0:00 /usr/sbin/sshd -D root 17684 0.0 0.0 94928 6988 ? Ss 11:22 0:00 sshd: cv [priv] cv 17780 0.0 0.0 94928 4476 ? S 11:22 0:00 sshd: cv@notty cv 17781 0.0 0.0 12880 1936 ? Ss 11:22 0:00 /usr/lib/openssh/sftp-server root 20338 0.0 0.0 94928 6608 ? Ss 16:31 0:00 sshd: cv [priv] root 20341 0.0 0.0 94928 6888 ? Ss 16:31 0:00 sshd: cv [priv] cv 20437 0.0 0.0 95236 4956 ? S 16:31 0:01 sshd: cv@pts/8 cv 20470 0.0 0.0 94928 4344 ? S 16:31 0:00 sshd: cv@notty cv 20475 0.0 0.0 12880 2020 ? Ss 16:31 0:00 /usr/lib/openssh/sftp-server
參考資料
[1] Linux grep 命令
[2] linux grep命令詳解