Linux search commands

Linux search commands

In Linux, we have four various types of search commands

  • Grep

Grep is an acronym that stands for Global Regular Expression Print.

Grep is a Linux command used to search for a string of characters in a specified file.

To Search a File:

Syntax: Grep match_string file_name

Eg: grep database sample1

To Search Multiple Files: 

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character.

Syntax: grep match_string file_name file_name

Eg: grep database sample1 sample2 sample3
  • AWK

AWK this command  is used for pattern search and processing, scanning files line by line then performing specified actions on matching lines

Syntax: awk ‘{search options from to}’ file_name

Eg: $ awk '{print $2 "\t" $3}' file.txt
  • Locate

locate command and find command is used to search a file by name. But, the difference between both commands is that locating command is a background process and searches the file in the database whereas, find command searches in the file system. The locate command is much faster than the find command.

We have some options in locating search commands by locate -h (help cmd)
Search for entries in a locate database.

-A, --all only print entries that match all patterns
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)
Syntax: locate {Options…..} (filename)

 Eg:  locate ramkumar.txt

[oracle@oracletest pfile]$ locate ramkumar.txt
/home/oracle/scripts/ramkumar.txt

  • Find

find command is one of the most powerful tools in Linux, It supports searching by file, folder, name, creation date, modification date, owner, and permissions.

By using the ‘-exec’

Syntax: find [where to start searching from]
[expression determines what to find] [-options] [what to find]

Eg: find ramkumar

Options :

-exec CMD: The file being searched which meets the above criteria and returns 0 
for as its exit status for successful command execution.

-ok CMD : It works same as -exec except the user is prompted first.

-inum N : Search for files with inode number ‘N’.

-links N : Search for files with ‘N’ links.

-name demo : Search for files that are specified by ‘demo’.

-newer file : Search for files that were modified/created after ‘file’.

-perm octal : Search for the file if permission is ‘octal’.

-print : Display the path name of the files found by using the rest of the criteria.

-empty : Search for empty files and directories.

-size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to 
measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means 
size < 'N' blocks.

-user name : Search for files owned by user name or ID ‘name’.

\(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.

! expr : True if ‘expr’ is false.

 

 

Thank you for giving your valuable time to read the above information.

If you want to be updated with all our articles send us the Invitation or Follow us:

Ramkumar’s LinkedIn: https://www.linkedin.com/in/ramkumardba/
LinkedIn Group: https://www.linkedin.com/in/ramkumar-m-0061a0204/
Facebook Page: https://www.facebook.com/Oracleagent-344577549964301
Ramkumar’s Twitter: https://twitter.com/ramkuma02877110
Ramkumar’s Telegram: https://t.me/oracleageant
Ramkumar’s Facebook: https://www.facebook.com/ramkumarram8