cool hit counter Linux Directory and File Related Knowledge Organizer_Intefrankly

Linux Directory and File Related Knowledge Organizer


Linux file management is the foundation of learning Linux, read Uncle Bird's Linux books, and organize Linux directory and file related knowledge as follows.

Note: The test environment for the text is CentOS 7.

  1. File Properties

1.1 Basic concepts

Owner: In Linux, each user has his or her own home folder, the /home/user folder, and user is the specific username.

Group: In order to facilitate cooperation and communication between users, users can form a small group, also known as a group. Here everyone can share some files, which are the ones that belong to the whole group.

Other: For users outside the group, they are treated as others.

The special user, root, is a god in Linux because it can do everything, including of course viewing files in any user's home folder, whereas a normal user cannot view the root user's home folder /root.

On Linux systems, by default, all accounts and general users, as well as the root user, are recorded in the /etc/passwd file. The individual passwords are recorded in the /etc/shadow file, but they are encrypted with an algorithm that makes it impossible for even the root user to know your plaintext password (check the encryption algorithm with authconfig --test |) (grep hashing command). In addition, all Linux group names are recorded in the /etc/group file.

1.2 Document properties

Log in as root, cd to the /root directory, and type ls -al to list all files (including hidden files) and attributes in the directory. ls is short for list, a is the first letter of the attribute attribute, and l is also the first letter of the list.

image.png

As you can see above, the attributes of the file are divided into 7 columns.

The first column is the type and permissions of the file. There are ten characters, the first character is the type of the file, d indicates a directory (dictionary), - indicates a normal file, and the remaining nine characters are the permissions of the file. Permission bits can be divided into three groups, the first group is the file owner's permissions, r means read, w means write, x means execute; the second group is the permissions of the rest of the group, - means no permissions for that bit; the third group is the permissions of others outside the group, file permission control in Linux is achieved through these nine permission bits.

Note: rwx These three mean different things for files and directories.

For directories, r means that you have this permission to read the structure list of the directory, that is, to view the file names and subdirectory names under the directory, but only the file names, whether you can view the file contents, but also depends on whether there is a file readable (r) permission; w means that you have this permission to change the structure list of the directory, that is, to change the file names or subdirectory names under the directory, specifically for new files or directories, delete existing files or directories, rename files or directories, move files or directories. x means that the user with this permission can enter the directory, but if they cannot enter the directory, they cannot read the contents of the files in the directory, but if they have the r permission of the directory, they can view the files and the directory list, note the difference between the two, x is the contents of the file and r is the name of the file. So if you want to open a directory for anyone to browse, you should give at least r and x permissions.

For files, r means that you have this permission to read the actual contents of the file; w means that you have this permission to modify the contents of the file, but not to delete the file, which is related to the permission w of the directory where the file is located; x means that you have this permission to execute the file, such as some command files.

image.png

The second column shows how many linked files this file has, so imagine how many shortcuts there are to a file under Windows.

The third column is the account of the owner of this file.

The fourth column is the group name for this file, and the account name is the same as the group name by default.

The fifth column is the size of this file, and the default unit is bytes (Byte).

The sixth column is the date this file was created or most recently modified, in month day time format, or just the year if the file was modified a long time ago. To display the full date of the file, use the ls -l --full-time command.

The seventh column is the filename of this file, which is preceded by . means this file is a hidden file.

1.3 Changing the attributes and permissions of a file or directory

The root user can change the attributes of any file or directory, while the normal user can only change the attributes of files or directories under their home folder.

1.3.1 chmod Change file and directory permissions

Command Format: chmod [-R] permission write File or directory, R is an optional parameter, adding this parameter means recursively change the permissions of all subdirectories or files under the directory at the same time.

Two ways.

  • Number Type

Use numbers to represent rwx,r->4,w->2,x->1。 For three sets of competencies(Owner,Group,Other) set up, Just add up the three numbers, No one has permission, The cumulative number will be 0。

  • Symbol Type

Use the symbols u, g, and o for User (Owner), Group, and Other, and set permissions by assigning (=), adding (+), and removing (-) rwx to these three symbols, in addition to using a for All to set permissions.

Example.

Using the root user, create the test file in the root directory of the system and view the default attributes set for the test folder as -rw-r--r-- 1 root root date test.

 cd / # Switch to the root directory
 touch test # The default attribute creates the test file
 ls -l test # View the test folder properties

Then log in to the system using another normal user and open the file, but since it is only read-only, you cannot edit the test blank file.

Using the root user, change the permissions on this file so that users in the same group (Group) and other users (Other) can edit this file.

Order 1.

 chmod 746 test # Modify test file attributes

Order 2.

 chmod g=rw,o=rw test # Use reassignment to set permissions, also write g and o together chmod go=rw test

Order 3.

