Linux file metadata and links

File metadata and table structure

The attribute information of each file, such as the size, time and type of the file, is used as the metadata of the file. These metadata are stored in a specific table, this table is the inode. There are many records in the node, mainly including the following information:

  1. The node number of the inode. The total data volume of this node is determined after formatting the disk.
  2. File type
  3. File permission information
  4. Owner ID information of the file
  5. ID information of all groups of the file
  6. The number of links, this file will change when there are hard links
  7. Pointer to the address where the data is actually stored
  8. Other data about the file.

Linux file metadata and links

The above figure can cover the basic information of the inode. We use experiments to manipulate the file to achieve the time change.
First, prepare the test file, copy /etc/fstab to /tmp, and
Linux file metadata and links
view the current file status information
Linux file metadata and links
to modify mtime. Modify mtime to 2019 to
Linux file metadata and links
view the modified results, which can be achieved. Because the attributes change, ctime will also change at the same time.
Linux file metadata and links

link

In many cases, multiple access paths need to be mapped to the same file

Hard link

The essence of a hard link is the same file. When a hard link is created, the number of links in the metadata will be increased. When the number of links is 1, the data file will only be deleted when it is deleted. Hard links cannot span partitions and cannot create hard links to directories.
Example: The
default ln command creates a hard link,
Linux file metadata and links
you can see that the inode number and link number have changed
Linux file metadata and links

Soft link aka symbolic link

A soft link is a symbol of the linked file, and it does not affect the original file. Can be for directories and cross partitions.
Example: Use the parameter s to let ln create a symbolic link. It
Linux file metadata and links
can be seen that the inode number of the symbolic link file fstab2 is different from the source file. Make sure that this file is a brand new file.
Linux file metadata and links

Commonly used file management commands

pwd displays the current working directory

In many cases, a directory error leads to the wrong execution of the command. The result may be catastrophic. It is very important to confirm the current working path.
Create a symbolic link for /etc here,
-L can display the current path
-P can display the actual location of the device
Linux file metadata and links

Absolute path and relative path

The absolute path
starts with a forward slash, which can be used for files and directories.
Relative paths:
do not start with a slash. The relative directory is based on the current working directory. You can use the file and directory
base name: basename takes only the file name
directory. Name: dirname takes only the path

Linux file metadata and links

Change working directory

cd (change directory)
Common options for changing the current working directory :
.. Switch to the parent directory
No parameters switch to the current user's home directory
-switch to the previous working directory
Linux file metadata and links

List the contents of the directory file

ls parameter file directory
-a displays all files and directories (hidden files starting with. will also be listed)
-l in addition to the file name, it also lists the file type, permissions, owner, file size and other information in detail
-r will The files are displayed in reverse order (originally according to the alphabetical order)
-t List the files in the order of creation time
-A Same as -a, but do not list "." (current directory) and ".." (parent directory) )
-F Add a symbol after the listed file name; for example, add "*" to the executable file, add "/" to the
directory -R If there are files in the directory, the following files are also listed in sequence
Linux file metadata and links

cp copy file command

In the use of the system, it is necessary to frequently copy file backups. Commonly used parameters of
cp parameter source and destination
:
-a: This option is usually used when copying directories. It retains links and file attributes and copies all contents under the directory. Its effect is equal to the combination of dpR parameters.
-d: Keep the link when copying. The link mentioned here is equivalent to a shortcut in the Windows system.
-f: Overwrite the existing target file without prompting.
-i: Contrary to the -f option, a prompt is given before overwriting the target file, asking the user to confirm whether to overwrite, the target file will be overwritten when answering "y".
-p: In addition to copying the contents of the file, the modification time and access permissions are also copied to the new file.
-r: If the given source file is a directory file, all the subdirectories and files in the directory will be copied at this time.
-l: Do not copy files, just generate link files.
Linux file metadata and links

Determine the content of the file

file File name
commonly used parameter
-b When the identification results are listed, the file name is not displayed.
-c displays the instruction execution process in detail, which is convenient for debugging or analyzing the execution of the program.
-f<name file> Specify the name file. When the content has one or more file names, let file identify these files in sequence, and the format is one file name per column.
-L directly displays the category of the file pointed to by the symbolic link.
-m<magic number file> Specify the magic number file.
-v Display version information.
-z Attempt to interpret the contents of the compressed file.
Linux file metadata and links

Move and rename

mv parameter source destination
-b: When the target file or directory exists, a backup will be created for it before overwriting.
-i: If the specified moving source directory or file has the same name as the target directory or file, it will first ask whether to overwrite the old file. Enter y to directly overwrite, and enter n to cancel the operation.
-f: If the source directory or file specified to be moved has the same name as the target directory or file, it will not be asked, and the old file will be overwritten directly.
-n: Do not overwrite any existing files or directories.
-u: When the source file is newer than the target file or the target file does not exist, the move operation is performed.
Linux file metadata and links

Show directory


Commonly used parameters of the tree parameter target :
-d only display the directory
-L display level
-P wildcard matching content
Linux file metadata and links

Create a directory

mkdir parameter target
common parameters:
-p directly create when the directory does not exist
-v often see the execution process
-m set the specified permissions of the directory
Linux file metadata and links

Delete empty directories

The rmdir parameter target
can use
common parameters only when the directory is empty :
-p recursive
-v view execution process information
Linux file metadata and links

vim small test

Set vim's tab to 4 characters
. Adding global configuration
Linux file metadata and links
settings to /etc/vimrc means to set tab to 4 characters in length.

vim command replacement practice

Copy system files to the tmp directory
Linux file metadata and links

It can be found that multiple lines are blank lines.
Linux file metadata and links
Use the command line mode, search and replace to delete blank lines,% searches for the global, ^\s matches the blank characters at the beginning of the space and tab, + means multiple, replaces with empty, g means global replacement
Linux file metadata and links

to sum up

The above content is only a drop in the ocean, as a little accumulation in the learning process.

Guess you like

Origin blog.51cto.com/15131458/2677915