Linux 0 Basics Lesson Five

Source: Internet
Author: User
Tags readable save file

Basic operations for files (bottom) file properties fileCommand syntax
>$ file file0 [file1 file2 ...]

fileThe command is used to confirm the type of file.
Linuxunder, the type of file is not usually determined strictly by file extension, especially if the executable program is often without an extension.
filecommand to view the specific type of file, and if it is an executable program, it also displays data such as its number of bits, linked information, as follows:

>$ file /bin/ls/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=d0bc0fb9b3f60f72bbad3c5a1d24c9e2a1fde775, stripped
statCommand syntax
>$ stat file0 [file1 file2]

statCommands are used to view the detailed properties of a file, similar to ls -l , but they are different, we use an example to explain each indicator.

>$ stat /bin/ls  File: '/bin/ls'  Size: 126584      Blocks: 248        IO Block: 4096   regular fileDevice: 805h/2053d  Inode: 980781      Links: 1Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2018-06-12 21:03:56.231999815 +0800Modify: 2017-03-03 02:07:22.000000000 +0800Change: 2018-02-06 15:52:08.189466243 +0800 Birth: -

FileFileName
Size: The size of the file, in bytes.
Blocks: How many blocks of data are consumed by the file. (Note that the disk is a block device, and the data is stored in blocks as the base unit, rather than in bytes.) )
IO Block: How many bytes of data each block can store.
regular file: This field is the type of file. regular fileindicates that this is an ordinary file.
Device: Device number.
Inode: The number of the file Inode . Inodeis the file system that is used on disk to save file attribute data, which contains all the file attributes except the file name. And the file name is saved in the directory file, the directory is also a document, it is used to save the file of special files.
Links: The number of hard links to the file.
Access: File permissions.
Uid: The file belongs to the owner.
Gid: File belongs to group.
Access Time: Also known atime as the time that the file was accessed. This timestamp is updated when the contents of the file are accessed.
Modify Time: Also known mtime as the time that the file was modified. This timestamp is updated when the contents of the file are modified.
Change Time: Also called ctime , indicates the file state modification time. This timestamp is updated when the state (or property) of the file is modified, such as modifying permissions, creating or deleting hard links, and so on.

File permissions chmodCommand syntax
>$ chmod [-R] mask name

chmodThe command is used to modify the file 权限 .
In a Linux system, any file can be assigned three permissions, i.e.,, 可读 可写 可执行 .
Each user has at least one of the following three identities for a file, namely 文件的拥有者 ,, 与文件拥有者在相同的用户组 其他用户 .
So what's the connection between these three permissions and three identities, and let's look at the properties of a file first.

>$ ls -l /bin/ls-rwxr-xr-x 1 root root 126584 3月   3  2017 /bin/ls

In the

file /bin/ls property, the first field -rwxr-xr-x represents the type and permissions of the file, which we'll cover in more detail later. Next look at the third and 42 fields, all root . The first root indicates which user the file belongs to and is referred to as the "owner" of the file. The second root indicates which user group the file belongs to, hereinafter referred to as the "genus" of the file. Other fields have nothing to do with permissions, and we don't introduce them here.
Next we explain the meaning of the first field, the -rwxr-xr-x is divided into four parts to interpret, as shown in the following table:
| segment | meaning |
|:-: |-|
|-| file type |
| rwx | The right of the owner Limited |
| r-x | The permissions of the genus Group |
| r-x | Permissions for other users |
Table 1 Permission segment

Figure 1 File Permissions
File type - indicates that this is an ordinary file, common file types are shown in the following table:
| identifier | type |
|:-: |-|
| d | | | - | Common File |
| l | Symbolic link |
| p | piping File |
| b | block device File |
| c | character device File |
| s | Socket File | The
Table 2 file types
File Permissions section has a total of 4 characters representing different permissions, as shown in the following table:
| identifier | octal Weight | permission |
|:-: |:-: |-|
| r | 4 | Can write |
| x | 1 | To file representation executable , folder representation to go to |
|-| 0 | Do not have this permission |
Table 3 Permission Values
Thus we can decipher the -rwxr-xr-x representation:

This is an ordinary document;
The owner of the file has a readable, writable, executable permission to it;
A user with the same user group as the file owner has readable, executable permissions on it;
Other users have readable, executable permissions.

Octal notation for file permissions

The permissions for each part of owner, group, other can calculate an octal value, and three octal values can be combined to represent the full permissions of a file.
As shown in table 3, the different permissions correspond to a weight value, and the weight value of each part of the permission is added to get the weight of this part.
Also use the permissions of the above /bin/ls file as an example, its octal permission calculation process is as follows:

(r w x) (r - x) (r - x)(4+2+1) (4+0+1) (4+0+1) = 7 5 5

So -rwxr-xr-x the corresponding octal permission is 755 .

chmodUsage of

With the knowledge of the preceding, we can look at the chmod use of the command. It can also use the form of an octal or an identifier to indicate permissions when modifying file permissions.

Permissions to modify files using octal methods

We use the following example to /bin/ls modify the file's permissions to "master readable, group writable, other user executable" using octal:

>$ $ sudo chmod 461 /bin/ls>$ ls -l /bin/ls-r--rw---x 1 root root 126584 3月   3  2017 /bin/ls

