Linux. Shell programming notes-file and file systems

Source: Internet
Author: User
Tags touch command

Chapter 6 file and File System

There are five file types in LINUX:

Normal File

"

Character Device File

Block Device Files

Symbolic Link file

File

Refer to the file for ls commands

-A lists all files in the directory, including hidden files starting. // Common

-L list the details of a file.

-T is sorted by time.

Reference: http://linux.chinaunix.net/techdoc/system/2009/06/08/1117450.shtml

File Type

Six types:

1. Common files:

The first symbol is-. Such a file is a common file in Linux. These files are generally created using related applications, such as tools, document tools, archiving tools... or cp tools.

2. Directory

The directory is a special file in Linux. Note that its first character is d. Run mkdir or cp to create a directory. cp can copy one directory to another. Delete the file by using the rm or rmdir command.

3. Character Devices

The first character is c, which indicates the character device file. Such as memory, terminal, and black hole

4. Block Device Files

The first character is B, which indicates a device, such as a hard disk or optical drive. Files of this type are created by mknode and deleted by rm.

5. Symbolic Link file

The first character is l, which is a link file.

6. Set interface files

The first character is s. Another character is p. This type is rarely used.

File Permissions

In Linux, each file or directory contains access permissions, which determine who can access and how to access these files and directories.

User Group

For an object, you can set different access permissions for three different user types. The three user types are:

Owner, the user who creates the file.

Group user Group, which refers to a Group of similar users. A single user in a user group can set its users

For example, all users in a certain type or project can be classified as one user group by the system administrator, A group member can grant access to files created by other Members in the project group.

Other users also open their files to all users in the system. In this case, the system

All users in the bucket can access their directories or files. In this sense, all other users in the system are the other user class.

You can restrict access permissions in the following three ways: allow only the user (owner) to access; allow a specified

User (group) access in the user group; allow access by any user (other) in the system.

[houchangren@ebsdi-23260-oozie data]$ cat /etc/passwd|head -2root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin

In/etc/passwd, each record consists of seven fields:
User Name,

Password in encrypted format,

User id,

Group id,

Full name or other account descriptions,

Home Directory,

Log on to the shell (the program that runs during logon ).

[houchangren@ebsdi-23260-oozie data]$ cat /etc/group | grep rootroot:x:0:root,huanghu,dywbin:x:1:root,bin,daemondaemon:x:2:root,bin,daemonsys:x:3:root,bin,admadm:x:4:root,adm,daemondisk:x:6:rootwheel:x:10:root

In the user group file/etc/group, it indicates that the user belongs to that group, and that group contains the information of those users.

To write a user to the root group, add the user to the last column.

Command for Chmod to change File Permissions

Chown command to change the file owner

Command for Chgrp to change the file group

File Access permission

Users can control the degree of access to a given file or directory. A file or directory may have read, write, and execution permissions. When a file is created, the system automatically grants the file owner the read and write permissions to display the file content and modify the file. The file owner can change these permissions to any permissions he wants to specify. A file may only have read permission and cannot be modified. It may only have execution permission, allowing it to execute like a program.

For each file, the file owner or super user can set the readable, writable, and executable permissions for the file, which are r, w, and x respectively.

R (Read) has the permission to Read the file content, and n records has the permission to browse the directory.

W (Write, Write) has the permission to add and modify the file content for the file, and has the permission to delete and move files in the directory for the directory.

X (exeute, execute) has the permission to execute the file; for Japanese, this user has the permission to access the directory.

Umask command

In addition, the default permissions can be modified using the umask command. The usage is very simple. You only need to execute the umask 777 command to block all permissions. Therefore, the files or directories created later are blocked, the permissions are changed to 000, and so on. Generally, the root account and umask command are equivalent to 022,027,077, while common users use 002. In this way, the generated permissions are 755,750,700. 775.

File Permission numeric representation

File and directory permissions are represented by the three characters rwx to represent the permissions of the owner, user group, and other users. Sometimes the characters seem to be too troublesome, so there is another way to indicate permissions with numbers, and only three numbers are required.

R corresponds to value 4

W corresponds to 2

X corresponds to value 1

-Corresponding value 0

The combination of values corresponding to the above is the permission number

For example, 777 means that all rwx users have 4 + 2 + 1 ...... 4 + 2 + 1 ...... 4 + 2 + 1

700 means that only the user owner can rwx

In fact, the prototype is binary.

For example, if-rw-xw-has a value corresponding to 1 and no value corresponds to 0, then the corresponding binary value is-110110110.

Special Permissions

Because special permissions have some "privileges", you should not enable these permissions if you do not have special requirements. To avoid serious security vulnerabilities and hacker intrusion, they can destroy the system!

