Linux-basic (7) Linux file and directory management __linux

Source: Internet
Author: User
Tags mkdir touch touch command file permissions
"7.1" Directory and Path
"7.1.2" directory-related actions

1 Special Directory list:

(1)
2 Directory Operation command, let's talk about a few common processing directory commands bar:
CDS: Transforming Catalogs
PWD: Displaying the current directory
mkdir: Create a new directory
RmDir: Delete an empty directory

"Litchi" pwd Display current directory = = Print working directory
PWD [-p] Displays the current path instead of using the link link path;


(2)
Because/var/mail is a link, connected to the/var/spool/mail
# So, with the PWD-P option, it will not be displayed with the data from the link file, but the correct full path is displayed.

"Litchi" mkdir [MP] Directory name
-M: Configuration file Permissions Oh. Direct configuration, do not need to see the default permissions (umask) of the face ~
-P: Helps you create a recursive list of the directories you need directly (including the previous level).


(3)


(4)
"Litchi" rmdir [-p] Directory name, delete directory
-P is deleted along with an empty directory at the top;


(5)
RmDir can only delete empty directories;

"7.1.3" about the variable that executes the file path: $PATH
1) Print $path variable


(6)
2 The general Identity user can not use ifconfig eth0, but the use of/sbin/ifconfig eth0 may be implemented;

3 to move LS to the/root directory, LS can not be executed, because LS file is not in the $PATH path: LS is the root directory executable file, so/root/ls can be executed;


(7)
If you want the root directory ls to execute, add the/root directory to the PATH;
Path= "$PATH":/root


(8)
"Summary"
Different identity user Default path is different, the default can run arbitrary commands are also different (such as Root and Vbird);
Path can be modified, so the general user can still query by modifying path to run some commands under/sbin or/usr/sbin;
Using an absolute or relative path to directly specify the filename of a command to run is better than searching for path;
Commands should be placed in the correct directory, the operation will be more convenient;
This directory (.) It's best not to put it in the path.


"7.2" File and directory Management
"7.2.1" View files and directories: LS



(9)

"Litchi 1" lists all the files in the home folder


(10)

"Litchi 2" on the title, do not display color, but at the end of the filename to show the type of file name represented (type)
(11)
"Litchi 3" complete rendering File modification time * (modification times)


(12)
"7.2.2" copy, delete and move: CP, RM, MV
"Litchi 1" cp-Copy files or directories


(13)
1-I: If the target file (destination) already exists, in the coverage will first ask the action to carry out (commonly used)


(14)
2-A: equivalent to the meaning of-pdr, to PDR please refer to the following instructions; (common) Copy the entire property of the file;


(15)


(16)
3 Copy all contents of/etc/directory to/tmp below


(17)
4) Use CP to create a symbolic link file and a hard link file


(18)
Add 1 Symbol link = = Shortcut;
Supplemental 2 hard links and soft links: (https://blog.csdn.net/qq_32907349/article/details/51510593)
Hard link: If an inode number corresponds to multiple file names, these files are called hard links. In other words, a hard link is the same file that uses multiple aliases (see Figure 2.hard Link is an alias for file, they have a common inode). Hard links can be created by command link or ln.
Soft Connection: The file is a soft connection if the contents of the file user data block are pointed to the path name of another file. Soft link is a common file, but the content of the data block is somewhat special. Soft links have their own inode numbers and user data blocks

5 if ~/.BASHRC is newer than/TMP/BASHRC; (cp-u parameters are often used for backup)


(19)
6)-D If the source file is a link file property (link files), then copy the link file property instead of the file itself;

(20)
The CP copies the source file, not the linked file's properties; the-d parameter can copy the properties of the linked file;

7 copy multiple files to a directory


(21)
8-A: equivalent to the meaning of-pdr, to PDR please refer to the following instructions; (commonly used)
-A can copy full permissions;


