find - Unix, Linux Command



NAME

find - search for files in a directory hierarchy

SYNOPSIS

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

DESCRIPTION

find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

OPTIONS

TAG DESCRIPTION
-P Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.

-H Do not follow symbolic links, except while processing the command line arguments. When find examines or prints information about files, the information used shall be taken from the properties of the symbolic link itself. The only exception to this behaviour is when a file specified on the command line is a symbolic link, and the link can be resolved.

EXAMPLES

EXAMPLE-1:

To find all the files whose name is xyz.txt in a current working directory.:

# find . -name names.txt

output:
# ls
names.txt  test.txt  xyz.txt
# find . -name names.txt
./names.txt

EXAMPLE-2:

To find all the files under /tmp directory with name xyz.txt.

# find /tmp -name names.txt

output:
# ls /tmp/
names.txt  test.txt  xyz.txt

# find /tmp/ -name names.txt
/tmp/names.txt

EXAMPLE-3:

To find all the files whose name is names.txt and contains both capital and small letters in /tmp directory.:

# find /tmp -iname names.txt

output:
# ls /tmp/
names.txt  Names.txt  test.txt  xyz.txt

# find /tmp/ -iname names.txt
/tmp/Names.txt
/tmp/names.txt

EXAMPLE-4:

To find all directories whose name is test in / directory.

# find / -type d -name test

output:
/usr/src/linux-headers-3.19.0-25/lib/raid6/test
/usr/src/linux-headers-3.19.0-25-generic/include/config/pkcs7/test
/usr/src/linux-headers-3.19.0-25-generic/include/config/v4l/test
/usr/src/linux-headers-3.19.0-25-generic/include/config/test
/usr/src/linux-headers-3.19.0-25-generic/include/config/usb/ehset/test
/usr/lib/python3.4/test
/usr/lib/python2.7/dist-packages/OpenSSL/test
/usr/lib/python2.7/dist-packages/twisted/trial/_dist/test
/usr/lib/python2.7/dist-packages/twisted/trial/test
/usr/lib/python2.7/dist-packages/twisted/manhole/ui/test
/usr/lib/python2.7/dist-packages/twisted/manhole/test
/usr/lib/python2.7/dist-packages/twisted/internet/test
/usr/lib/python2.7/dist-packages/twisted/application/test
/usr/lib/python2.7/dist-packages/twisted/test
/usr/lib/python2.7/dist-packages/twisted/persisted/test
/usr/lib/python2.7/dist-packages/twisted/python/test
/usr/lib/python2.7/dist-packages/twisted/scripts/test
/usr/lib/python2.7/dist-packages/twisted/protocols/test
/usr/lib/python2.7/test

EXAMPLE-5:

To find all php files whose name is test.py in / directory.

# find / -type f -name test.py

output:
/usr/lib/python2.7/dist-packages/chardet/test.py

EXAMPLE-6:

To find all py files in / directory.

# find / -type f -name "*.py"

output:
/etc/python3.4/sitecustomize.py
/etc/python2.7/sitecustomize.py
/usr/src/linux-headers-3.19.0-25/scripts/analyze_suspend.py
/usr/src/linux-headers-3.19.0-25/scripts/rt-tester/rt-tester.py
/usr/src/linux-headers-3.19.0-25/scripts/checkkconfigsymbols.py
/usr/src/linux-headers-3.19.0-25/scripts/tracing/draw_functrace.py
/usr/share/doc/python-debian/examples/deb822/grep_native_packages.py
/usr/share/doc/python-pam/examples/pamtest.py
/usr/share/doc/apt-xapian-index/examples/axi-query-similar.py
/usr/share/doc/apt-xapian-index/examples/axi-query-tags.py
...
...
...
/usr/lib/python2.7/bsddb/dbobj.py
/usr/lib/python2.7/bsddb/db.py
/usr/lib/python2.7/bsddb/dbshelve.py
/usr/lib/python2.7/urllib.py
/usr/lib/python2.7/optparse.py
/usr/lib/python2.7/runpy.py
/usr/lib/python2.7/fractions.py
/usr/lib/python2.7/popen2.py
/usr/bin/miniterm.py

EXAMPLE-7:

 To find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

output:
# ls -l /tmp/
total 4
-rwxrwxrwx 1 root   root    0 Dec 29 15:06 names.txt
-rw-r--r-- 1 root   root    0 Dec 29 15:13 Names.txt
-rwxrwxrwx 1 ubuntu ubuntu 13 Dec 28 01:12 test.txt
-rw-r--r-- 1 root   root    0 Dec 29 15:05 xyz.txt

# find /tmp -type f -perm 0777 -print
/tmp/names.txt

EXAMPLE-8:

To find all files that belongs to user ubuntu under /tmp directory.

# find /tmp -user ubuntu

output:
# ls -l /tmp
total 4
-rw-r--r-- 1 root   root    0 Dec 29 15:13 Names.txt
-r--r--r-- 1 ubuntu ubuntu 13 Dec 28 01:12 test.txt
-rw-r--r-- 1 root   root    0 Dec 29 15:05 xyz.txt

# find /tmp -user ubuntu
/tmp/test.txt

EXAMPLE-9:

To find all files that belongs to group ubuntu under /tmp directory.

# find /tmp -group ubuntu

output:
# ls -l /tmp
total 4
-rw-r--r-- 1 root   root    0 Dec 29 15:13 Names.txt
-r--r--r-- 1 ubuntu ubuntu 13 Dec 28 01:12 test.txt
-rw-r--r-- 1 root   root    0 Dec 29 15:05 xyz.txt

# find /tmp -group ubuntu
/tmp/test.txt

EXAMPLE-10:

* To find a single file called names.txt and remove it.

# find . -type f -name "names.txt" -exec rm -f {} \;

output:
# ls /tmp
names.txt  Names.txt  test.txt  xyz.txt

# find /tmp -type f -name "names.txt" -exec rm -f {} \;

# ls /tmp
Names.txt  test.txt  xyz.txt

Print
Advertisements