Common Linux commands and Example 2: CP, MV, chown, chmod, and find

Source: Internet
Author: User

Name: CP
Permission: All Users
Usage:
CP [Options] source dest
CP [Options] source... directory
Note: copy one file to another or copy several files to another directory.
Parameters:
-A tries its best to copy the file status, permissions, and other information as they are.
-R if the source contains the directory name, the files under the directory are also copied to the destination in sequence.
-F if the destination already has an archive with the same file name, delete it before copying.
Example:
Copy the file AAA (already exists) and name it BBB:
CP aaa bbb
Copy all C language programs to the finished subdirectory:
Cp *. c finished

Name: MV
Permission: All Users
Usage:
MV [Options] source dest
MV [Options] source... directory
Note: move one file to another or multiple files to another directory.
Parameter:-I if the destination already has a file of the same name, first ask whether to overwrite the old file.
Example:
Change the archive AAA to BBB:
MV aaa bbb
Move all C language programs to the finished subdirectory:
MV-I *. c finished
Command name: chown
Permission: Root
Usage: chmod [-cfhvr] [-- help] [-- version] user [: Group] file...
Note: Linux/Unix is a multi-person, multi-employee operating system. All files are owned by the owner. Chown can be used to change the owner of an archive. Generally, this command is only used by the system administrator (Root). Generally, users do not have the permission to change the owner of another user's archive or change their owner to another user. Only the system administrator (Root) has such permissions.
Parameters:
User: User idgroup of the new owner: User Group of the new owner (Group)-C: if the owner of the file has indeed changed, the change action is displayed-F: if the archive owner cannot be changed, do not display the error message-H: only the link is changed, not the file that the link actually points to-V: show owner change details-R: perform the same owner change on all files in the current directory and sub-directories (I .e., change one by one in the way of delivery) -- help: show auxiliary description -- version: display version
Example:
Set the owner of the file file1.txt to user Jessie of the Users Group:
Chown JESSIE: Users file1.txt
Set all files in the current directory and sub-directory owner to user Lamport of Users Group:
Chmod-r Lamport: Users *

Command name: chmod
Permission: All Users
Usage: chmod [-CFVR] [-- help] [-- version] mode file...
Note: The file calling permissions for Linux/Unix are divided into three levels: file owner, group, and others. Chmod can be used to control how files are called by others.
Parameters:
Mode: permission setting string in the following format: [ugoa...] [[+-=] [rwxx]...] [,...], where
U indicates the owner of the file, G indicates that the owner of the file belongs to the same group, O indicates that the owner of the file belongs to other people, and a indicates that all three are.
+ Adds a permission,-Indicates canceling the permission, and = indicates a unique permission.
R indicates that the file can be read, W indicates that the file can be written, and X indicates that the file can be executed only when the file is a subdirectory or the file has been set to executable.
-C: if the permission of the file has been changed, the change action is displayed.
-F: Do not display an error message if the file permission cannot be changed.
-V: displays details of permission changes.
-R: Change the permissions of all files and sub-directories in the current directory in the same way (that is, change one by one in the way of delivery)
-- Help: displays auxiliary instructions
-- Version: displays the version.
Example: Set the file file1.txt to readable by all users:
Chmod Ugo + R file1.txt
Set file1.txt to readable:
Chmod A + R file1.txt
Set file1.txt and file2.txt as the owner of the archive, which can be written to the same group to which the archive belongs, but not to others:
Chmod ug + W, o-w file1.txt file2.txt
Set ex1.py to only the owner of the file to execute:
Chmod U + x ex1.py
Set all files and subdirectories in the current directory to be readable by anyone:
Chmod-r a + R *
In addition, chmod can use numbers to indicate permissions, such as chmod 777 file.
Syntax: chmod ABC File
Each A, B, and C is a number, indicating the permissions of the user, group, and other respectively.
R = 4, W = 2, x = 1
If you want the rwx attribute, 4 + 2 + 1 = 7;
If the RW-attribute is required, 4 + 2 = 6;
If you want the R-x attribute, 4 + 1 = 7.
Example:
Chmod A = rwx File
And
Chmod 777 File
Same effect
Chmod ug = rwx, O = X file
And
Chmod 771 File
Same effect
If chmod 4755 filename is used, the program can have root permissions.

Name: Find
Usage: Find
Instructions for use:
List the files that match the expression in the file system. You can specify a combination of different information such as the name, category, time, size, and permissions of an archive. Only the information that matches the information is listed.
Find judges the path and expression according to the following rules, and first-() in the Command column -(),! The previous part is path, followed by expression. If path is a Null String, use the current path. If expression is a Null String, use-print as the default expression.
There are more than 20 or 30 options available in expression. Here, we will only introduce the most commonly used options.
-Mount,-xdev: Only checks the files in the same file system as the specified directory to avoid listing the files in other file systems.
-Amin N: Read in the past n minutes
-Anewer file: The file that has been read later than the file.
-Atime N: files that have been read in the past n days
-Cmin N: modified in the past n minutes
-Cnewer file: The file that is updated than the file.
-Ctime N: Files modified in the past n days
-Empty: empty file-gid N or-group name: GID is N or group name is name
-Ipath P,-path P: The file whose path name is P-compliant. ipath ignores case sensitivity.
-Name,-INAME name: name of the file that matches the name. INAME ignores case sensitivity.
-Size N: the file size is n units. B indicates the block of the 512-bit tuples. C indicates the number of characters, K indicates kilo bytes, and W indicates two-bit tuples. -Type C: the archive type is C.
D: Directory
C: font Device File
B: block device Archives
P: named storage Column
F: General Files
L: Symbolic Link
S: Socket
-Pid N: file whose process ID is n
You can use () to separate the operators and use the following operations.
Exp1-And exp2
! Expr
-Not expr
Exp1-or exp2
Exp1, exp2
Example:
List all files with C extension files under the current directory and Its subdirectories.
# Find.-Name "*. c"
List all common files in the subdirectories of the current directory
# Find.-FTYPE F
List all files updated in the last 20 minutes in the current directory and Its subdirectories.
# Find.-ctime-20
Find.-Name "*"-exec grep XXX {};-print | morexxx string you want

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.