(22)
"CP Replication Instruction Summary" when replicating, you must have a clear understanding of:
Whether you need complete information about the source file.
Whether the source file is a link document (symbolic link file).
Whether the source file is a special document, such as FIFO, socket, etc.
Whether the source file is a directory.

"Litchi 2" rm-remove file or directory


(23)
1)-I asks whether to delete:


(24)
2-R Delete directory, rmdir can only delete empty directory (no file directory)


(25)


(26)
3 \RM can ignore the specified parameters of the alias, and precede the command with a backslash, you can ignore the specified options for the alias.


(27)
4 Delete a file with-the beginning

"Litchi 3" mv-Mobile file (1804022214)

(28)


(29)
"Litchi" To change the filename by rename
After the experiment, rename does not have any effect at all;

"7.2.3" gets the file name and directory name of the path

(30)

(31)

"7.3" File content lookup
1) file Content Lookup Command list:
Cat: Displays the contents of the file starting with the first line;
TAC: Starting from the last line, it can be seen that the TAC is written by cat backwards;
NL: When displayed, by the output line number;
MORE: A page of the contents of the display file;
Less is similar to more: But better still, he can turn the page forward;
Head: Only a few lines;
Tail: Only look at the tail a few lines;
OD: Reads the contents of the file in binary way;

"7.3.1" To view the contents of a file directly
1 Direct access to the contents of a file can use cat/tac/nl these commands ah.

(32)


(33)
"Litchi" cat-a: Can list some special characters rather than blank;


(34)

Add 1 if you add-N or-B, each line is preceded by a line number;
Add 2) Bird brother rarely uses cat: After all, when the file content rows more than 40 lines, the screen can not display completely;
"Litchi" TAC: Reverse display


(35)
"Litchi" nl: Add line number Print

(36)


(37)
"7.3.2" page to view
1 NL, Cat, TAC is a one-time print out the text content;
2) Page view: more or less
3 more command: One page flip (blank key) or a line of flip (enter)
Blank Key (space): The representative to turn down a page;
Enter: Represents down "line";
/string: In this display of content, search down the "string" keyword;
: F: Immediately display the file name and the number of rows currently displayed;
Q: The representative immediately left more, no longer show the contents of the file.
b or [ctrl]-b: The representative pages back, but this action is only useful to the file, the pipeline is useless.


(38)
)/input query characters, repeat query the same string, you can press n directly;

"Litchi" less (one page flip, can be forward (blank key or PageDown key) can also be back page (PageUp key))
Blank key: Flip a Page down;
[PageDown]: Flip a page down;
[PageUp]: Flip up a page;
/string: Search Down "string" function;
? string: Search up the "string" function;
N: Repeat the previous search (related to/or?). )
N: Reverse Repeat the previous search (with/or?). )
Q: Leave less this program;


(39)
"7.3.3" Data selection
1 head: Take out the front lines;
Options and Parameters:
-N: followed by a number representing the meaning of a few lines

(40)


(41)
Add) head-n negative numbers: Do not display the following negative lines, such as Head-n 100 does not show the 100th line to the final content;


(42)
2) Tail: Take out the following lines;

(43)


(44)

Add)-n + positive integer file name: The data row after the positive integer; tail-n +100/etc/man.config list 100 rows after the data;

(45)

"Supplemental" tail-f/var/log/messages: Continuous monitoring of the contents of the/var/log/messages, until the input CTRL + C will not leave tail the execution of this command;

(46)
Add 1) when "Tail-n +100/etc/man.config" is issued on behalf of the file from 100 lines will be listed, the same, in the Man.config total of 141 lines, so the 100th to 141th line will be listed. The first 99 lines will not be shown. Add 2) To example two, since/var/log/messages will have data written at any time, you want the file to have data written to the screen immediately, using the-f option, he can always detect/var/log/messages this file, The newly added data will be displayed on the screen. until you press [Crtl]-c will not leave the tail detection. Supplementary 3) Example:
Suppose I wanted to show the 11th to 20th line of/etc/man.config.
A: This should not be difficult, think about, in the 11th to the 20th line, then I take the first 20 lines, and then take 10 lines, so the result is: "Head-n 20/etc/man.config | Tail-n 10 ", so you can get the content between lines 11th through 20th. But it involves a pipeline command that needs to be addressed in the third chapter.


