Linux File & Directory Management & Links

Linux files

Basic properties

             The Linux system is a typical multi-user system. In order to protect the security of the system, different users have different statuses and permissions. The Linux system has different regulations on the permissions of different users to access the same file (including directory files).

             You can use the command: ll or ls –l to display the attributes of a file and the user and group to which the file belongs, as shown in the figure:

Detailed analysis of the command: the content displayed in ls -l

             When using the command: ll or ls –l, the attributes of a file and the user and group to which the file belongs will be displayed. The detailed analysis of this information is as follows:

drwxr-xr-x.  3 root root      101 9月   8 01:52 abrt

As shown in the figure: Among the 10 characters              from left to right   , the first character determines the file type. The next characters, grouped in groups of three, are all combinations of the three parameters of rwx.

             The  first  character determines the file type. The types are as follows:

  • d represents directory;
  • - represents a file;
  • l represents a link file;
  • b represents a storage-available interface device (random access device) in the device file;
  • c represents the serial port device in the device file, such as keyboard and mouse (one-time reading device).

             Bits  2-4  confirm that the owner (the owner of the file) has permissions for the file. Among them, r represents readable (read), w represents writable (write), and x represents executable (execute). It should be noted that the positions of these three permissions will not change. If there is no permission, it will be displayed as a minus sign - .

             Bits  5-7  determine that the group (users in the same group as the owner) has permissions for the file.

             Bits 8-10 determine what other users have permissions to the file.

 The meaning of the characters after the 10th character can be referred to the picture above and will not be repeated              here  .

The owner and group of the file

             In Linux systems, files have a specific owner, which is the user who has ownership of the file. At the same time, users are classified by groups, and a user belongs to one or more groups. Users other than the file owner can be divided into "users in the same group as the file belongs to" and "other users". Therefore, the Linux system stipulates different file access permissions according to "file owner", "users in the same group of file owners" and "other users".

drwxr-xr-x.  3 root root      101 9月   8 01:52 abrt

             In the above example, the abrt file is a directory file. The owner and group are both root. The owner has readable, writable, and executable permissions; other users in the same group as the owner have readable and executable permissions. permissions; other users also have readable and executable permissions.

             But for the root user, under normal circumstances, file permissions have no effect.

Change file attributes

             The following two commands are usually used in Linux to modify the user and permissions of a file or directory:

  • chown (change owner): Modify the user and group it belongs to.
  • chmod (change mode): Modify user permissions.

chgrp(change group)

             Used to change the group to which a file or directory belongs. grammar:

chgrp [ * ] [--help] [--version] Group name file or directory 

chgrp [ * ] [--help] [--reference=<reference file or directory>] [--version] file or directory 

[*]  Parameter description:

  • -c or --changes : The effect is similar to the "-v" parameter, but only the changed parts are reported.
  • -f or --quiet or --silent : Do not display error messages.
  • -h or --no-dereference : Only modify the symbolic link file without modifying any other related files.
  • -R or --recursive : Recursive processing, processing all files and subdirectories in the specified directory together.
  • -v or --verbose : Display the instruction execution process.

[ --help ]  Parameter description:  --help : online help.

[--version]  Parameter description: --version : Display version information.

[--reference=<reference file or directory>]  Parameter description: Set all the groups of the specified file or directory to be the same as the group of the reference file or directory.

             Example 1, change the group attribute of the file hello.cpp in the /etc/chen directory:

             Example 2, change the group attribute of the file based on the specified file:

 
 