chmod g+w,o+w test # Set permissions by adding new permissions, or by concatenating g and o as chmod go+w test

Order 4.

 chmod a+w test # Use a directly to add w permissions to all users

There are various commands to set permissions to a file, just choose one. This changes the permissions on the test file so that other users can edit the file.

1.3.2 chown Change file and directory owners

Command Format: chown [-R] username[:groupname] file or directory, R is an optional parameter, adding this parameter means recursively change the owner of all subdirectories or files under the directory at the same time. The group name can also be changed with the chown command, and of course with the chgrp command below.

Example.

If the root user copies a copy of the test file to another normal user, wzw, after the copy is made, the file owner and group in the file attributes of the test copy are still root root, which means that the copy operation copies the file attributes of the original file as well. Then wzw, the normal user, does not have all the operations of this copy file when he receives it, such as resetting the permissions of this copy file, so the root user has to change the owner of this file to wzw before giving it to wzw, the user.

 cp test test1 # Copy test file to test1 file
 ls -l test1 # View the properties of the copied test1 file, the file owner and group are still root root
 chown wzw test:test test1 # Change both the test1 file owner and the group to the wzw user
 ls -l test1 # Check the properties of the test1 file again, the file owner and group have been changed to wzw wzw

Modifying groups in file attributes can also be done with the following chgrp command.

1.3.3 chgrp Changing file and directory groups

Command Format: chgrp [-R] Group Name File or directory, R is an optional parameter, adding this parameter means recursively changing the owners of all subdirectories or files under the directory at the same time.

Example.

Set the above copied file test1 group to the already existing root group and the non-existing test group. In Linux, when a new user is created, a group with the corresponding user name is also created by default.

 chgrp root test1 # Set the group attribute of the test1 file to the root group
 ls -l test1 # Check the properties of the test1 file, you can see that the group has been changed to root, but the owner of the file remains the same as wzw
 chgrp test test1 # Set the group attribute of the test1 file to the test group, which will indicate that test is an invalid group

chown harmony chgrp All commands can be modified File Properties Cluster of, nevertheless chown You can also set the owner of the file, That's the difference between the two。

  1. Directory Configuration

2.1 FHS Standard

Since there are so many distributions of Linux, apart from CentOS, which is used for text, the common ones are Ubuntu, Debian, RedHat and so on. If every distribution had a different system directory structure, it would be necessary to learn every version of Linux, which is certainly something we don't want, so the Linux community has developed the File System Hierarchy Standard, or FHS standard for short. The FHS standard defines the main directories and directory contents in the Linux operating system, and most Linux distributions follow the FHS standard and maintain the FHS requirements. Today the FHS standards are also under constant revision.

2.2 Description of the table of contents

    Table of Contents Description                   
    / root directory of the entire file system hierarchy             
   /bin directory of executable files (commands), which can be used by root and normal users, such as cat, chmod, mkdir and other common commands
  /sbin directory of commands needed to boot, repair, and restore the system. These commands can only be used by root to "set up" the system; other users can only use them to "query" the system.
  /boot boot directory, e.g. Linux kernel file name is vmlinuz, boot manager grub
/dev Accesses the device directory. In Linux, any device exists in text form in this directory, such as cpu, disk, etc.
   /etc The main system configuration file directory, such as the password information for the user mentioned earlier.       
   /lib system function library directory                 
  /media removable device directory, e.g. CD-ROM, DVD            
   /mnt Temporarily mounted device directory                
   /opt directory for third-party software, such as the KDE desktop management system         
   /run The directory where the information generated after the system is booted is placed, and memory emulation can be used.       
   The data directory to be fetched by the /srv service, e.g. WWW service, FTP service      
   /tmp is a directory where files are temporarily stored by the general user or an executing program, and can be accessed by anyone.
   /usr directory where software resources are located, similar to C:Windows and C:Program files on Windows systems to install some system default software or general software
 /usr/bin Directory of commands that the user can use               
 /usr/lib is similar to the /lib directory               

/usr/lib64 is similar to the /lib64 directory /usr/libexec Certain executable files or script directories that are not commonly used by the average user, such as the operation command under the X graphical window /usr/local The installation directory of the software /usr/sbin System commands that are not required for normal system operation, such as service commands for some network service software, basically the same function as /sbin /usr/share Read-only architecture for data files and shared files directory /usr/games The directory where data related to game comparison is placed /usr/include The header and include directories for languages such as c/c++ are used when we install software as *.tar.gz. /usr/src General source code recommended directory /home User home folder directory. When a new user account is not created, a folder with the same name as the user is created in that directory /lib64 is used to store binary libraries in a different format than /lib, such as /lib64 functions with 64-bit support /root The system administrator's (root) home folder /proc virtual filesystem directory, data is placed in memory and does not take up any hard disk space, such as system core, trip information, network status The /sys virtual file system directory, similar to /proc, also does not take up hard disk space, and mainly records core and system hardware information. /var directory of frequently changing files, such as cache, login files, and files generated during software operation, such as database files /var/cache The cache file directory is generated during the operation of the application itself /var/lib The directory where data files are placed during program execution /var/lock File lock directory, such as shared lock, can only read files, not modify them The directory where the /var/log login file is placed /var/mail The directory where personal email addresses are placed /var/run is similar to /run /var/spool queues data directories that are waiting to be used by other programs, such as job scheduling data