Specific content reference http://www.cnblogs.com/huangzhen/archive/2011/08/22/2149300.html

File modification time

The touch command can modify the creation time. If the-c parameter is not specified, it is created if it does not exist.

Search for files

The file search mechanism in UNIX and linux is very powerful. When you use the find command to work with other tools, you can:

(1) Find the file that complies with certain rules;

(2) execute a command in sequence for such files.

Find command

Formula: Usage: find [path...] [expression]

The common format of the find command is:

Find [pathname] [-options] [-print-exec-OK...]

The parameters in the command are described as follows:

Pathname find command. For example, it is used to represent the current directory and the system root directory is used to represent the directory.

-The print find command outputs matching files to the standard output.

-Execute the exec find command to execute the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And.

-OK and-exec play the same role, but execute the shell command given by this parameter in a safer mode. A prompt will be given before each command is executed, let the user determine whether to execute.

Instance:

[Houchangren @ ebsdi-23260-ooziedata] $ find .. /-iname "*. sh "-print .. /checkUserIsExist. sh .. /if. sh .. /testwhile. sh .. /add. sh .. /testfor. sh .. /testalg. sh .. /user_login.sh .. /whileexample. sh .. /. sh .. /testcase. sh .. /whileexample2.sh [houchangren @ ebsdi-23260-ooziedata] $ mkdir .. /.. /tmp [houchangren @ ebsdi-23260-ooziedata] $ find .. /-iname "*. sh "-exec cp {}.. /.. /tmp/\; [houchangren @ ebsdi-23260-ooziedata] $ ll .. /.. /tmp/total 44-rw-rw-r -- 1 houchangren 51 01-20 16: 14add. sh-rw-r -- 1 houchangren 337 01-20. sh-rw-r -- 1 houchangren 85 01-20 16: 14checkUserIsExist. sh-rw-r -- 1 houchangren 55 01-20 16: 14if. sh-rwxrwxr-x 1 houchangren 284 01-20 testalg. sh-rw-r -- 1 houchangren 206 01-20 testcase. sh-rw-r -- 1 houchangren 42 01-20 16: 14testfor. sh-rw-r -- 1 houchangren 319 01-20 testwhile. sh-rw-r -- 1 houchangren 132 01-20 user_login.sh-rw-rw-r -- 1 houchangren 193 01-20 whileexample2.sh-rw-r -- 1 houchangren 253 01- whileexample. sh [houchangren @ ebsdi-23260-ooziedata] $

There are also many usage of find, refer:

Http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html.

Traverse files

When too many find files are passed into the next exec, if there are too many parameters, an error will be reported. In this case, you can use the xargs command to accept batch processing.

The find command passes the matching file to the xargs command, while the xargs command only obtains a single object instead of all objects at a time, unlike the-exe option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this.

In some systems, the-exec option will initiate a corresponding process for processing a matching Mei file, rather than executing all the matching files as parameters once; in this way, in some cases there may be too many processes and the system performance may decline, resulting in low efficiency;

The xargs command has only one process. In addition, when xargs command is used, all parameters are obtained at a time. you can also obtain parameters in batches and the number of parameters each time. it is determined based on the options of the command and the corresponding adjustable parameters in the system kernel.

Query all common files in the current directory and execute the file command.

[houchangren@ebsdi-23260-ooziedata]$ find  . -type f |xargs file./sort.txt:        ASCII text./fruit.txt:       ASCII text./ips.txt:         ASCII text./stuff.txt:       ASCII text./ips.txt.t:       ASCII text./fruits.txt:      ASCII text./fruit.txt.rep:   ASCII text./fruit.txt.upper:ASCII text./salary.txt:      ASCII text./fruit.txt.rm:    ASCII text 

Query the information of common files in the current directory that contain apple characters.

[houchangren@ebsdi-23260-oozie data]$find  . -type f |xargs grep 'apple'./fruit.txt:apple./fruit.txt:apple ./fruits.txt:1:2:apple-2./fruits.txt:1:2:apple-1
Compare files

There are many methods to compare file differences in UNIX/Linux. The most typical method is to use the comm and diff commands. In addition, some text & Wood editors provide interactive file comparison methods and display them in a more user-friendly way.

Use comm to compare sorted files

The comm command compares the differences between two sorted files row by row and displays the results. If no parameters are specified, the results are displayed in three columns; the 1st columns are the rows that have appeared in the 1st files, and the 2nd columns are only the rows that have appeared in the 2nd files, the 3rd column is the row that appears in both the 1st and 2nd files. If the given file name is "-num", the comm command reads the data port from the standard input device.

Comm usage

