[Shell, 1] chmod and find commands:

Source: Internet
Author: User
Tags uppercase letter

Command: 1. Modify the chmod permission:

The general format of the chmod command is: chmod [who] operator [permission] filenamew h o. The meaning of o is: u file master permission. G. O other user permissions. A. All users (file owner, users in the same group, and other users ). Operator meaning: + add permissions. -Cancel the permission. = Set permissions. Permission: r read permission. W write permission. X execution permission. S file owner and group s e t-I D. T viscosity position *. L lock the file to make it inaccessible to other users. U, g, o operations on file owners, users in the same group, and other users. Example: chmod a-x myfile rw-Revoke the execution permission of all users chmod og-w myfile rw-r -- r-Revoke the write permission of the same group of users and other users chmod g + w myfile rw-r--grant the write permission to the same group of users chmod u + x myfile rwx rw-r--grant the file owner the execution permission chmod go + x myfile rwx rwx r-x grant the same group of users and other users the execution permission chmod 666 rw-grant all users the read and write permissions chmod 644 rw-r -- r--grant all file owner's read and write permissions, all other user read permissions chmod 744 rwx r -- r-grant the file owner read, write, and execute permissions, all other users read chmod 664 rw-r--grant the file owner and the same group users read and write permissions, read Permission of other users chmod 700 rwx --- grant the file owner the read, write, and execute permissions chmod 444 r -- r--grant all users the read permission
2. find command: ①. Search by file name and use the name option.
Regardless of the current path, if you want to find a file with a file name *. t x t in your root directory $ HOME, use ~ As the pathname parameter, the Tilde ~ Represents your $ h o m e directory. $ Find ~ -Name "*. txt "-print: search for all '* in the current directory and subdirectory '*. t x t' file, which can be used: $ find. -name "*. txt "-print: find the file name starting with an uppercase letter in the current directory and subdirectory. You can use: $ find. -name "[A-Z] *"-print: $ find/etc-name "host *"-print to search for files in the $ h o m e directory, you can use: $ find ~ -Name "*"-print or find.-print: to enable the system to run at high load, search for all files from the root directory. If you want to keep a good impression on the system administrator, you 'd better consider it before doing so! $ Find/-name "*"-print if you want to find the file name in the current directory starting with two lower-case letters, followed by two numbers, and finally *. t x t file, the following command returns the name a x 3 7. t x t file: $ find. -name "audio a-z1_a-z1_1_0--91_0--91_.txt"-print
② Search by File Permission mode: Use the perm option:
To find a file with a permission of in the current directory, that is, the file owner can read, write, and execute the file. Other users can read and execute the file: $ find. -perm 755-print: If you want to find all files that can be read, written, and executed by all users in the current directory (Be careful with this situation ), we can use the-p e r m option of the f I n d command. Add a horizontal bar before the octal digit -. In the following command,-p e r m indicates searching by File Permission, the expression '0 0 7' is exactly the same as that used in the absolute mode of the c h m o d command. $ Find.-perm-007-print
③ Ignore a directory and use the-prune option:
If you want to ignore a directory when searching for a file because you know that there is no file in the directory, you can use the-p r u n e option to specify the directory to be ignored. Be careful when using the-p r u n e option, because if you use the-d e p t h option at the same time, the-p r u n e option is ignored by the f I n d command. If you want to search for files in the/a p s directory, but do not want to search for files in the/a p s/B I n directory, you can use: $ find/apps-name "/apps/bin"-prune-o-print
④ Search by file owner: Use the user and nouser options:
If you want to search for a file by file owner, you can give the corresponding user name. For example, to find a file whose owner is d a v E in the $ h o m e directory, you can use: $ find ~ -User dave-print: In the/e t c directory, find the file whose owner is u c p: $ find/etc-user uucp-print in order to find files that have been deleted by the owner account, you can use the-n o u s e r option. In this way, you can find files with no valid account in the/e t c/p a s w d file of the owner. When you use the-n o u s e r option, you do not need to give a user name. The f I n d command can complete the corresponding work for you. For example, to search for all such files in the/h o m e directory, you can use: $ find/home-nouser-print
⑤ Search by file group and use the group and nogroup options:
Like the u s e r and n o u s e r options, the f I n d command also has the same options for the user group to which the file belongs, to search for files belonging to the c t s user group under the/a p s directory, you can use: $ find/apps-group accts-print to find all files in the user group that do not have a valid value, you can use the n o g r o u p option. The following f I n d command looks for such a file from the root directory of the file system $ fine/-nogroup-print
6. Select the type option based on the file type:
If you want to find all directories in the/etc directory, you can use: $ find/etc-type d-print to find all types of files except directories in the current directory, available: $ find .! -Type d-print: To find all symbolic link files under the/e t c directory, you can use: $ find/etc-type l-print
7. Use the-mtime option to search for files based on the Change Time:
You can use the m t I m e option to search for files based on the change time. If the system suddenly has no available space, it is very likely that the length of a file will grow rapidly during this period, then you can use the m t I m e option to find such a file. Use minus signs-to limit the files whose change time is earlier than n days ago, and use the plus sign + to limit the files whose change time is earlier than n days ago. To search for files whose modification time is less than 5 days in the root directory of the system, you can use: $ find/-mtime-5-print to search for files whose change time was earlier than 3 days in the/v a r/a d m directory, you can use: $ find/var/adm-mtime + 3-print
To execute the shell command:
After matching some files, you may want to perform some operations on them. Then you can use the-e x e c option. Once the fi n d command matches the corresponding file, you can use the command in the-e x e c option to operate on it (in some operating systems, only the-e x e c option is allowed to execute commands such as l s or ls-l. ). The e x e c option is followed by the command to be executed, followed by a pair of characters {}, a space, a \, and a semicolon. To use the ls-l command to list the matched files, you can place the ls-l command in the-e x e c option of the f I n d command. For example:
Find.-type f-exec ls-l {}\; To find and delete files whose modification time is earlier than 5 days in the/l o g s directory, you can use: $ Find logs-type f-mtime + 5-exec rm {}\;Usage of commands such as seek, find, and xargs: It seems that xargs has the same function as exec.
The following example finds the memory information dumping file (core dump) in the entire system, and then saves the result to/tmp/core. in the log file: $ find. -name "core"-print | xargs echo "">/tmp/core. in the example of log, search for all files with read, write, and execution permissions under the/a p s/a u d I t directory, and revoke the write permissions: $ find/apps/audit-perm-7-print | xargs chmod o-w in the following example, we use the g r e p command to search for the word d e v I c e in all common files: $ find/-type f-print | xargs grep "device" in the following example, we use the g r e p command to search all common files in the current directory for the word d B O: $ find. -name \ *-type f-print | Xargs grep "DBO" NOTE: In the preceding example, \ is used to cancel the special meaning of * in shell of the 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.