Searching File And Directory With Command

Define Find, Grep and Its Types

Find

Getting ready

$ vagrant up
$ vagrant ssh

How we to do it

From the man pages find, it search for files in a directory hierarchy.

use help command for find

# find --help

find key option for basic

$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [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 -xautofs -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 ;

Find with command line and Its type of Syntax

find command for filename matching search from local system.

$ sudo find / -name passwd
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd

$ sudo find / -name fstab
/etc/fstab

match with case insensitive

$ sudo find / -iname passwd
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd

$ sudo find / -iname fstab
/etc/fstab

find the user owner file.

$ sudo find / -user shyam
.
/var/spool/mail/shyam
/home/shyam
/home/shyam/.bash_logout
/home/shyam/.bash_profile
/home/shyam/.bashrc
/home/shyam/.bash_history

find the group owner file.

$ sudo find / -group shyam
.
/home/shyam/.bash_logout
/home/shyam/.bash_profile
/home/shyam/.bashrc
/home/shyam/.bash_history

find the uid owner file

$ sudo find / -uid 1001
.
/home/shyam
/home/shyam/.bash_logout
/home/shyam/.bash_profile
/home/shyam/.bashrc
/home/shyam/.bash_history

find the gid owner file

$ sudo find / -gid 1001
.
/home/shyam
/home/shyam/.bash_logout
/home/shyam/.bash_profile
/home/shyam/.bashrc
/home/shyam/.bash_history

find the permission file

for exactly match permission

$ sudo find /var/log -perm 600
/var/log/tallylog
/var/log/audit/audit.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/syslog
/var/log/anaconda/anaconda.xlog
/var/log/anaconda/anaconda.program.log
/var/log/anaconda/anaconda.packaging.log
/var/log/anaconda/anaconda.storage.log
/var/log/anaconda/anaconda.ifcfg.log
/var/log/anaconda/ks-script-SxLm6U.log
/var/log/anaconda/ks-script-YCWTKO.log
/var/log/anaconda/ks-script-ljNRat.log
/var/log/grubby
/var/log/maillog-20160518
/var/log/maillog
/var/log/messages-20160518
/var/log/messages
/var/log/secure-20160518
/var/log/secure
/var/log/spooler-20160518
/var/log/spooler
/var/log/yum.log-20160518
/var/log/yum.log
/var/log/btmp-20160518
/var/log/btmp

for permission at least

$ sudo find /var/log -perm -600
/var/log
/var/log/tallylog
/var/log/lastlog
/var/log/wtmp
/var/log/ppp
/var/log/audit
/var/log/audit/audit.log
/var/log/tuned
/var/log/tuned/tuned.log
/var/log/anaconda
/var/log/anaconda/anaconda.log
/var/log/anaconda/syslog
/var/log/anaconda/anaconda.xlog
/var/log/anaconda/anaconda.program.log
/var/log/anaconda/anaconda.packaging.log
/var/log/anaconda/anaconda.storage.log
/var/log/anaconda/anaconda.ifcfg.log
/var/log/anaconda/ks-script-SxLm6U.log
/var/log/anaconda/ks-script-YCWTKO.log
/var/log/anaconda/ks-script-ljNRat.log
/var/log/grubby
/var/log/VBoxGuestAdditions.log
/var/log/vboxadd-install.log
/var/log/vboxadd-install-x11.log
/var/log/dmesg.old
/var/log/httpd
/var/log/cron-20160518
/var/log/cron
/var/log/maillog-20160518
/var/log/maillog
/var/log/messages-20160518
/var/log/messages
/var/log/secure-20160518
/var/log/secure
/var/log/spooler-20160518
/var/log/spooler
/var/log/yum.log-20160518
/var/log/yum.log
/var/log/btmp-20160518
/var/log/btmp
/var/log/boot.log
/var/log/dmesg

find the size file

for exactly match size

$ sudo find /var/log -size 2M
/var/log/audit/audit.log

for more than exact size

$ sudo find /var/log -size +1M
/var/log/audit/audit.log

for less than exact size

$ sudo find /var/log -size -1M
/var/log/tallylog
/var/log/anaconda/ks-script-SxLm6U.log
/var/log/anaconda/ks-script-YCWTKO.log
/var/log/anaconda/ks-script-ljNRat.log
/var/log/spooler-20160518
/var/log/spooler
/var/log/btmp-20160518

How it work

Grep

Getting ready

$ vagrant up
$ vagrant ssh

How to do it

From the man pages grep, it print lines matching a pattern.

use help command for grep

# grep --help

grep key option for basic

$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to 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             print version information and exit
      --help                display this help and exit
      --mmap                deprecated no-op; evokes a warning

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -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 the file name for each match
  -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 the part of a line matching PATTERN
  -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=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching 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
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --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)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

use grep for simple command line

$ grep shyam /etc/passwd
shyam:x:1001:1001::/home/shyam:/bin/bash

use grep for particular name

$ grep -n shyam /etc/passwd
22:shyam:x:1001:1001::/home/shyam:/bin/bash

use grep with pipe command

$ ls -l /etc | grep passwd
-rw-r--r--.  1 root root   1021 May 19 05:19 passwd
-rw-r--r--.  1 root root    980 Aug  1  2014 passwd-

$ ls -lrt /etc | grep passwd
-rw-r--r--.  1 root root    980 Aug  1  2014 passwd-
-rw-r--r--.  1 root root   1021 May 19 05:19 passwd

use grep command search with ignore-case.

$ grep -i 'n' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
vagrant:x:1000:1000:vagrant:/home/vagrant:/bin/bash
vboxadd:x:998:1::/var/run/vboxadd:/bin/false
shyam:x:1001:1001::/home/shyam:/bin/bash

use grep command search with invert-match.

$ grep -v '[#;]' /etc/selinux/config 

SELINUX=enforcing
SELINUXTYPE=targeted

use grep command search with ignorecase and invert-match

$ grep -iv 'sbin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
vagrant:x:1000:1000:vagrant:/home/vagrant:/bin/bash
vboxadd:x:998:1::/var/run/vboxadd:/bin/false
shyam:x:1001:1001::/home/shyam:/bin/bash

How it work

results matching ""

    No results matching ""