In CentOS 7, directories are organized differently than in previous versions, with many directories that were originally placed in the root (/) directory now in the /usr directory, and then only the links to the directories are set in the root directory. These include the following.

  • /bin -> /usr/bin
  • /sbin -> /usr/sbin
  • /lib -> /usr/lib
  • /lib64 -> /usr/lib64
  • /var/lock -> /run/lock
  • /var/run -> /run

Doing this with hides file paths, increases permissions security, and has the benefit of saving storage.

Combined with the directories above, the directory tree structure of Linux is shown below.

image.png

  1. File and Directory Management

3.1 Special directories and paths

3.1.1 Special catalogue

  • .: indicates the current directory, or you can use ./ to indicate it.
  • ... : indicates the previous level of the directory, or you can use ... / to indicate.
  • -: Indicates the previous working directory.
  • ~: indicates the current user's home folder, i.e. /home/user name.
  • ~user name: Indicates the home folder of the user.

3.1.2 Special paths

  • absolute path (computing)

A path representation that starts at the root of the system, such as /home/wzw/Desktop

  • relative path (computing)

For example, if you are currently in /home/wzw/Download directory and want to switch to /home/wzw/Desktop directory, just use cd . The /Desktop command is sufficient.

absolute path (computing) It may be easier to write than absolute path (computing), But with absolute correctness, but (not) relative path (computing) compare absolute path (computing) More flexibility, For example, when writing a program for program portability, commonly used relative path (computing) to represent the directory。

3.2 Directory-related commands

3.2.1 Switching directories

Command Format: cd directory

cd be Change Dictionary abbreviation for, The directory can be the special directory mentioned earlier, use absolute path (computing) or when relative path (computing) express。

Example.

 cd / # Switch to the root directory

3.2.2 Displaying the current directory

Command Format: pwd [-p]

pwd is the abbreviation of Print Working Dictionary, that is, to display the current directory, the p parameter is to get the real directory of the current directory, such as the linked directory file /bin, directly use the pwd command, display /bin, but add the P parameter, display the real directory as /usr/bin.

Example.

 cd /bin # Switch directories to /bin
 pwd # Show current directory as /bin
pwd -P # Show that the current directory is /usr/bin, which is true under CentOS7, but may not be true for other versions of Linux.

3.2.3 Viewing the Table of Contents

Command Format: ls [-aAdfFhilnrRSt] directory name or file name, ls [--color={never,auto,always}] directory name or file name, ls [--full-time] directory name or file name

ls This command has more parameters, you can use man, info to see ls details, or see ls help ls -- help.

Options and Parameters.

-a: all files, together with the hidden files (starting with . (the documents) are listed together (commonly used)

-A : All the files, together with the hidden files, but excluding . regard sth. These two catalogues

-d : lists only the directory itself, not the file data within it (common)

-f : lists the results directly without sorting (ls will sort by filename by default!) )

-F : Gives additional data structures based on information about files, directories, etc., e.g.

   *:for executable files; /:for directories; =:for socket files; |:for FIFO files.

-h: List the file size in a more human-readable format (e.g. GB, KB, etc.).

-i : lists the inode numbers, i.e. the number of links to directories or files

-l: long data serialization, containing data on file attributes and permissions, etc. (common)

-n : lists UIDs and GIDs instead of user and group names

-r : reverse the sorting result, for example: the original file name from small to large, the reverse will be from large to small

-R : Listed together with the contents of the subdirectory, equal to all files in the directory will be displayed

-S : Sort by file size, not by filename

-t : sorted by time, not by file name

--color=never: do not give colours based on the characteristics of the document.

--color=always : Display color

--color=auto : Let the system decide whether to give the color based on the setting itself

--full-time : Output in full time mode (including year, month, day, hour, minute)

--time={atime,ctime}: output access time or time to change permission attribute (ctime) instead of content change time (modification time)

The most common of these arguments is the l argument, and for convenience, many Linux versions have given the ls -l command the alias ll. Both commands serve the same purpose.

3.2.4 Creating a new directory

Command Format: mkdir [-mp] Directory Name

mkdir is short for Make Dictionary, the directory name can be preceded by the directory location, the m parameter sets the directory permissions, the default directory permissions (umask) are not used, and the p parameter recursively creates the desired directory.