Comm file1 file2

Comm [-123] filefile2

-1 does not show columns that appear only in 1st files.

-2 do not show columns that appear only in 2nd files.

-3 do not show columns that appear only in 1st and 2nd files.

Use diff to compare files

The diff command is used to compare two text files one by one and list their differences. It performs more complex checks than comm commands. It performs a system check on the given files and displays all different rows in the two files. It is not required to sort the files in advance.

Diff command output format
 

File1 affects rows

Action

File2 affects rows

Number1

A

Number2 [, Number3]

Number1 [, Number2]

D

Number3

Number1 [, Number2]

C

Number3 [, Number4]

These lines are similar to the edit sub-USER command to convert the file1 file into the f ile2 file. The number before the Action letter refers to file1, And the number after it refers to file2. Therefore, by replacing a with d and reading from right to left, you can know how to convert file2 to file1. Under the edit command, the same pair (I .e. Numbed = Nurnber2) is simply a single number.

For each of the following lines, the diff command explicitly does not show all the lines in the first file starting with <(less than symbol, colon), and then displays with> (greater than symbol) all affected rows in the second file.

Instance:

[houchangren@ebsdi-23260-ooziedata]$ diff fruit.txt fruit1.txt2c2< banana  --->1banana  5c5<%%banae --->1%%banae  [houchangren@ebsdi-23260-ooziedata]$ diff fruit.txt fruit2.txt8,9d7< orange  < persimmon [houchangren@ebsdi-23260-ooziedata]$ diff fruit2.txt fruit.txt7a8,9> orange  > presimmon

Run diff-help or below.

Diff parameter reference: http://linux.chinaunix.net/techdoc/system/2007/08/01/964240.shtml

Other comparison tools

Vimdiff tool-provides a GUI text comparison method

Usage

[houchangren@ebsdi-23260-oozie data]$vimdiff fruit2.txt fruit.txt

Online Reference:

Http://blog.chinaunix.net/uid-361890-id-175414.html

Http://www.ibm.com/developerworks/cn/linux/l-vimdiff/

File System

Different from Windows, UNIX/Linux supports multiple file systems (Windows only supports common Fat32 and

NTFS), which can be called the Swiss army knife in the operating system. Common file systems in UNIX/Linux include ext3, ext4, reiserfs, ZFS, etc. UNIX/Linux also has special file systems in some special scenarios, such as mounting swap directories.

(/Tmp) tmpfs, used for network NFS, etc.

What is a file system?

Although the kernel is the core of Linux, files are the main tool used by users to interact with the operating system. This is especially true for Linux because in LINUX tradition, it uses file I/O mechanisms to manage hardware devices and data files.

Unfortunately, new users often confuse terms that introduce the concept of Linux file systems. The term file system can be used interchangeably in Linux File preparation to refer to several different but related concepts. In addition to the actual disk partition, the file system also refers to the data structure and file management methods in the partition.

This term is also used to refer to the overall structure of files in the system: directory tree. In addition, it can also refer to each subdirectory in the directory tree, for example, in the/home file system. some people think that these directories and directories cannot be called true file systems unless they all reside on their respective disk partitions. However, others call it a file system, which undoubtedly adds confusion.

Linux Veterans can understand the meaning of these terms from the context. It is difficult for new users to identify such a context at half past one.

File System and disk partition

A disk is a readable and writable device. There are a lot of tracks on the disk. The track reader (head) can read and write the disk when the disk is flying. However, in many cases, a disk can accommodate a large amount of data, so for convenient management, we divide disk tracks into several parts, each of which is used for a disk partition.

We usually consider the file system as a disk partition. In UNIX/Linux, the basic file storage units are disk partitions. One or more hard disks are logically divided. The operating system regards each logical partition as an independent disk. Sometimes, if a disk is too large, we need to properly plan partitions to effectively block storage on the disk.

Linux treats these disk partitions as devices and uses the file I/0 mechanism through special files in the/dev directory. There are two types of device files: Block devices and character devices. One important difference between the two is that block devices are buffered, and character devices are not buffered because they do not have a file management system.

To effectively deal with disks (such as searching, storing, and reading disks), the operating system unifies the disk storage methods according to certain rules. This rule specifies how a file is mapped to the disk's partition track. It specifies that when there is not enough continuous track to store large files, how to splice multiple discontinuous channels to hold files. This rule is called the file management system or the disk partition format.

The partition table stored in the disk's initial position provides the partition ing port for the disk. You can use the fdisk command to view the Partition Table of the system:

