Parsing special Linux Files

Source: Internet
Author: User

 

With your experience in Dos and Windows, you probably know that there are several types of files in the system, such as system files, read-only files, and hidden files. In Linux/Unix, file types are far more diverse and complex than Dos/Windows systems. Taking RedHat Linux as an example, this article aims to give readers a better understanding of the file types in Linux. Most of the content in this article is also true for Linux, Aix, Solaris, Freebsd, and other operating systems of other releases. In Linux, you can use the ls-l command to determine the file type, as shown in the figure above. It can be determined based on the 10 characters in the first column. ? -Rw-r-r-indicates that 1.txt and myprog04 files are common files. All files starting with "-" are common files, while those starting with "d" are directory files.

? Brw-rw ---- indicates that/dev/sda1 is a Block Device file. All files starting with "B" are block device files .?

? Crw-rw ---- indicates that/dev/lp0 is a character Device file. Files starting with "c" are character Device files.

? Srwxrwxrwx indicates that/var/lib/mysql. sock is a socket file. All files starting with "" are socket files.

? Prwxr-r -- specifies that mypipe is a pipeline file. An attribute of the MPs queue file is "p ".

? Lrwxrwxrwx indicates that softlinkof1.txt is a soft link file (or a symbolic link file) that refers to 1.txt. Files starting with "l" are soft-link files.

? -The hard_link_of_1.txt file starting with rw-r-r-r-seems to be a common file, but it is actually a hard link file.

? -Rwsr-xr-x indicates that myprog01 is an executable file with a setUid, which is determined by the fourth character "s.

? -Rwxr-sr-x indicates that myprog03 is an executable file with a setGid, which is determined by "s" in the seventh character.

? -Rwsr-sr-x indicates that myprog02 is an executable file with setUid and setGid. This is determined by "s" in the fourth and seventh characters.

? The first "d" in drwxrwxrwt indicates that the tmp file is a directory, and the last character "t" indicates that the directory is set with a sticking bit. I. Device Files

The/dev directory in Linux contains a large number of device files. Mainly block device files and character device files. Block Device Files

In the past, after adding a new disk, you often needed to manually add block device files. Now we usually do not need to manually add a block device file. Run service kudzu start and the system will automatically configure the corresponding device for you. Block devices can be read and written randomly. The most common Block devices are disks, such as/dev/hda1,/dev/sda2, And/dev/fd0. Character Device File

Like Block devices, we can use the service kudzu start command to automatically add, delete, or modify character devices. The most common character devices are printers and terminals that can accept the RST stream. /Dev/null is a very useful character device file, and everything sent to this device is ignored. If you redirect the output results of any program to/dev/null, no output information is displayed. You can even point a user's shell to/dev/null to prevent it from logging on. MPs queue Device File

Pipeline device files are sometimes called FIFO files (FIFO is the meaning of FIFO). Literally, pipeline Device Files flow from one end to the other. We usually do some work in it to achieve the goal of "eating grass, squeezing milk", and pipeline files are also useful. In the past, Unix systems used a limit of 2 GB for maximum file usage. Although the current versions of Linux, Solaris, And FreeBSD do not have this limit, the requirements for processing large files still exist, assume that you want to use an image (dd command) to back up a 20 GB partition, a 20 GB file will be generated. Based on the actual usage of your disk, this file may only be compressed from several MB to several GB. We can create an MPS file to automatically implement this compression process. [Root @ linux236 root] # mknod mypipe p

[Root @ linux236 root] # ls-l mypipe

Prw-r -- 1 root 0 Aug 5 23:27 mypipe

[Root @ linux236 root] # here, we have created a pipeline file named mypipe. We can use the ls-l command to see that its property is prw-r --, use the following command to implement image and compression: [root @ linux236 root] # compress <mypipe>; sda6.img. Z &

[Root @ linux236 root] # dd if =/dev/sda6 of = mypipe

[Root @ linux236 root] # ls sda6.img. Z

The first command of sda6.img. Z compresses the files exported from the mypipe pipeline into the sda. img. Z file. Note that the end of this command must use the "&" symbol. The second command imports the data in the/dev/sda6 partition into the MPs queue file mypipe. In other words, the data in the/dev/sda6 partition enters the MPs queue and the compressed file sda6.img. the Z file is exported from the MPs queue. When exporting large databases such as Oracle and DB2, large files are often generated. Skilled database administrators often choose to compress data through pipelines. For Oracle databases, you can use the combined command below to compress the exported content from Oracle to expdat. dmp. Z file. Compress <mypipe>; expdat. dmp. Z &

Exp userid = system file = mypipe owner = scott

 

Chapter 2 linking file links is a bit similar to the so-called shortcuts of Windows, but they are not exactly the same. There are two methods for linking: Soft link and hard link. Soft link file

