Published on

Linux file Search and Comparison

Authors
  • Name
    Jackson Chen

grep

https://www.geeksforgeeks.org/grep-command-in-unixlinux/

The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by programmers, system administrators, and users alike for its efficiency and versatility in handling text data. The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by programmers, system administrators, and users alike for its efficiency and versatility in handling text data.

grep [options] pattern [files]
Options     Description
------------------------------------------
-c          This prints only a count of the lines that match a pattern
-h          Display the matched lines, but do not display the filenames.
–i          Ignores, case for matching
-l          Displays list of a filenames only.
-n          Display the matched lines and their line numbers.
-v          This prints out all the lines that do not matches the pattern
-e exp      Specifies expression with this option. Can use multiple times.
-f file     Takes patterns from file, one per line.
-E          Treats pattern as an extended regular expression (ERE)
-w          Match whole word
-o          Print only the matched parts of a matching line, with each such part on a separate output line.
-A n        Prints searched line and nlines after the result.
-B n        Prints searched line and n line before the result.
-C n        Prints searched line and n lines after before the result.

Examples

grep -l "unix" f1.txt f2.txt f3.xt f4.txt
grep -v "unix" geekfile.txt     # Inverting the pattern, display the lines that are not matched
grep "^unix" geekfile.txt       # ^     regular expression pattern specifies the start of a line
grep "os$" geekfile.txt         # $     specifies the end of a line
grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt
        # specifies epression with -e option for multiple matches

grep -R [Search] [directory]
        # search Recursively for a pattern in the directory
grep -iR geeks /home/geeks
# Search searching in file
grep "<string-to-be-searched>"  /<path-to-file>
    grep -rni  "search-string"  /path-to-directory

    grep -rni  "search-string"  /path-to-directory  -rli        # l     - print only the name of the file containg the pattern

How to list the files accessed by a program

# utilities that could be used
strace
loggedfs
auditctl
lsof

inotifywait -m -r -e OPEN /path/to/traced/directory     # seems works better
strace [program]  2>$1  | grep 'openat'
strace -f -e open [command]  2>$1

If you want to list files opened by specific process, just check /proc/$PID/fd directory. If you want to trace file operations of specific process (and it's childs) you can use strace

strace -efile -f -p$PID