chown(change owner

             Commands to set the file owner and file association group. grammar:

chown [ * ] [--help] [--version] owner name file name
chown [ * ] [--help] [--version] owner name: group name file name

 [*]  Parameter description: 

  • -c: Display information about the changed part
  • -f : ignore error messages
  • -h : Repair symbolic links
  • -v: display detailed processing information
  • -R: Process all files in the specified directory and its subdirectories

[ --help ]  Parameter description: --help: display auxiliary instructions

[ --version ]  Parameter description: --version : display version

             For example, enter the /etc/chen directory and change the owner and group of hello.cpp to the account chen:

chmod(change mode):

             Change the 9 basic attributes of Linux files (  the read (read)/write (write)/execute (execute)  permissions  of the three identities of  owner/group/others) . There are two ways to set attributes, one is numbers and the other is symbols.

Numeric type changes file permissions

             The permission characters of the file are:  - rwx rwx rwx  . These nine permissions are in groups of three! Therefore, numbers can be used to represent each permission. The score of each permission is: r(4), w(2), x(1), and the permissions (r/w/x) scores of each group are accumulated. For example, when the permissions are: -rwxrwx--- , then the score is:

  • owner = rwx = 4+2+1 = 7
  • group = rwx = 4+2+1 = 7
  • others= --- = 0+0+0 = 0

             Therefore, when using numbers to change file permissions, the permission number xyz of the file is  770. At this time, the syntax of the chmod command is:

 chmod [ * ] [--help] [--version] xyz file or directory

xyz  : Permission attribute of numeric type (for example: 770).

[*]  Parameter description:

  • -c: If the file permissions have indeed been changed, the change action will be displayed.
  • -f : Do not display an error message if the file permissions cannot be changed
  • -v: Display details of permission changes
  • -R: Make the same permission changes to all files and subdirectories in the current directory (that is, change them one by one recursively)

[--help] Parameter description: --help: display auxiliary instructions

[--version] Parameter description: --version: display version

             For example, if all permissions of the file hello.cpp in the directory /etc/chen are set to enabled, then the permission score becomes [4+2+1][4+2+1][4+2+1 ]=777:

Symbol type changes file permissions

             Use  u, g, and o  to represent the permissions of three identities: user, group, and others. In addition,  a  represents  all , that is, all identities.

             The permissions for reading, writing, and executing can be written as r, w, x. You can also use + (add permissions), - (cancel permissions), = (uniquely set permissions) to remove permissions without changing other existing permissions. As shown below:

             For example, the permissions of the file hello.cpp in the directory /etc/chen are set to:  -rwxr-xr--  . You can use the command: chmod u=rwx,g=rx,o=r hello.cpp to set:

             For example, use -(remove) to remove executable permissions for everyone without changing other existing permissions:

Linux directory management

             The directory structure of Linux is a tree structure, and the top-level directory is the root directory /. Other directories are added to the tree by mounting and removed by unmounting.

Absolute path and relative path

  • Absolute path: The path is written starting from the root directory /, for example: /etc/chen directory.

  • Relative path: The path is written in a way that does not start with /. For example, when going from /usr/share/doc to /usr/share/man, it can be written as: cd ../man. This is the way of writing a relative path.


Common commands for working with directories

             Several common commands for processing directories:

  • ls (list files): List directories and file names
  • cd (change directory): switch directory
  • pwd (print work directory): displays the current directory
  • mkdir (make directory): Create a new directory
  • rmdir (remove directory): delete an empty directory
  • cp (copy file): copy a file or directory
  • rm (remove): delete files or directories
  • mv (move file): Move files and directories, or modify the names of files and directories

             Note: You can use: man [command]  to help view the usage documentation of each command, such as: man cp.

ls

              List directories. grammar:

ls [options] directory name
ls [--color={never,auto,always}] directory name
ls [--full-time] directory name

[Option]  parameter description:

  • -a: List all files, including hidden files (files starting with .) (commonly used)
  • -d: Only list the directory itself, not the file data in the directory (commonly used)
  • -l: long data string list, including file attributes and permissions, etc.; (commonly used)
  • -r Sort in reverse order.
  • -t Sort by time information.
  • -u Sort (when using the "-t" option) or list (when using the "-l" option) using the most recent access time instead of the most recent modification time.

[ --color={never,auto,always} ]  Parameter description: Specify whether to use color to distinguish file categories.

[ --full-time ]  Parameter description: List the full time instead of using the standard abbreviation.

              For example: List all files in the directory /etc (including attributes and hidden files)

 
 

cd

              Command to change working directory. grammar:

 cd [relative path/absolute path]

              For example, use the absolute path to switch to the /etc/chen/documents directory

              Use a relative path to switch to the documents directory

pwd

              pwd is the abbreviation of Print Working Directory and displays the current directory. grammar:

pwd  [-P]

[ -P  ]  Parameter description: Display the actual path instead of using the link path.

              For example, the command: pwd displays the current actual working directory, not the directory name of the linked file itself: 

              And /var/mail is a link file, linked to /var/spool/mail. Therefore, after using the command: pwd -P, the data of the link file will not be displayed, but the correct full path will be displayed.

mkdir

              mkdir is the abbreviation of make directory, which creates a new directory. grammar:

 mkdir [options] directory name

[Option  ]  Parameter description:

  • -m: Configuration file permissions;
  • -p: Create the required directory (including the upper-level directory) recursively!

              For example, create several new directories under the directory /etc/chen:

              Add option -p to create multi-level directories

              Create a directory with permissions  r-xr-xr-x  . Requires option -m. If -m is not added to force configuration properties, the system will use the default properties.

is rm

              Delete empty directories. grammar:

rmdir [options] directory name

[Option  ]  Parameter description:

  • -p: Starting from this directory, delete multiple levels of empty directories at one time

              For example, delete the empty directory test under the directory /etc/chen/tmp and the non-empty directory test1.

cp

              Copy files and directories. grammar:

cp [option] source file (source) destination file (destination)

cp [options] source1 source2 source3 .... directory

[Option  ]  Parameter description:

  • -a: When copying, keep the structure and attributes of the file as much as possible. (But do not keep the directory structure) Equivalent to -dpR. (commonly used)

  • -d: If the source file is an attribute of a link file, copy the link file attributes instead of the file itself;

  • -f: means force. If the target file already exists and cannot be opened, remove it and try again;

  • -i: If the target file (destination) already exists, it will first ask about the progress of the action when overwriting (commonly used)

  • -l: Create a link file for a hard link instead of copying the file itself;

  • -p: Copy the file together with its attributes instead of using the default attributes (commonly used for backup);

  • -r: Recursive continuous copying, used for directory copying behavior; (commonly used)

  • -s: Copy into a symbolic link file, also known as a "shortcut" file;

  • -u: If the existing target file has the same or newer modification time, do not copy the directory (file).

              For example, copy test.c in the /etc/chen directory to /home/chen and name it hello.c

rm

              Remove a file or directory. grammar:

rm [options] file or directory

[Option  ]  Parameter description:

  • -f: means force, ignore files that do not exist, and no warning message will appear;
  • -i: Interactive mode, the user will be asked whether to take action before deletion
  • -r: Recursive deletion! Most commonly used in directory deletion! Use with caution!

              For example, delete hello.c under /home/chen! Adding the -i option will display the query:

mv 

              Move files and directories, or change their names. Syntax: source file

mv [options] source file (source) destination file (destination)

mv [options] source1 source2 source3 .... directory (directory)

[Option  ]  Parameter description:

  • -f: force means force. If the target file already exists, it will be overwritten directly without asking;
  • -i: If the destination file (destination) already exists, you will be asked whether to overwrite it!
  • -u: If the target file already exists and the source is relatively new, it will be upgraded (update)

              For example, move the tmp in the /home/chen directory to /etc/chen  

 
 

              Move hello.cpp in the /etc/chen directory to /home/chen and rename it to test.cpp

Linux content viewing

              Use the following command to view the contents of the file:

  • cat: displays the file contents starting from the first line
  • tac: Display starting from the last line
  • nl: output line number while displaying
  • more: Display file content page by page
  • less: similar to more, and can turn pages forward!
  • head: only look at the first few lines
  • tail: only look at the last few lines

              You can use the command: man [command]  to view the usage documentation of each command, such as: man cp.

cat

              Display the file contents starting from the first line. grammar:

cat [options] [file]

[Option  ]  Parameter description:

  • -A: Equivalent to the integration option of -vET, show-all, which can list some special characters instead of just blanks;
  • -b: List line numbers, only display line numbers for non-blank lines, blank lines are not marked with line numbers!
  • -E: Display the ending line break byte $;
  • -n: Print line numbers, and blank lines will also have line numbers, which is different from the -b option;
  • -T: Display the [tab] symbol as ^I;
  • -v: All control characters except LFD and TAB are displayed in ^ and M- notation.

              For example, view the contents of the /etc/profile file:

 
 

tac

              The tac and cat commands have exactly the opposite effect. The file contents are displayed starting from the last line. For example, view the contents of the /etc/profile file:

nl

              Show line number. grammar:

nl [options] [file]

[Option  ]  Parameter description:

  • -b: Specify the way to specify the line number. There are two main ways:
    -ba: Indicates that the line number is also listed regardless of whether it is a blank line (similar to cat -n);
    -bt: If there is a blank line, the empty line Do not list line numbers (default);
  • -n: There are three main ways to list line numbers:
    -n ln: The line number is displayed at the far left of the screen;
    -n rn: The line number is displayed at the far right of its own column without adding 0 ;
    -n rz: The line number is displayed at the far right of its own column, and 0 is added;
  • -w: Number of digits occupied by the line number field.

              For example, use nl to list the contents of /etc/profile

 
 

more

              Turning the pages. For example, view the contents of the /etc/profile file:

 
 

              The bottom will display: --More--(**%). More is still running, which means it is waiting for your command. There are several keys you can press at this time:

  • Space key (space): represents turning down a page;
  • Enter: represents scrolling down "one line";
  • /String: means searching for the keyword "string" in the displayed content;
  • :f: Immediately display the file name and the number of lines currently displayed;
  • q: means to leave more immediately and no longer display the content of the file.
  • b or [ctrl]-b: means turning pages back, but this action is only useful for files, not pipelines.

less

              Turning page by page, the following example outputs the contents of the /etc/chen/test.c file:

[chen@localhost chen]$ less /etc/chen/test.c 

              The commands that can be entered when less is running are:

  • Space key: Scroll down one page;
  • [pagedown]: Scroll down one page;
  • [pageup]: Scroll up one page;
  • /String: The function of searching downward for "string";
  • ?String: The function of searching upward for "string";
  • n : Repeat the previous search (related to / or ?!)
  • N: Repeat the previous search in reverse (related to / or ?!)
  • q: Leave the less program;

head

              Take out the first few lines of the file. grammar:

head [-n number] file

[-n number]  parameter description:

  • -n: followed by a number, indicating how many lines to display. By default, the first 10 lines are displayed!

              For example, output 4 lines of the contents of the /etc/chen/test.c file:

 
 

tail

              Take out the last few lines of the file. grammar:

tail [-n number] file

[-n number]  parameter description:

  • -n: followed by a number, indicating how many lines to display
  • -f: Indicates that the following file names will be continuously detected. The detection of tail will not end until [ctrl]-c is pressed.

               For example, output the following 4 lines of the file /etc/chen/test.c:

 
 

Linux link

               There are two types of Linux links, one is called Hard Link and the other is called Symbolic Link. By default, the ln command produces hard links.

Hardwired

               Hard connections refer to connections through index nodes. In the Linux file system, files stored in disk partitions are assigned a number regardless of their type, called an Inode Index. In Linux, multiple file names pointing to the same index node exist.

For example: A is a hard link to B (A and B are both file names), then the inode node number in A's directory entry is the same as                the inode node number in B's directory entry, that is, one inode node corresponds to two different files . name, two file names point to the same file, A and B are completely equal to the file system. Deleting either one will not affect access to the other.

               The function of hard link is to allow a file to have multiple valid path names, so that users can establish hard links to important files to prevent "accidental deletion". The reason for this is as mentioned above, because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the index node itself and other connections. Only when the last connection is deleted, the data blocks of the file and the directory connections will be released. In other words, the condition for the file to be truly deleted is that all hard-linked files related to it are deleted.

Soft connection

               Another type of connection is called a symbolic link (Symbolic Link), also called a soft link. Soft link files have Windows-like shortcuts. It is actually a special file. In a symbolic link, the file is actually a text file that contains the location information of another file.

For example: A is a soft link to B (A and B are both file names). The inode node number in A's directory entry is different from                the inode node number in B's directory entry. A and B point to two different inode, which in turn points to two different data blocks. But the data block of A stores only the path name of B (you can find the directory entry of B based on this). There is a "master-slave" relationship between A and B. If B is deleted, A still exists (because the two are different files), but points to an invalid link.

Example

               It can be seen from the above results that the inode node of the hard link file f2 is the same as the original file file1, both are 3291628, but the inode node of the symbolic link file is different.

               Note: The -i parameter displays the inode node information of the file.

 
 

               It can be seen from the above test that when the original file file1 is deleted, the hard link file2 is not affected, but the symbolic link file3 is invalid.

Guess you like

Origin blog.csdn.net/weixin_60461563/article/details/133031176