Linux Search

find command

It supports searching by file, folder, name, creation date, modification date, owner and permissions

-type f: Search for files
-type d: Search for directories
-not : Return results that do not match the test case
-iname: Search without regard for text case
-perm octal: Search for the file if permission is ‘octal’ or with entered permissions

find ./Ananova -perm 777

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

find ./Ananova -perm 771 -exec chmod o+r '{}' \;

-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’
-print: Display the path name of the files found by using the rest of the criteria
-empty: Search for empty files and directories

find ./Ananova -empty

Find and delete all the ‘.err' files in the hierarchy starting at the current directory
find . -name “*.err” -delete

find . -name "*.err" -delete

-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

Starting from the root, search for the specified file

find / -name <filename>

Find specific file in the specified directory

Search a file with a pattern using ‘*' or ‘?'

To find all the text files with an extension ‘.txt' in Ananova directory

find ./Ananova -name *.txt

The below command finds and deletes adult-host.txt from Ananova directory by taking confirmation

$ find ./Ananova -name adult-host.txt -exec rm -i {} \;

locate command

locate <filename>

Update or create a database ‘mloacate' of the files under the root directory, used by ‘locate' command

-n: Limit Search Queries to a Specific Number to avoid redundancy

locate "*.php" -n 10

-c: Display The Number of Matching Entries

locate -c [LinuxHost]*
-i: Ignore Case Sensitive Locate Outputs
locate -i *linuxhost.php*

-e: Display Only Files Present in Your System

locate -i -e *linuxhost.php*

-S: to view the locate database statistics

updatedb

Search in all files for a specified string

grep command (global search for regular expression and print out)

The grep filter searches a file for a particular pattern of characters referred to as a regular expression and displays all lines that contain that pattern.

grep <string> <path>/<directory-name>

Search $PATH, man pages and source files for an application specified

-i: Ignores, case for matching

grep -i "Ananova" hosting.php

-c: This prints only a count of the lines that match a pattern
-h: Display the matched lines, but do not display the filenames.
-l: Displays list of a filenames only
-o: Print only the matched parts of a matching line, with each such part on a separate output line
-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

whereis command

whereis <application-name>

Search $PATH for an application specified

-b: Search only for binaries
-m: Search for manual sections
-s: Search for sources
-u: Search for unusual entries. A file is said to be unusual if it does not have one entry of each requested type. For example, “whereis -m -u *” asks for those files in the current directory which have no documentation
-B: Change or otherwise limit the places and searches for binaries
-M: Change or otherwise limit the placesand searches for manual sections
-S: Change or otherwise limit the places the command searches for sources
-f: Terminate the last directory list and signals the start of file names, and must be used when any of the -B, -M, or -S options used

which command

To find the location of a program

which <application-name>

-a: show all the places a program installed