[root@ebsdi-23260-oozie~]# fdisk –lDisk /dev/sda:85.8 GB, 85899345920 bytes255 heads, 63sectors/track, 10443 cylindersUnits =cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks  Id  System/dev/sda1   *          1          13      104391  83  Linux/dev/sda2              14       10443   83778975   8e  Linux LVM

This example shows that this host has only one hard disk, sda. If there are more than two disks, sdb, sdc, and so on. In the case of an IDE drive, the partition in the had. drivers is represented by a number, so/dev/sda5 is the fifth partition on the first SCSI drive.

The first to fourth partitions are retained to the primary partition, and the fifth and subsequent partitions are used for logical partitions. therefore, the partition table shown above contains a drive sda, which contains four primary sda1 to sda4 and one extended partition sda5.

In Linux, partitions include primary partitions, extended partitions, and logical partitions. The term primary partition is a legacy product of the four partition restrictions on the previous x86 system. Unlike Dos and Windows, linux can be started from the primary partition or logical partition. A primary partition used as a placeholder for a logical partition is called an extended partition. Extended partitions have partition tables that point to one or more logical partitions (they are only subpartitions of the primary partition. In the above fdisk list, sda4 is an extended partition.

In addition to viewing partition tables, fdisk can also modify partition tables. In fact, fdisk is a partition table modifier. For example, you can use fdisk to modify the/dev/sda partition table.

[root@ebsdi-23260-oozie~]# fdisk /dev/sda1Device containsneither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a newDOS disklabel. Changes will remain in memory only,until you decideto write them. After that, of course, the previouscontent won't berecoverable. Warning: invalidflag 0x0000 of partition table 4 will be corrected by w(rite) Command (m forhelp): mCommand action   a   togglea bootable flag   b  edit bsd disklabel   c  toggle the dos compatibility flag   d  delete a partition   l  list known partition types   m  print this menu   n  add a new partition   o  create a new empty DOS partition table   p  print the partition table   q  quit without saving changes   s  create a new empty Sun disklabel   t  change a partition's system id   u  change display/entry units   v  verify the partition table   w  write table to disk and exit   x  extra functionality (experts only) Command (m forhelp): 

In the interactive command line of fdisk, you can perform partitioning, deletion, modification, addition, and formatting operations. You can run the m command to view the help documentation of fdisk.

If you do not like the Q & A partitioning method, you can select the interactive command cfdisk and run the command

Cfdisk/dev/sda1

The pop-up interactive interface is shown in figure

In this interface, you can move the cursor by pressing the upper/lower left key to delete or create a partition. You can also specify the partition format. Cfdisk is actually the graphic interface of fdisk.

Linux partition format selection and security

UNIX/Linux is known as a file system (partition format) Swiss Army knife, you can choose a variety of partition formats. Besides

Supports FAT32 and NTFS partition formats in Windows (supports read/write, not as the root file system). ext2, ext3, and reiserfs are common examples. In addition, well-known operating systems often have their own file system killer copper, such as Solaris's ZFS file system and Apple's HFS + file system.

Common File System formats in Linux include ext2, ext3, swap, VFAT, and reiserfs.

Ext2:

Ext2 is a standard file system in GNU/Linux systems. This is the most widely used File System in Linux. It is specially designed for Linux and has extremely fast speed and extremely small CPU usage. Ext2 can be used for both standard block devices (such as hard disks) and Mobile storage devices such as floppy disks.

Ext3:

Ext3 is the next generation of Ext2, that is, the log function is added under the Ext2 format. Ext3 is a log-type File System (JournalFile System). It records the write operations of the entire disk in a certain area of the disk, in order to trace back when necessary. When a process is interrupted, the system can directly trace and refresh the interrupted part based on these records, and the refresh speed is quite fast. This partition format is widely used in Linux.

Linux swap:

It is a swap file system used for partition swap in Linux. Linux uses this entire partition as the swap space. Generally, the swap partition in this swap format is twice the primary memory. When the memory is insufficient, Linux will write part of the data to the swap partition.

VFAT:

VFAT is called a long file name system. It is a Linux file system compatible with Windows systems. It supports long file names and can be used as partitions for switching files between Windows and Linux systems.

File System and directory tree

Reference: http://blog.csdn.net/ruishenh/article/details/18602117

Create and mount a File System

When fdisk or cfdisk is used to divide a disk into multiple partitions, You need to specify the partition format for each partition of the partitioned disk. In Windows, this concept is called disk formatting and the command is mkfs.

Mkfs creation command reference:

Http://blog.163.com/yi_yixinyiyi/blog/static/136286889201164102353634/

Mount command reference:
Http://www.51ou.com/browse/linuxml/mount.html

Umonut uninstall command reference:

Http://www.51ou.com/browse/linuxml/umount.html

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.