(47)
"7.3.4" Plain text file: OD view binaries

(48)
Litchi 1) will/usr/bin/passwd content using the ASCII way to show.


(49)
Lychee 2) will/etc/issue the contents of this file to the table with the data stored in 8 and ASCII


(50)

"7.3.5" Modify the file time or create a new file: Touch
1 each file under Linux will record a lot of time parameters, in fact, there are three major changes in time, then three of the meaning of time.
Modification Time (Mtime): When the "content data" of the file changes, it will be upgraded. Content data refers to the contents of a file, not to the attributes or permissions of a file.  Status Time (CTime): When the status of the file changes, it will be upgraded, for example, as permissions and attributes have been changed, it will be upgraded this time ah. Access Time (atime): When "The contents of this file is taken", the read times are upgraded (access). For example, if we use cat to read/etc/man.config, we will upgrade the atime of the file.
(51)
"Litchi" Touch parameter list

(52)
options and Parameters:
-A: Only changes to access time;-C: When the file is only modified, no new file is created if it does not exist;-D: You can follow the date you want to revise without the current date, or you can use--date= "date or Time"-M: Modify only the Mtime; t: Back Can be changed to the time without the current time, the format is [YYMMDDHHMM] Litchi 1) Create a new empty file and observe the time

(53)
Lychee 2) Copy ~/.BASHRC to BASHRC, assuming that the full properties are replicated and the date is checked

(54)
Add 1 LL = = Ls-l, where ll is the ls-l command alias;
Supplement 2) semicolon; : On behalf of the succession of orders issued. You can write multiple commands in one line of commands that can be run "sequentially".
Add 3) to the results of the operation, we can find that the content and attributes of the data are replicated, so the file content time (Mtime) is the same as the original file. But since this file was just created, the state (CTime) and read time handy present time. So if you want to change the time of this file. You can do this:
"Litchi" Time to modify the document;

(55)
Added) Atime Mtime changed, but CTime did not change;
The "Lychee 2" will bashrc the date of 2017/09/15 to 0202 as follows:

(56)
The date was changed in both Atime and Mtime, but CTime is the current time.

Summary of the "1" Touch Command summary:

Through the touch of this command, we can easily revise the file date and time. And you can also create an empty file. Note, however, that even when we copy a file, we copy all the attributes, but there is no way to replicate CTime this attribute. CTime can record the time when the file's recent status (status), that is, attributes and permissions, was changed. Anyway, still want to tell everyone, we usually look at the file attributes, more important is the mtime ah. We are often concerned about when the "content" of this document is changed to say ~.


"Summary 2" Touch This command is most commonly used in cases where:
Create an empty file;
Revise a file date to current (Mtime and Atime)

default permissions and Hidden permissions for "7.4" files and directories
1 The hidden properties of the profile: In addition to the basic R, W, x permissions, in the Linux ext2/ext3 file system, we can also configure other system hidden properties, this part can be configured using Chattr, and lsattr to view, The most important attribute is the feature that can be configured that cannot be modified.

Example 1) root can copy this file to Dmtsai, but the file in Dmtsai's home directory may not allow Dmtsai to read and write (because the file belongs to root). And Dmtsai can not use the chown of the reason). In addition, we are worried about covering up Dmtsai's own. BASHRC configuration file, so we can do the following actions OH:
Copy file: CP ~/.BASHRC ~DMTSAI/BASHRC
Modify properties: Chown dmtsai:users ~DMTSAI/BASHRC

