[Shell Foundation]--find Command

Source: Internet
Author: User
Tags file permissions

Find command options
-name Search by file name
-type find a type of file (b for device block; d directory; C character device file; L sign (soft) link file; F normal file)
-size find file length or size

-prune exclude current folder when locating files, do not find
-path
-depth find a file, first look for the current file, the file in the current directory, and then find it in its subdirectories
-maxdepth followed by the number num, which indicates the depth of num layers under the current directory (default is layer 1)

-perm follow file permissions to find
-user can be found according to the owner of the file
-group can be found in the file array.
-nouser finding files that are not valid for the master
-nogroup finding files for groups that are not valid

-newer: Finding files that are newer than the file update time
-mtime: Search by Object Content modification time (days)
-atime: Search by the time the object is accessed (days)
-ctime: Search by the time the object state is modified (number of days)
-mmin/-amin/-cmin: (Number of minutes)
"+ N" N days/minute ago
"-N" N days/minute or less

-empty finding a file size of 0 or an empty directory
-mount file system that does not cross mount when looking up the file system
-follow if the find command encounters a symbolic link file, the source file to which the link file points is traced

-A and
-O or
! Non -
-ok reminds you if you want to execute the following command

(1)-name

Find/test below all files ending in. txt # find/test-name "*.txt" find/test below all txt files starting with a number # find/test-name "[0-9].txt" Find/directory under "passwd" File and "Profile" File # Find/-name "profiles"-o-name "passwd"


(2)-type

Find all directories below/test # Find/test-type D | Xargs file Find all L connection files in/etc below # Find/etc-type L | Xargs Ls-l | More


(3)-size

Find files with current catalog files greater than 10M # find. -size +10m | Xargs DU–LH find files that are less than 10M in the current directory file # find. -size-10m | Xargs Du–lh find files larger than 1M in the/etc/directory and list file size information # find/etc/-size +1m |xargs du-sh


(4)-prune-path-depth

The current directory has 1.doc, 2.doc, its sub-directory/test 3.doc, 4.doc Find all the current directory under the. doc file # Find. -name "*.doc"./test/4.doc./test/3.doc./2.doc./1.doc ignores subdirectories/test, querying only the. doc file for the current directory # find. -path "./test"-prune-o-name "*.doc"./test./2.doc./1.doc enter (or specify) subdirectories/test find. doc file # Find./test-depth-name "*.doc". Test/4.doc./test/3.doc


(5)-perm

Find files with permission 755 under/test # find/test-perm 755


(6)-user-group-nouser-nogroup

Modify the Gongda.txt file of the genus Group for gongda# Useradd gongda# touch gongda.txt# chown Gongda:gongda gongda.txt Find the current directory subordinate to the main Gongda file # find. -user Gongda | Xargs ls-l-rw-r--r--1 Gongda gongda 0 May 20:50./gongda.txt find the file in the current directory under Group Gongda # Find. -group Gongda | Xargs ls-l-rw-r--r--1 Gongda gongda 0 May 20:50./gongda.txt Delete Gongda users and Groups # Userdel-r Gongda Find Invalid Master file # in current directory # find. -nouser | Xargs ls-l-rw-r--r--1 509 509 0 May 20:50./gongda.txt find file with invalid genus in current directory # Find-nogroup | Xargs ls-l-rw-r--r--1 509 509 0 May 20:50./gongda.txt


(7)-newer


(8)-mtime

Find files under/directory change within 5 days # Find/-mtime-5 Find in/directory change time 3 days before directory # Find/-mtime +3


(9)-mount


(10) Use EXEC or OK to execute shell commands
Find. -type f-exec ls-l {} \; Do not prompt
Find. -type f–ok ls-l {} \; Safety Tips

-ok after the general root Delete command RML
Find/test-type f–ok rm {} \;

(one) Xargs command
Xargs is more convenient than exec
Find/test–name "* *" | Xargs ls–l

Find Exercises

1. Find/directory under "passwd" File and "profile" file
# Find/-name "profile"-o-name "passwd"
/usr/local/python3.5.1/lib/python3.5/site-packages/ipython/core/profile
/usr/bin/passwd
/usr/libexec/emacs/23.1/x86_64-redhat-linux-gnu/profile
/usr/src/kernels/2.6.32-279.el6.x86_64/include/config/branch/profile
/root/.vnc/passwd
/root/apr-1.5.2/passwd
/root/passwd
/root/home/passwd
/etc/pam.d/passwd
/etc/passwd
/etc/profile
/passwd
/home/passwd

2. Find the file with permission 755 under/TMP, without creating one, modify permission
# find/tmp-perm 755

3, under/TEST/AA Create a file named Gongda.txt, with the Find command to the/TEST/AA directory to ignore if you can also find?

4, create a user and group named Gongda, and then gongda.txt the user and the owning group are all modified to Gongda,
Can I find a user group that uses users and group to find Gongda?
Then delete the Gongda users and groups, and then use Nouser and Nogroup to find the files that do not belong to and say the group can be found?
# Useradd Gongda
# Touch Gongda.txt
# chown Gongda:gongda Gongda.txt
# Find. -user Gongda |xargs ls-l
-rw-r--r--1 Gongda Gongda 0 May 20:50./gongda.txt
# Find. -group Gongda | Xargs ls-l
-rw-r--r--1 Gongda Gongda 0 May 20:50./gongda.txt
# Userdel-r Gongda
# Find. -nouser | Xargs ls-l
-rw-r--r--1 509 509 0 May 20:50./gongda.txt
# Find-nogroup | Xargs ls-l
-rw-r--r--1 509 509 0 May 20:50./gongda.txt

5. Find files within 5 days of change under/directory
# Find/-mtime-5

6. Find the directory under the/directory Change time 3 days ago
# Find/-mtime +3

7. Create a file named New.txt below/opt/test/. Wait 5 minutes before you create a ok.txt file. Use the Find command to find a newer file than the ok.txt older than Gongda.txt.
# find

8. Use Find to find all the directories under/home
# find/home-type D |xargs File
/home:directory
/home/user25:directory
/home/user25/.mozilla:directory

9. Use Find to find all of the following files, not directories
# Find/home! -type d |xargs File

10. Find all the connection files under/etc/using Find
# Find/etc-type L

11. Look for files larger than 1M in the/etc/directory, and list the file size information.
# find/etc/-size +1m |xargs du-sh
2.0m/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
6.3m/etc/selinux/targeted/policy/policy.24
6.3m/etc/selinux/targeted/modules/active/policy.kern

12. Look for files less than 500K below the/etc/directory, and list file size information
# find/etc/-size-500k |xargs du-sh

13. Find the Gongda.txt file under the/opt/test subdirectory
# find "./test"-depth-name gongda.txt
./test/gongda.txt

14, check the system has a few hanging in the file system, such as/and home is separate,. Then create a/home/in the
Sxgongda.txt file. Can I find the Sxgongda.txt file using the Find parameter mount Lookup/directory?

15. Query/opt/The following Gongda.txt file, and use Exec to list its details.
# find/opt/-name gongda.txt-exec ls-l {} \;
-rw-r--r--1 509 509 0 May 20:50/opt/rh/gongda.txt
-rw-r--r--1 root root 0 May 21:31/opt/rh/test/gongda.txt

16. What parameters can I use to give a security prompt every time find is followed by a system command?
#-ok

[Shell Foundation]--find Command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.