Linux Learning Command Rollup eight-file Find command find options and usage tips

Source: Internet
Author: User
Tags chmod

This chapter blog related Linux knowledge points

Different points for find versus grep:

Find is a file Search command--used to find files in a specified directory, and grep as a text Search command--to search the target file according to a user-specified text pattern, showing the rows that can be matched;

The Find option supports file wildcard characters, while the grep line supports regular expressions;

Find finds exact matches for exact lookups, but searches are slightly slower.

grep command options and regular expression types, please refer to the previous "Linux Learning commands summary Three--linux User group management, file Rights Management, Text Search command grep and regular Expressions"
For more information about the Bash feature file wildcard, refer to the "Linux Learning Command Rollup four--bash" variable. File wildcard. Piping and redirection-arithmetic operations and loops or judgment statements "

Find Path-option [-print] [-exec-ok command] {} \;

Path: Find the path, default to the current directory

Option: Find a condition that defaults to all files in the current directory

[Print] or [exec]: Find action, default to display

Find command related options

Find Path-option [-print] [-exec-ok command] {} \;

[option]:

-name "file name": Support File Wildcard Example: # Find/-name httpd.conf Find/directory name httpd.conf file

-iname "file name": Search without distinguishing file name case

-user UserName: Find the user-Hadoop files in the/FTP directory based on the owner lookup # Find/ftp-user Hadoop

-group GroupName: Search by Genus Group

-uid UID: Search by Main ID number

-gid GID: Search by group GID number

-nouser: Find files without a master # Find/ftp-nouser

-nogroup: Finding files that are not belong to a group

-type {f | d | l | p | s | b | c}: Find examples by type: # Find/etc-type D finds files of type directory under/etc/directory and displays

-size [+|-] n[k| m| G]: Find examples by file size: # find/var-size +2m

-atime [+|-] n: Find files accessed (access) based on time [more than |] n days

-mtime [+|-] n: Find files modified (modify) based on time [over |] n days

-ctime [+|-] n: Find the changed (change) file based on time [more than |] n days

-amin-mmin-cmin: Used in a similar way to-atime-mtime-ctime

-perm [+|-] Mode:

-perm 755: Indicates a strict match, that is, your file permission bit is converted to the corresponding decimal number is exactly the same as mode, then the match succeeds.

-perm-[mode]: Indicates that the two-level bits in mode that are converted to binary are 1 must match in the file permission bit. Example: #find/var-perm-355, the lookup of the permissions 355 converted to bits 011 101 101, to determine whether the file permissions are converted to two-level 1 bits is the same as the bits of the permission 1, the bit regardless of whether the match is satisfied, does not meet the mismatch; If the file permission is 0 1 101 111, 011 111 101, etc. files can be matched, and 001 101 101 will not be matched, because the second bits in the file permission differs from the bit in which the second binary of the lookup mode is 1.

find-perm +mode: The difference with-mode is that +mode just one of the 1 bits parts is matched, and +mode no matter 0 bits. Example: #find/var-perm +355, find the permissions 355 converted to bits 011 101 101, such as file Permissions 001 001 001 files can be matched, and 100 010 010 will not be matched because none of the bits is matched

Handling actions:

-print: Print on standard output

-exec COMMAND {} \; Executes the specified command on the found file

-ok COMMAND {} \; Interactive-exec

Example: #find/tmp-perm-003-exec ls-ld {} \;

Combination conditions:

-A: With, the representative conditions need to be satisfied at the same time, can be omitted

-O: Or, represents a condition that satisfies one

-not or! : No, take counter

Example: # find/usr/-not-user root-a-not-user bin-a-not-user Hadoop Lookup directory/usr/files that belong to the master not root or bin or Hadoop

Example: #find/var/-user root-group Mail Find a file in the/var directory where the owner is not a root and belongs to a group other than mail


Security context

The permissions-matching mechanism when a process accesses a file: The owner of the process is the same as the owner of the file: If the same, the process runs as the main property of the file, otherwise the owning group of the process is the same as the genus of the file, and if the same, the process runs with the permissions of the file's group; The process accesses the file with the permission of the other user of the file.

Process Owner-The initiator of the process

The genus of a process-typically a basic group of the owner of a process

File permissions:-rwsr-xr-x. 1 root root 30768 Feb 2012/usr/bin/passwd

suid--executables, when any user executes this executable program, not as the owner of the process as the user's own, but as the owner of the file as the master of the process

sgid--has a directory of Sgid, when the user creates a file in this directory, the group of the new file is not the base group to which the user belongs, but a group of directories

sticky--sticky bits, for public writable directories, users can create files that can delete their own files, but cannot delete other users ' files

Modify Permissions:

# chmod u+s File Settings suid permissions

# chmod u-s file suid permissions

# chmod Directory sgid permissions g+s directory settings

# chmod g-s Directory Sgid permissions

# chmod o+t directory set sticky bit permissions

# chmod o-t directory remove sticky bit permissions


Exercise 1: Copy the cat command to the/tmp directory, the normal user can use the/tmp/cat command to view all files that the root user has permission to view

[Email protected] ~]# cp/bin/cat/tmp/[[email protected] ~]# chmod u+s/tmp/cat [[email protected] ~]#/tmp/cat/etc/iss Uecentos Release 6.6 (Final) Kernel \ r on an \m[[email protected] ~]#/tmp/cat/etc/redhat-release CentOS release 6.6 (Fina L

Exercise 2, create a new/tmp/test directory, require both OpenStack and Docker users to have write permissions, and the group of files created in the directory are cloud groups, requiring that each user cannot delete others ' files, but can edit

[[email protected] ~]# mkdir/tmp/test[[email protected] ~]# groupadd cloud[[email protected] ~]# useradd openstack-g CLO Ud[[email protected] ~]# useradd docker-g cloud[[email protected] ~]# chown:cloud/tmp/test[[email protected] ~]# chmod O+t/tmp/test[[email protected] ~]# chmod g+s/tmp/test/[[email protected] ~]# ls-ld/tmp/testdrwxr-sr-t. 2 root cloud 4096 Mar 10:22/tmp/test


Summary of knowledge points in this chapter

#find path-option [-print] [-exec-ok command] {} \;

[option]:

-name "file name": Supports file wildcard characters to find files that match file names

-iname "file name": Search without distinguishing file name case

-user UserName: Based on owner Lookup

-group GroupName: Search by Genus Group

-uid UID: Search by Main ID number

-gid GID: Search by group GID number

-nouser: Find files without a master # Find/ftp-nouser

-nogroup: Finding files that are not belong to a group

-type {f | d | l | p | s | b | c}: Find examples by type: # Find/etc-type D finds files of type directory under/etc/directory and displays

-size [+|-] n[k| m| G]: Find examples by file size: # find/var-size +2m

-atime [+|-] n: Find files accessed (access) based on time [more than |] n days

-mtime [+|-] n: Find Files modified (modify) based on time [over |] n days

-ctime [+|-] n: Find the changed (change) file based on time [more than |] n days

-amin-mmin-cmin: Used in a similar way to-atime-mtime-ctime

-perm [+|-] Mode:

# chmod u+s File Settings suid permissions

# chmod u-s file suid permissions

# chmod Directory sgid permissions g+s directory settings

# chmod g-s Directory Sgid permissions

# chmod o+t directory set sticky bit permissions

# chmod o-t directory remove sticky bit permissions


Linux Learning Command Rollup eight-file Find command find options and usage tips

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.