Example 2 because other people cannot modify the files in the directory except Dmtsai, the permissions for the entire directory should be drwxr-xr-x. So you should do this:
Create a table of contents: Mkdir/tmp/chapter7_1
Modify Properties: Chown-r Dmtsai:users/tmp/chapter7_1
Modify Permissions: Chmod-r 755/tmp/chapter7_1

"7.4.1" file default permissions: Umask
1 Umask is: Specify the "Current user in the creation of files or directories when the default value", then how to know or configure umask it. His specified conditions are specified in the following manner:
2) Umask says: The default value needs to be lost; (dry--umask)


(57)
-S = = symbolic (symbol)
Add 1) How Umask will have four sets of numbers AH. Isn't there only three groups? That's right. The first group is special permission to use, we do not care about him, so first look at the back three groups.
Supplementary 2) The default situation is as follows:
If the user created as "file" then the default "no running (x) permission", that is, RW only two items, that is, the maximum is 666 points, the default permissions are as follows:-rw-rw-rw-; and because you need to subtract the permissions umask=0022 = = 022 = =----w--w-, so new The final access to the file is-rw-r--r--, as shown in the previous example;

If the user is created as a "directory", then X is related to whether or not this directory is accessible, so the default is all permissions are open, that is 777 points, the default permissions are as follows: DRWXRWXRWX; Due to the need to subtract the permissions umask=0022 = = 022 = =----w--w-, So the last access to the new file is Drwxr-xr-x, as shown in the previous example;

summarized below)
When creating a file: (-rw-rw-rw-)-(-----w--w-) ==>-rw-r--r--
When creating a directory: (DRWXRWXRWX)-(d----w--w-) ==> drwxr-xr-x

3 How to set up Umask. Just enter 002 on the back of the umask.


(58)
example) Assuming that your umask is 003, ask the Umask case, create the file and directory permissions for.
For:
Umask is 003, so the permission to take away is--------WX, so:
File: (-rw-rw-rw-)-(--------wx) =-rw-rw-r--
Table of Contents: (DRWXRWXRWX)-(--------wx) = drwxrwxr--

Umask summary) In the default case, root umask will remove more attributes, root umask default is 022, this is based on security considerations ~ to general identity users, usually their umask is 002, that is, retain the same group write power.

"7.4.2" File hidden properties chattr lsattr
1 But first the emphasis is that the chattr command below can only take effect on the Ext2/ext3 file system, and other file systems may not be able to support this command.
2) Let's talk about how to configure and check these hidden properties.

(59)
Note: The configuration values for a and I are common to property configurations, and many configuration values must be configured as root;
A: After configuring a, this file will only add data, not delete or modify data, only root
To configure this property.
I: This I can be very powerful. He can make a file "cannot be deleted, renamed, Configured to link also cannot
Write or add data. "is very useful for system security. Only root can configure this property;


(60)
3) lsattr (show file hidden properties)

(61)

(62)

"7.4.3" File Special permissions: SUID, SGID, Sbit

"Litchi" Look at the hidden permissions of the/USR/BIN/PASSWD, as follows:

()
How can-rwsr-xr-x s represent anything.

1) Set UID = = SUID (permissions for files)
When s This flag appears on the file owner's x permissions, such as the/usr/bin/passwd file's permission status just mentioned: "-rwsr-xr-x , this is referred to as the Set UID, which is referred to as SUID special permissions. 1.1) So what is the special function of suid to a file? Basically, SUID has such limitations and functions:
  SUID permissions are valid only for binary programs (binary program);
  The runtime has the right to run an X for the application, and
  this permission only in Valid (Run-time) in the process of running the program, and
  will have permissions for the owner (owner) of the program.
