in

UbuntuUbuntu

How to Find Files & Contents in Linux using FIND & GREP Command?

Search Files using FIND & GREP Command in Linux!

Locate the Files using FIND and GREP Command

F

ind Files in Linux: Linux has many default commands which will help you find or search files and folders on your system. The command FIND & GREP can be used to search & locate the files on your Linux system.

Jump To:

Locate Files using FIND Command:

The FIND command option can be used to locate the files based on the location, name, and extension of the file on your Linux system.

The basic syntax of the FIND command is,

Syntax
$ find {path_destination} {file_name}

FIND Command Usage:

You can use the below example find command to locate the file,

$
cd ./Desktop

By using the above command, the terminal will get into the Desktop location on your Linux system.

~/Desktop$
find Example.txt

Then the above command will locate the file

The output of the command is,

[email protected]:~$ cd ./Desktop
[email protected]:~/Desktop$ find Example.txt
Example.txt
[email protected]:~/Desktop$ 

Find Command Options:

You can find the FIND command options by using the following command,

$
find --help

The above command will print the following available options.

[email protected]:~$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]      -context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Valid arguments for -D:
exec, opt, rates, search, stat, time, tree, all, help
Use '-D help' for a description of the options, or see find(1)

Please see also the documentation at http://www.gnu.org/software/findutils/.
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at
https://savannah.gnu.org/bugs/?group=findutils or, if
you have no web access, by sending email to <[email protected]>.
[email protected]:~$ 

That’s it. This is how you can use the Find command to locate the files in Ubuntu Linux.

Check this too  How to use MV Command in Linux Ubuntu 19.10/18.04 LTS?

Locate Files using GREP Command:

This is the alternate method to find or locate the files by matching a specified pattern on your Linux system. GREP command is the best alternative to the FIND command. Working with the GREP command is quite easy as equal as FIND Command.

Basic Command Syntax:

Syntax
$ grep [OPTION]… PATTERNS [FILE]…

GREP Command Usage:

You can use the GREP command to locate the files by matching specific pattern on your Linux system.

$
grep -i 'hello world' menu.h main.c

In the above command, the GREP command will search for files “menu.h & main.c” by matching the pattern “hello world“. The option -i is used to ignoring the case sensitive while locating the files.

GREP Command Options:

You can find the GREP command option by using the following command,

$
grep --help

The above command will print the following available options.

[email protected]:~$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM selected lines
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print file name with output lines
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only nonempty parts of lines that match
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --include=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files and directories matching GLOB
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)

When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: [email protected]
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
[email protected]:~$ 

That’s it. This is how you can use the GREP command to locate the file by matching a specific pattern on your Linux system.

Check this too  How to Change User Password by Passwd Command in Linux?

Verdict:

That’s it. Using FIND & GREP command is the fast and best method to search & locate the files on your Linux system. If you have any queries related to this article and then kindly comment us below.

Related Article:

Leave a Reply

Your email address will not be published. Required fields are marked *

Avatar

Written by Rachel

I'm Rachel, From Arizona. I am playing the "Content Management" Role in Tec Robust. I started working in TR from Jan 2019. Big Marvel Fan.

Contact me:
tecrobust [@] gmail.com
Sub: Message to Rachel

Contribute Us!

Skype for Linux Mint

Install Skype for Linux Mint 19.3 Tricia and Stay in Touch!!

Apache NetBeans

How to Install Apache NetBeans for Fedora 31?