461 What is the identifier for a permission, you can calculate it based on the knowledge you learned earlier.

To modify the permissions of a file by using an identifier

To modify the permissions of a file in the form of an identifier, you specify which group of users to modify permissions, and the group uses the following table characters to indicate:
| Identifiers | Group |
| :-: | - |
| A | All Users |
| u | Owner |
| G | Genus Group |
| o | Other users |
Table 4 Group Identifiers
With the combination of the group identifier in table 4 and the permission identifier for Table 3, you can modify the permissions for the corresponding group.
In the following example, we modify the permissions of the /bin/ls file to "owner has readable, writable, executable permissions, and the group and other owners have the ability to read and execute."

>$ ls -l /bin/ls-r--rw---x 1 root root 126584 3月   3  2017 /bin/ls>$ sudo chmod u+wx /bin/ls>$ ls -l /bin/ls-rwxrw---x 1 root root 126584 3月   3  2017 /bin/ls>$ sudo chmod g-w /bin/ls>$ ls -l /bin/ls-rwxr----x 1 root root 126584 3月   3  2017 /bin/ls>$ sudo chmod g+x /bin/ls>$ ls -l /bin/ls-rwxr-x--x 1 root root 126584 3月   3  2017 /bin/ls>$ sudo chmod o+r /bin/ls>$ ls -l /bin/ls-rwxr-xr-x 1 root root 126584 3月   3  2017 /bin/ls

We can see that the corresponding permissions have changed for each modification in the previous example.
A common scenario for using identifiers to modify permissions is when creating scripts, for security reasons, when you touch create a script file with a command, the file does not have the permissions to execute, and we need to manually add the executable permissions to all users. Then it can be conveniently authorized as follows:

>$ touch test.sh>$ ls -l test.sh -rw-rw-r-- 1 user user 0 6月  27 20:42 test.sh>$ chmod a+x test.sh >$ ls -l test.sh -rwxrwxr-x 1 user user 0 6月  27 20:42 test.sh

As you can see clearly in this example, it is a+x convenient to add executable permissions for all users.

If you want to recursively modify permissions for sub-files in a directory, simply add the -R parameters as follows:

>$ chmod 777 -R /tmp/17bang>$ chmod a+x -R /tmp/17bang
chownCommand

This command is used to modify the owner of a file or directory, or to modify the genus group at the same time.

>$ chown user[:group] [-R] dir/file

With the above chmod command we know that the owner of the file and the user group that the user is in determines what permissions a user has on a file, and the chown command is used to modify the owners and genera of a file.
Examples of Use:

>$ ls -l 17bang.txt -rw-rw-r-- 1 user user 0 7月   5 18:51 17bang.txt>$ sudo chown root 17bang.txt >$ ls -l 17bang.txt-rw-rw-r-- 1 root user 0 7月   5 18:51 17bang.txt>$ sudo chown root:root 17bang.txt >$ ls -l 17bang.txt-rw-rw-r-- 1 root root 0 7月   5 18:51 17bang.txt

Note: chown commands are only available to super users.
Imagine why there is such a limit?
For a chestnut , if any user can use the chown command to modify the file owner, then it is easy to bypass the disk quota control.
Assuming that the administrator allocates a certain amount of disk space to User a , when User a has insufficient disk space, TA can chown change the owner of some files to another User Bby command, so that the account is sent to User B 's head. When User A needs to use this file, change the owner back on the line. and User B But completely do not know that they are in User a 's home directory also has the file, in vain for User a back pot.

chgrpCommand

This command is specifically intended to modify the file and directory of the genus Group, the usage and chown similar, here is no longer introduced.

Find Files

which, locate and find commands are the three common commands for finding files, which we introduce separately.

whichCommand

whichIs the shell's built-in command, typically used to find the path to a command, because it only $PATH looks for files from the path specified by the environment variable.

>$ which ls/bin/ls
locateCommand

locateCommand /var/lib/mlocate/mlocate.db queries the path of the file from the database file, so the retrieval speed is very fast.
The system automatically invokes the command at a specific time updatedb to update the database file, so the locate command does not query to the most recently added file in the system, but waits for a certain amount of time before it can be queried.

>$ locate .vimrc/home/user/.vimrc

-cParameters can be counted in quantity.
-iParameters can be ignored for case.

findCommand

findCommands are the most common and powerful of all find commands, and can be queried not only by filenames, but also by file types and properties.

>$ find [path] [expression]

We'll just give you a chestnut with a file name to find it.

>$ find /etc -name "*.conf"/etc/depmod.d/ubuntu.conf/etc/rsyslog.conf/etc/sensors3.conf/etc/ca-certificates.conf......

If you do not add a path, the default is to find the file under the current directory.

$ find -name "*.txt"./.mozilla/firefox/s7tyv1vj.default-1530780643269/SiteSecurityServiceState.txt./.mozilla/firefox/s7tyv1vj.default-1530780643269/pkcs11.txt

findThere are many other high-level uses of the command, and you can try to find the information yourself and experiment with it.

The above content is not enjoyable? More in-depth knowledge can refer to the LZ Blog: (iii) Learn UNIX environment Advanced Programming (APUE) files and directories

Linux 0 Basics Lesson Five

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.