1.2) Suid Litchi (Very important Litchi): Our Linux system, all the password of the account is recorded in/etc/shadow this file, the permissions of this file is: "R--------1 root root", This means that the file is only root readable and only root can be forced to write. Since this file only has root can be modified, then bird Brother Vbird This general account user can modify their own password. You can use your own account to enter the "passwd" this command to see, hehe. The general user can certainly modify his or her password.
1.3) With the above functional description, we can know that:
  Vbird has x permissions for the/USR/BIN/PASSWD program, indicating that Vbird can run passwd;
  PASSW D is the owner of the account root,
  Vbird run passwd, will be "temporarily" to obtain root permissions,
 /etc/shadow can be vbird to run the passwd Modify.

"Note" SUID can only be used on binary program, and not enough on shell script. SUID permissions are also invalid for the directory;


2 Set GID = = Sgid permissions (for directory and file permissions)
2.0 when the S flag is SUID in the file owner's X project, that s is called Set GID in the group X, SGID. Yes, that's right. ^_^. For example, you can use the command below to observe a file with SGID permissions:

(64)
2.1 If it is to the file, SGID like the following function:
SGID is useful for binary programs;
The program is required to have X's permission for the program.
The runtime will receive support from the program group during the run.

"Litchi" For example, you can use the command below to observe a file with SGID permissions:

(65)
2.2 When a directory is configured with SGID permissions, he will have the following features:
If the user has R and X permissions for this directory, the user can enter the directory;
A valid group of users in this directory (effective group) will become a group of that directory;
Usage: If the user has W permission in this directory (you can create a new file), the user creates a new file that has the same group as this directory.
3 Sticky bit: This Sticky bit, sbit is currently only valid for the directory, for the file has no effect. Sbit's role in the directory is:
When the user has the W, x permission for this directory, that is, the Write permission;
When the user creates a file or directory under the directory, only the root and the person have the right to delete the file;
4 in other words: When a user in a directory is a group or another person's identity, and has the rights of the Directory W, which means that "a user of the directory created by any person or file can be" delete/rename/move "and so on. "However, if you add the Sbit permission entry to the a directory, you can only delete/rename/move the files or directories that you create, and you cannot delete others ' files."

"Litchi" For example, our/TMP authority is "DRWXRWXRWT", under which anyone can add and modify files in/tmp, but only the file/directory creator and Root can delete their own directories or files. This feature is also very important AH. You can do a simple test like this:
Log into the system with root and enter/tmp;
Touch test and change the test permission to 777;
Log in as a general user and enter/tmp;
Attempt to delete test this file.

5 suid/sgid/sbit Permission configuration: How to configure the file to become a SUID with SGID permissions.
So if you add a number before the three digits, the first number will represent these privileges.
4 for SUID
2 for SGID
1 for Sbit
"Litchi" If you want to change a file permission to "-rwsr-xr-x", because s in the user's permission, so is SUID, therefore, before the original 755 plus 4, that is: "chmod 4755 filename" to configure. In addition, there are big S and big T produced Oh. Refer to the example below.
Note: You must understand that SUID is not used in the directory, and Sbit is not used in the file.
"Litchi" SUID sgid permission Operation


(66)
Supplement 1) chmod 7666 test; Ls-l Test:user, group and others do not have X's operational flag (because of 666), so this S, T represents "empty". How to say. SUID means "The file has permissions to the owner of the file while it is running," but the owner of the file is unable to run and where the permissions are being used by others. Of course it's empty. ^_^
Supplemental 2 S and T permissions are based on X permissions, and if the file's user, group or others does not have X permission, its corresponding s and T permissions are empty, that is, large s or large t;

6) By means of symbolic method to deal with OH. Among them SUID is U+s, and SGID is G+s, Sbit is o+t. Take a look at the following examples:


(67)

(68)
"7.4.4" View file type: Files
1 If you want to know the basic data of a file, such as ASCII or data file, or binary, and if there is no use of dynamic function library (share libraries) and so on information, you can use the command of file to review Oh.


(69)
"7.5" Command and file query
Query for "7.5.1" script filename
1 The full file name of the command is placed where. To find by which or type;
2) which: find the execution document;


(70)
"Litchi" is as follows:

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.