Which command searches for and displays any text contents of a binary file?

The options (or flags) in this table are grouped by the function that they perform. Most options have a short form and a long form; the short form starts with a single dash, the long form starts with two dashes.

OptionDescriptionOptions that control matching behavior-E

--extended-regexp

Interprets pattern as an extended regular expression.-F

--fixed-strings

Interprets pattern as a fixed string.-G

--basic-regexp

Interprets pattern as a basic regular expression.-e pattern

--regexp pattern

Allows you to specify more than one search pattern, each separated with a -e. An input line is selected if it matches any of the specified patterns.-f patternfile

--file patternfile

Reads one or more patterns from a file, where patternfile is the path to the file that contains the patterns you are searching for. Separate pattern strings in patternfile by a newline character (\n). The newline character is not considered part of the pattern. You can specify more than one patternfile, each separated by -f.-i

--ignore-case

Ignores the case when searching for matching lines. If you do not specify this option, searches are case sensitive.-v

--revert-match

Displays the lines that do not match the specified criteria.-w

--word-regexp

Displays the lines that match only whole words.-x

--line-regexp

Displays lines only when pattern matches the entire line.Options that control which files are searched-HFollows symbolic links to directories during a recursive search (-R option) only if they were explicitly listed on the command line. The default behavior does not follow symbolic links.-PSpecifies not to follow symbolic links during a recursive search (-R option). This is the default.-RRecursively searches subdirectories; that is, if you specify a directory all files and subdirectories under that directory are searched. See the -H, -P, and -S options for additional controls when searching recursively.-SFollows all symbolic links to directories during a recursive search (-R option). The default behavior does not follow symbolic links.Options for binary files (See Additional information for more information)-I

--binary-files=without-match

Ignores binary files.-a

--text

--binary-files=text

Treats all files as text files. Matching lines in binary files are displayed.-U

--binary

--binary-files=binary

Searches binary files. Matching lines in binary files are not displayed; instead the message Binary file filename matches is displayed. This is the default.Options that control output-b

--byte-offset

Displays the matching lines prefaced with their relative offset in the file, starting at zero. The offset counter is reset for each file processed.-c

--count

Displays the count of matching lines instead of the lines themselves.-h

--no-filename

Displays matching lines without including the file name in the output. This is the default if less than 2 files are searched.-L

--files-without-match

Displays the file names that do not contain matching lines instead of the lines themselves. If the standard input (stdin) stream is searches, the text '(standard input)' is displayed.-l

--files-with-matches

Displays the file names that contain matching lines instead of the lines themselves. If the standard input (stdin) stream is searches, the text '(standard input)' is displayed. Each file is searched only until a match is found, which can make searches quicker.--line-bufferedSpecifies line buffering for the output line. The buffer is flushed when a newline character is written or when the buffer is full.-n

--line-number

Displays the matching lines prefaced with their relative line number in the file, starting at line 1. The line number counter is reset for each file processed.-oDisplays the matching lines prefaced with the file name. This is the default if more than 1 file is searched.-q

--quiet

--silent

Suppresses normal output. Each file is searched only until a match is found, which can make searches quicker. The -c, -l, and -L options override this option.-s

--no-messages

Suppresses error messages that result from files that do not exist or cannot be read.Options that control context line output-A anum

--after-context anum

Displays lines of context after each match, where anum is the number of lines of context that you want.-B bnum

--before-context bnum

Displays lines of context before each match, where bnum is the number of lines of context that you want.-C

-Cnum

--context

--context=num

Displays lines of context before and after each match, where num is the number of lines of context that you want. Do not enter a space between the -C option and num. If you do not specify num, 2 lines of context are displayed before and after each match.Options for other displays--helpDisplays help information to the standard error (stderr) stream and exits.-V

--version

Displays grep version information to the standard error (stderr) stream and exits.