Soft links are also called symbolic links. This file contains the path name of another file. It can be any file or directory that can be linked to files of different file systems. A linked file can even link a non-existing file, which leads to a problem commonly referred to as "broken chain" (or "Symptom", the linked file can even link itself cyclically. Similar to recursion in programming languages. [Yaoyao @ linux236 yaoyao] $ ls-l

Total 0

Lrwxrwxrwx 1 yaoyao 5 Aug 6 17:39 1.txt->; 3.txt

Lrwxrwxrwx 1 yaoyao 5 Aug 6 17:38 2.txt->; 1.txt

Lrwxrwxrwx 1 yaoyao 5 Aug 6 3.txt->; the three files above 2.txt form a recursion, which has no function in essence. The system administrator should avoid system leeching or loop links. You can use the ln-s command to generate a soft connection, as shown in the following figure: [root @ linux236 test] # ln-s source_file softlink_file when reading or writing symbol files, the system automatically converts the operation to the operation on the source file. However, when you delete a linked file, the system only deletes the linked file instead of the source file itself. Hard link file

The info ln Command tells you that A hard link is another name of an existing file (A "hard link" is another name for an existing file), which is somewhat confusing. The hard connection command is ln-d existfile newfile. There are two restrictions on the hard link file. 1. Hard links cannot be created for directories;

2. A link can be created only between files in the same file system. When you perform read/write or delete operations on hard-link files, the results are the same as those of soft-link files. However, if we delete the source file of the hard link file, the hard link file still exists and retains the expected content. At this time, the system "forgot" that it was a hard link file. And regard it as a common file. 3. When setUid, setGid, and directory files with adhesive bits are in Linux/Unix, an executable file is setUid, which allows any user to execute the file, are bound to the permissions of the file owner. Just like a file with a powerful sword, the setUid file is usually used to enhance user permissions. The most representative su command. Common users can execute this command to upgrade themselves to the root. The setUid command is used: the chmod 4755 your_program setGid file is very similar to the setUid file, which enables any user to bind the permissions of all groups of the file when executing the file. A single setGid file is rarely used, usually both setUid and setGid. But it may be a little different from what you guess. SetUid + setGid is generally not used to enhance permissions, but to bind special permissions to a special user and their groups, such as qmail's peripheral software vpopmail, A program vchkpw with setUid + setGid is used to verify the user name and password. This principle is the same as that of Apache, which is often run as a nobody user. Its purpose is to be more secure. The setGid command is used

Chmod 2755 your_program usually uses the command

Chmod 6755 yourprogram makes it possible for a root user who can execute a program at the same time setUid and setGid all-around to set arbitrary setUid and setGid. However, Shang fangbaojian cannot use it to fake the sacred intent. Common users can only configure setUid or setGid for their own files. Because the setUid or setGid file will increase the permissions of common users, careful system administrators usually pay attention to the changes in the setUid or setGid file in the system. Reduce security risks. In Linux,/tmp is a directory for storing temporary files, which must be writable to all users. However, each user can only delete their own files. In this case, you can add a sticking bit to the directory. [Root @ yaoyao/] # ls-l | grep tmp

Drwsrwsrwt 9 root 4096 August 7 10:50 tmp note the character "t", which indicates that the directory is set with a sticking bit. We create a self-built abc directory so that it has the same features as/tmp chmod 777 abc

The two command combinations in chmod + t abc are equivalent to the following command: chmod 1777 abc uses ls-l to view the attributes of the abc directory: [root @ yaoyao test] # Total ls-l usage 4 drwsrwsrwt 2 root 4096 August 7 11:32 the same requirements for the abc and/tmp directories often exist in the ftp server's upload directory. It can be processed in the same way. 4. socket files are similar to pipelines, but they work on the network. You rely on it for network processing on your computer. You may have heard of "Winsock", which is a set of Windows interfaces. We will not go into the related sets of interfaces here, because if you do not write a program, you will not use it, but if you see a file type in your system is s, you only need to know what it is. For example, a socket file is usually generated during mysql running. [Root @ yaoyao tmp] # ls-l/tmp/mysql. sock srwxrwxrwx 1 mysql August 7 10:03 mysql. there are some socket files in the sock/tmp directory, most of which are generated when running Xwindows. 5. Miscellaneous: Why can't I delete files that cannot be deleted? "why can't I delete some files starting with an annoying minus sign ("-")? Why? "You may have heard such a shout from a newbie at your desk, hoping that the same thing will not happen to you, which is very easy to solve, you only need to use the Path Method to delete them. If a file name is "-abc", you can use rm. /-abc or

The rm/home/yaoyao/-abc command can be easily deleted. You can also use vi or other tools to modify them in the same way. Some other files may seem to be normal, but when you try to delete them, an error will be reported, as shown below: [root @ linux236 root] # ls-l 1.txt

-Rw-r -- 1 root 0 Aug 5 23:00 1.txt

[Root @ linux236 root] # rm-rf 1.txt

Rm: cannot unlink '1.txt ': Operation not permitted: You are an all-around root user. Actually, the system tells you that the Operation is not allowed. Is it Linux crazy? Of course not. If you use the lsattr command, the answer is provided. [Root @ linux236 root] # lsattr

--- I ----------./1.txt

--------------./Weiqi. ldif

--------------./Qi. schema is completely confidential. In lsattrcommand, this 1.txt file has an "I" attribute, so it cannot be deleted. Now you can use the following commands: [root @ linux236 root] # lsattr 1.txt

--- I ---------- 1.txt

[Root @ linux236 root] # chattr-I 1.txt

[Root @ linux236 root] # rm-rf 1.txt

[Root @ linux236 root] # succeeded. This attribute is used to protect important files from being deleted. Generally, generally, the system administrator can determine whether the file can be deleted by using these commands. If you want to add more protection to a file, use the following command: chattr + I filename. In this way, you need to take one more step to delete the file. At the same time, such files cannot be edited or modified. Only the root user can use the chattr command. This command can be used on Linux ext2 or ext3. Similar to Dos and Windows file systems, most files that cannot be deleted at will have their own principles. Even if you know how to delete them, you should think twice. Summary

Linux/Unix system management is a complex task. It is essential to master and understand the Linux/Unix file types. There is no shortcut to learning Linux/Unix, and it must be learned through a lot of practices and hard work.

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.