Example.

 cd ~/Desktop # Switch the desktop under the user's home folder
 mkdir test # Create the test directory on the desktop
 mkdir -m 711 test0 # Create the test0 directory and set rwx--x--x permissions on the directory
 ls -ld test0 # Show the properties of the test0 directory
 mkdir -p test1/test2/test3 # Create a multi-level directory without the p argument, it will prompt that there is no such file or directory and the creation will fail
 ls -ld test* # Show the properties of the directory just created with the prefix test

3.2.5 Deleting empty directories

Command Format: rmdir [-p] Directory Name

rmdir stands for Remove Dictionary, and the p argument recursively removes empty directories.

Example.

 mkdir test # Delete the test directory created above
 mkdir -p test1/test2/test3 # Recursively delete the test1/test2/test3 directory created above

However, if the directory is not empty, i.e. there is content in the directory, you cannot use rmdir to delete the directory, but use the rm -r command to delete it.

Command Format: rm [-rf] Directory Name

The r argument asks the contents of the directory one by one if you want to delete it, but with the f argument you can force it to delete without asking.

Example.

 mkdir test # Create the test directory
 touch test/test.txt # Create the test.txt file in the test directory
 rm -rf test # Delete the test directory and all its contents, if you rmdir directly, you will be prompted with a deletion failure and the directory is not empty

3.3 File-related commands

3.3.1 New document creation

Command Format: touch [-acdmt] File Name

A file in Linux has multiple time parameters to record time-related information.

  • modification time (mtime): This time will be updated when the "content data" of the file changes, within The tolerance data refers to the content of the file, not the attributes or permissions of the file.
  • status time (ctime): This time will be updated when the "status" of the file changes, such as when permissions and attributes are changed.
  • access time (atime): This read time (access) is updated when "the contents of this file are fetched". For example, when we use cat to read a file, we update the atime of that file.

The touch command can not only create files, but also modify the time information of files. The a parameter is to modify the atime; the m parameter is to modify the mtime; the c parameter modifies the time of the file, and if the file does not exist, no new file will be created; the d parameter is date shorthand, and you can add the time parameter directly after it.

3.3.2 Reproduction of documents

Command Format: cp [-adfilprsu] source file target file

Options and Parameters.

-a : equivalent to -dr --preserve=all, as for dr please refer to the following description (commonly used)

-d : If the source file is the attribute of a linked file(link file), then copy the link File Properties and not the document itself

-f : means force, if the target file already exists and cannot be opened, remove it and try again

-i: If the destination file (destination) already exists, the overwrite will first ask for the action to be performed (common)

-l: Create the link file for hard linking, not copying the file itself

-p : Copy the file with its attributes (permissions, user, time) instead of using the default attributes (common for backups)

-r : recursive continuous replication, for directory replication behavior (commonly used)

-s : copied as a symbolic link, i.e. a "shortcut" file

-u : update destination only if destination is older than source, or copy if destination does not exist

--preserve=all : In addition to the -p permission-related arguments, SELinux attributes, links, xattr, etc. are also copied.

Finally, note that if there are more than two source files, the last destination file must be a "directory"!

3.3.3 Deletion of documents

The same commands mentioned above for deleting non-empty directories also apply to deleting files.

3.3.4 Moving documents

Command Format: cp [--fiu] Source Location Destination Location

The f argument is to overwrite directly without asking if the target file already exists, the i argument is to ask whether to overwrite if the target file already exists, the u argument is to update the target file to the source file if the target file already exists but the source file is newer, and additionally this command can be used to rename files and directories.

3.3.5 View content

There are many commands to view the contents of a file, the most common ones are cat, more, less, etc. Of course, we can also view the contents of a file directly in a text editor, such as vim (and syntax highlighting) to open it.

  • cat is short for Concatenate, not "cat". cat shows the contents of the file from the first line
  • tac is shown from the last line, and you can see that tac is cat written backwards.
  • nl output the line number in passing when displayed.
  • more Displays the contents of the file page by page, spacebar to page down, Enter to scroll down one line, /string, query string.
  • less is similar to more, but better than more in that it can page forward, spacebar page down, pagedown page down, and pageup page up.
  • head Read only the first few lines.
  • tail Look at the tail for just a few lines.
  • od Reads the contents of a file as a binary.

Personal GitHub address.https://github.com/weizhiwen We welcome visits.


Recommended>>
1、Protection of Seedlings Internet Safety Lesson 1 Protecting Personal Privacy
2、Liu Xingjun President of the Institute of Materials Genetics and Big Data of HUST and his delegation visited our institute
3、Terrible Its amazing how restoring factory settings on an old phone can also restore data
4、Open Source Curriculum Initiative Lighting the Darkness Under the MooC Lights
5、The teacher who creates a beautiful world with mathematics Mr Shuai Shuai

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号