Additional information

  • Online help information is available for this command. To display the help information, enter one of the following:
    • ZFILE HELP grep
    • ZFILE HELP egrep
    • ZFILE HELP fgrep
    • ZFILE HELP
    • ZFILE ?
    The correct command syntax also is displayed if you enter the syntax incorrectly.
  • The ZFILE grep command checks the first 1000 bytes of each file to determine if the file is a binary file or a text file. The file is handled as a text file if the first 1000 bytes contain only the following characters or symbols:

    a–zA–Z0–9ampersand (&)left angle bracket (<)right angle bracket (>)asterisk (*)at sign (@)back quote (ˋ) (X'79')backslash (\)backspace (X'16')left brace ( { )right brace ( } )left bracket ( [ )right square bracket ( ] )carat (^)carriage return (X'0d')colon (:)comma (,) (X'6b')dollar sign ($)equal sign (=)exclamation point (!)form feed (X'0c')forward slash (⁄)hyphen (-)newline (X'15')number sign (#)left parenthesis ( ( )right parenthesis ( ) )percent sign (%)period (.) (X'4b')plus sign (+)question mark (?)quotation mark (")semicolon (;)single quotation mark (')space (X'40')horizontal tab (X'05')vertical tab (X'0b')tilde (~)underscore (_)vertical bar (|)

    To determine whether a file will be handled as binary, enter zfile hex -c -n1000filename. If there are any characters displayed as \a or in hexadecimal notation, ZFILE grep will handle the specified file as a binary file.

  • Piped binary data is handled as text as if the -a parameter is specified. For this, the -I or -U parameter is ignored.
  • If you specify more than one parameter, you can specify these parameters separately or together. For example, you can enter one of the following:
    • ZFILE grep -c -i pattern path
    • ZFILE grep -ci patternpath
  • You can redirect the standard input (stdin) stream from the keyboard to a file by specifying the redirection character (<) followed by the file name that you want the input read from.

    You can redirect the standard output (stdout) stream from the display terminal to a file by specifying one of the redirection characters (> or >>) followed by the file name that you want the output written to. The > character writes the output to a file. The >> character appends the output to an existing file.

    You can redirect the standard error (stderr) stream from the display terminal to a file by specifying one of the redirection characters (2> or 2>>) followed by the file name that you want the error output written to. The 2> character writes the error output to a file. The 2>> character appends the error output to an existing file.

    For example, the ZFILE dd command displays information from both stdin and stdout. You can use redirection to send the information from stderr to stdin, as well as redirect all of the contents of stdout to a file for storage. The following example converts the contents of a file to uppercase, redirects the number of bytes converted from stderr to stdin, and redirects all of the output from stdin to a file named test3.

    User:   zfile cat test1
    
    System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
            this is a test
            END OF DISPLAY
    
    User:   zfile dd if=test1 conv=ucase >test3 2>&1
    
    System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY
    
    User:   zfile cat test3
    
    System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
            THIS IS A TEST
            0.1 records in
            0.1 records out
            15 bytes transferred in 1 secs (15 bytes/sec)
            END OF DISPLAY

    Note: When you use the > or 2> character and the file that you are redirecting data to already exists, the file is overwritten and any data in that file is lost. If you do not want to overwrite the file, ensure that you use the >> or 2>> character.

  • You can use a vertical bar, or pipe (|), to direct data so that the output from one process becomes the input to another process. This type of one-way communication allows you to combine ZFILE commands on one line to create a pipeline. For example, the following pipeline uses the standard output (stdout) stream from the ZFILE ls command and redirects it to the standard input (stdin) stream of the ZFILE grep command to search for those lines containing the word Jan.
    ZFILE ls -l | grep Jan
    The result is filtered output from the ZFILE ls command displaying only the lines containing the word Jan in any position.

    You can use pipes only with a combination of ZFILE commands where the command on the left side of the pipe provides data through stdout and the right side accepts data through stdin.

  • You can use the following wildcard characters in the path name to select files that satisfy a particular name pattern:*Matches any string, including the empty string.?Matches any single character.[..]Matches any one of the characters between the brackets. You can specify a range of characters by separating a pair of characters with a dash (-).
  • This command supports the following three quoting mechanisms, which allow you to override the special meaning of some characters:escape character (\)Preserves the literal value of the character that follows. To ignore the special meaning of a character, escape it by placing a backslash (\) in front of it. In the example that follows, environment variable PATH is /bin:/usr/bin:., the first dollar sign ($) is escaped because of the preceding backslash, and the second dollar sign takes on a special meaning.
    User:   ZFILE echo \$PATH is $PATH
    
    System: $PATH is /bin:/usr/bin:.
    single quotation marks (' ')Preserves the literal value of all characters between the opening single quotation mark and the closing single quotation mark. For example, entering ZFILE echo '*' displays an asterisk character (*). Without the single quotation marks, the files in the current working directory are displayed.quotation marks (" ")Preserves the literal value of all characters between the opening quotation mark and the closing quotation mark except the dollar sign ($), the backquote (`), and the backslash (\). This allows you to use the escape character inside quotation marks, which you cannot do inside single quotation marks.

    For example, entering ZFILE echo "\$PATH is \"$PATH\""
    displays $PATH is "/bin:/usr/bin:."

    If you want to use the literal meaning of any of the following characters, you always must use a quoting mechanism:

    left angle bracket (<)right angle bracket (>)ampersand (&)backquote (ˋ)backslash (\)dollar sign ($)newline (\n)left parenthesis ( ( )right parenthesis ( ) )quotation mark (")semicolon (;)single quotation mark (')blank spacetabvertical bar (|)

    In addition, use a quoting mechanism when any of the following characters are used in a way that takes on a special meaning, but you want to use the literal meaning:

    asterisk (*)equal sign (=)left bracket ( [ )number sign (#)question mark (?)tilde (~)

  • The ZFILE grep command is based on the grep utility of the Portable Operating System Interface for Computer Environments (POSIX) standards with several extensions.

Examples

The following test files are used in the examples:

  • /etc/group, which contains:
    root::0:root,operator
    bin::1:root,http,bin,tftp,ftp
    http::2:root,http,bin
    sys::3:root,bin,adm,mail,news,postfix
    adm::4:adm,operator,root
    operator::5:operator,root
    nogroup::55:nobody
    mail::8:root,mail,postmstr
    news::13:root,news
    ftp::25:root,ftp
    tftp::35:root,tftp
    tpfuser::100:root,tpfuser1,tpfuser2,tpfuser3
    tpfdfltg::257:root,tpfdfltu
  • /etc/tftp.conf, which contains:
    log=/logs/tftp.log
    auth:444
    allow:/staging/area1
  • /etc/postfix/main.cf, which contains in part:
    myhostname = tpf01.tpfmail.com
    mydomain = tpfmail.com
    mynetworks = 172.0.0.0/8, 192.0.0.0/8
    mail_name = TPF Mail
    smtpd_banner = $myhostname ESMTP $mail_name

The following example uses a simple regular expression to search the file for any line that ends with root.

User:   zfile grep root$ /etc/group

System: FILE0001I 19.36.31 START OF DISPLAY FROM grep root$ /etc/group
        adm::4:adm,operator,root
        operator::5:operator,root
        END OF DISPLAY

The following example uses fgrep so that the pattern is handled as a fixed string instead of a regular expression. Quotation marks are required in this example because the pattern contains a space.

User:   zfile fgrep "ESMTP $mail_name" /etc/postfix/main.cf

System: FILE0001I 19.45.32 START OF DISPLAY FROM fgrep "ESMTP $mail_name" /etc/postf...
        smtpd_banner = $myhostname ESMTP $mail_name
        END OF DISPLAY

Note: The results of the previous example are the same if you enter zfile grep -F "ESMTP $mail_name" /etc/postfix/main.cf.

The following example shows that matching is case sensitive by default; that is, the line in the /etc/postfix/main.cf file with the uppercase TPF did not match.

User:   zfile grep tpf /etc/postfix/main.cf

System: FILE0001I 19.51.57 START OF DISPLAY FROM grep tpf /etc/postfix/main.cf
        myhostname = tpf01.tpfmail.com
        mydomain = tpfmail.com
        END OF DISPLAY

The following example searches the specified file for all occurrences of the string tpf regardless of case. For this, both tpf and TPF match.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
0

The following example recursively searches files and directories in the /etc directory.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
1

The following example repeats the previous recursive example, but specifies the -lparameter to print only the names of the files with matching lines.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
2

The following example searches for the word ftp, not just any occurrence of the characters. Notice that the lines that contain tftp and do not contain ftp do not match.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
3

The following example repeats the previous example, but uses the long form of the -wparameter. The long form is preceded by two dashes, which produce the same results, but are more descriptive.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
4

The following example searches the specified file for more than one pattern. Notice that the patterns are handled as a logical or, not a logical and.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
5

The following example uses the -Cparameter to display three lines of context above and below the matching line.

User:   zfile cat test1

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test1
        this is a test
        END OF DISPLAY

User:   zfile dd if=test1 conv=ucase >test3 2>&1

System: FILE0003I 14.26.32 dd if=test1... COMPLETED SUCCESSFULLY. NO OUTPUT TO DISPLAY

User:   zfile cat test3

System: FILE0001I 14.26.32 START OF DISPLAY FROM cat test3
        THIS IS A TEST
        0.1 records in
        0.1 records out
        15 bytes transferred in 1 secs (15 bytes/sec)
        END OF DISPLAY
6

For more information about the POSIX standards, see Information Technology—Portable Operating System Interface for Computer Environments (POSIX).

Which Linux command provides the information by displaying the names of files in a directory?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How to search a particular word in a file in Linux using find command?

Using grep to Find a Specific Word in a File.
grep -rw '/path/to/search/' -e 'pattern' ... .
grep --exclude='*.csv' -Rw '/path/to/search' -e 'pattern' ... .
grep --exclude-dir='{dir1,dir2,*_old}' -Rw '/path/to/search' -e 'pattern' ... .
find ..

How to find text in all files Linux?

You can search for a text string all files under each directory, recursively with -r option:.
$ grep -r "redeem reward" /home/tom/.
$ grep -R "redeem reward" /home/tom/ Look for all files containing cacheRoot text on Linux:.
$ grep -R cacheRoot /home/vivek/.

Which command is used to display the amount of free space that exists on a filesystem?

df shows the amount of free space that is left on a file system. Space can have the following values: Space Used. Total amount of space that is allocated to existing files in the file system.