Linux file system has a good structure and provides a lot of file handlers, so what are the commonly used file processing commands?

Source: Internet
Author: User
Tags control characters diff compressed file types egrep

This article tags: getting started with Linux Linux system Linux file system Linux Linux Common command internet Talk

Linux system information is stored in a file, and the file is similar to ordinary official documents. Each file has its own name, content, storage address, and other administrative information, such as the user of the file, the size of the file, and so on. A file can be a letter, an address book, or a source statement for a program, a program's data, or even an executable program or other non-body content. Linux file system has a good structure, the system provides a lot of file processing programs. This article mainly describes the commonly used file processing commands.

File

2. Use

File determines the type of files by probing the contents of the file, and the permission is used by all users.

2. Format

File

[Options] File name

3.[options] Main parameters

-V: Displays the version information after the standard output and exits.

-Z: Detects compressed file types.

-L: Allow the connection to be met.

-F

Name: Read the list of file names to parse from the file NameFile.

4. Brief description

You can use the file command to know whether a file is a binary (elf-formatted) executable, a shell script file, or any other format. File types that can be identified include directories, shell scripts, English text, binary executables, C-language source files, text files, and DOS executable files.

5. Application examples

If we see a file with no suffix grap, you can use the following command:

    1. $ file Grap

Grap:english text

At this point the system shows that this is an English text file. Note that the file command is not able to detect multimedia file types including graphics, audio, video, and so on.

Mkdir

1. Role

The purpose of the mkdir command is to create a subdirectory named DirName, similar to the MD command under MS DOS, which is used by all users.

2. Format

Mkdir

[Options] Directory name

3.[options] Main parameters

-M,--mode= mode: Set permissions < mode;;, similar to chmod.

-P,--Parents: Creates a top-level directory when needed, and is not considered an error if the directory already exists.

-V,--verbose: Displays information each time a new directory is created.

--version: Show version information and leave.

4. Application examples

You can set permissions for the directory at the time of catalog creation, and the parameters used are "-M". Assuming that the directory name you want to create is "tsk", so that all users have rwx (that is, read, write, execute), you can use the following command:

    1. $mkdir-M 777 tsk

Grep

1. Role

The grep command allows you to specify a file to search for specific content and to output the standard rows containing the content. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

2. Format

grep [Options]

3. Main parameters

[Options] Main parameters:

-C: Outputs only the count of matching rows.

-I: Case insensitive (only for single-character).

-H: The file name is not displayed when querying multiple files.

-L: Only file names that contain matching characters are output when querying multiple files.

-N: Displays matching lines and line numbers.

-S: does not display error messages that do not exist or have no matching text.

-V: Displays all lines that do not contain matching text.

Pattern Regular Expression Main parameters:

\: Ignores the original meaning of special characters in regular expressions.

^: matches the start line of the regular expression.

$: Matches the end line of the regular expression.

\<: Starts from the line that matches the regular expression.

\>: To the end of the line that matches the regular expression.

[

]: A single character, such as [a], a meets the requirements.

[

-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.

。 : all the individual characters.

*: There are characters, the length can be 0.

Regular expressions are a very important concept in the Linux/unix system . A regular expression (also known as "regex" or "regexp") is a pattern (pattern) that can describe a class of strings. If a string can be described with a regular expression, we say that the character matches the regular expression (match). This and the DOS user can use the wildcard character "*" to represent any character similar. On Linux systems, regular expressions are often used to find patterns in text, and to perform "search-and-replace" operations and other functions on text.

4. Application examples

Querying the DNS service is one of the daily tasks, which means maintaining a large number of IP addresses that cover different networks. Sometimes there are more than 2000 IP addresses. If you want to see the NNN.NNN network address, but forget the rest of the second part, only know that there are two periods, such as nnn nn ... To extract all of the nnn.nnn IP addresses, use [0-9]\{3 \}\. [0-0\{3\}\. The meaning is that any number appears 3 times, followed by a period, followed by any number 3 times, followed by a period.

$grep

' [0-9]\{3 \}\. [0-0\{3\}\ ' Ipfile

To add, the grep family also includes Fgrep and Egrep. Fgrep is fix grep, which allows to find strings instead of a pattern; Egrep is an extended grep that supports both basic and extended regular expressions, but does not support the application of \q mode scope and some of the more canonical patterns corresponding to it.

Dd

1. Role

The DD command is used to copy the file and convert and format the data according to the parameters.

2. Format

DD [Options]

3.[opitions] Main parameters

bs= byte: Force ibs=< byte >; and obs=< byte >;.

cbs= bytes: Each conversion of the specified < bytes >;.

conv= Keyword: Converts a file based on a comma-delimited representation of a keyword.

count= number of blocks: only the input data of the specified < block number >; is copied.

ibs= Bytes: Reads the specified < bytes >; each time.

if= file: Read < file >; content, not standard input data.

obs= Bytes: Writes the specified < byte >; each time.

of= file: Writes data to < file;; instead of standard output display.

Number of seek= blocks: first skip the output data for the specified < block number >; in OBS units.

Number of skip= blocks: first skip the input data for the specified < block number >; in IBS.

4. Application examples

The DD command is often used to make Linux boot disks . Find a bootable kernel, point its root device to the correct root partition, and then use the DD command to write it to the floppy disk:

    1. $ Rdev Vmlinuz/dev/hda
    2. $dd
    3. If=vmlinuz of=/dev/fd0

The code above shows that using the Rdev command to point the root device in the bootable kernel vmlinuz to/dev/hda, replace "HDA" with its own root partition, and then write the kernel to a floppy disk using the DD command.

Find

1. Role

The function of the Find command is to search the directory for a file, and its use rights are for all users.

2. Format

Find [Path][options][expression]

path Specifies the directory path from which the system starts to look down the file down the directory tree. It is a list of paths that are separated from each other by spaces, and if you do not write path, the current directory is assumed.

3. Main parameters

[Options] Parameters:

-depth: Use the Depth-level lookup process to prioritize file content in a specific level of the specified directory.

-maxdepth levels: Indicates that at most find the first level subdirectory of the start directory. Level is a non-negative number, and if level is 0, it is only found in the current directory.

-mindepth levels: Indicates that at least the level subdirectory of the start directory is found.

-mount: Not found in directories and files in other file systems (such as MSDOS, VFAT, etc.).

-version: Print version.

[Expression] is a matching expression and is an expression accepted by the Find command, and all operations of the Find command are for an expression. It has a lot of parameters, and here are just a few common parameters.

-name: Supports wildcard characters * and?.

-atime N: Searches for files that have been read in the last n days.

-ctime N: Searches for files that have been modified in the last n days.

-group Grpoupname: Search for all files with Grpoupname group.

-user User name: Searches for all files that belong to the primary user name (ID or name).

-size N: The file size of the search file is n blocks.

-print: Output search results, and print.

4. Application Tips

The Find command finds several ways to locate a file:

(1) Search by file name

For example, if we want to find a file with a filename of lilo.conf, you can use the following command:

    1. Find/-name lilo.conf

The "/" after the Find command means that the entire hard disk is searched.

(2) Quickly find files

Finding files based on file names can have a real problem, which is to take a long time, especially when large Linux file systems and high-capacity hard disk files are placed in deep sub-directories. If we know that this file is stored in a directory, you can save a lot of time by looking down in that directory. such as the smb.conf file, from its file suffix ". conf" can be determined that this is a configuration file, then it should be in the/etc directory, at this time can use the following command:

    1. Find/etc-name smb.conf

This way, you can shorten the time by using the Quick Find file method.

(3) Search method based on partial file name

Sometimes we know that only one file contains the 4 words of ABVD, then to find all the files in the system that contain these 4 characters, you can enter the following command:

    1. Find/-name ' *abvd* '

After entering this command, the Linux system will look in the/directory for all files containing the ABVD 4 characters (where * is a wildcard character), such as Abvdrmyz and other eligible files can be displayed.

(4) Find a file using a hybrid lookup method

The find command can use a hybrid lookup method, for example, if we want to find a file that is larger than 500000 bytes in the/etc directory and modified within 24 hours, you can use-and (and) to link the two lookup parameters together into a mixed lookup.

    1. Find/etc-size +500000c-and-mtime +1

Mv

2. Use

The MV command is used to rename a file or directory, or to move a file from one directory to another, and its use rights are for all users. This command is like a combination of Ren and move in a DOS command.

2. Format

Mv[options] Source file or directory destination file or directory

3.[options] Main parameters

-I: Interactive mode operation. If the MV operation will result in overwriting an existing target file, then the system asks whether to overwrite it, asking the user to answer "y" or "n", which avoids overwriting the file by mistake.

-F: Disables interactive operation. MV operation to overwrite an existing target file without giving any indication, after specifying this parameter the I parameter will no longer work.

4. Application examples

(1) Move all files in the/USR/CBU to the current directory (".") Representation) in:

$ mv/usr/cbu/*.

(2) Rename the file Cjh.txt to Wjz.txt:

    1. $ mv Cjh.txt Wjz.txt

Ls

2. Use

The LS command is used to display directory contents, similar to the dir command under DOS, which is used by all users.

2. Format

LS [options][filename]

3.options Main parameters

-A,--all: do not hide anything with "." The character starts the item.

-A,--almost-all: list except ". "and". "Any item other than the".

--author: The author of each document is printed.

-B,--Escape: A non-printable character is represented in an octal overflow sequence.

--block-size= Size: The block is in bytes that specify < size >;.

-B,--ignore-backups: Do not list any items that end with the ~ character.

-F: Do not sort,-au parameter is valid,-LST parameter is invalid.

-F,--Classify: Add an indicator of the file type (*/[email protected]| one).

-g:like-l, but do

Not list owner.

-G,--no-group:inhibit display of group information.

-I,--inode: Lists the inode number for each file.

-I,--ignore= style: Do not print any items that match the shell character < style >;.

-K: that is--block-size=1k.

-L: Lists information in a longer format.

-L,--dereference: When displaying the file information of a symbolic link, displays the object indicated by the symbolic link, not the information of the symbolic link itself.

-M: All items are separated by commas and fill the entire row width.

-N,--numeric-uid-gid: Similar to-l, but lists UID and GID numbers.

-N,--literal: Lists the names of the unprocessed items, such as control characters that are not handled specifically.

-P,--file-type: plus an indicator of the file type (/[email protected]| one).

-Q,--quote-name: Enclose the project name in double quotation marks.

-R,--Reverse: in reverse order.

-R,--Recursive: Lists all subdirectory tiers at the same time.

-S,--size: In order of block size.

4. Application examples

The LS command is the most frequently used command in a Linux system, and its parameters are the largest of the Linux commands . There are several different colors when using the LS command, where Blue is the directory, green means executable, red is compressed, light blue is a linked file, bold black indicates a symbolic link, and gray is another format file. LS is most commonly used in ls-l,

The file type begins with a 10-character string. Where the first character represents a file type, it can be one of the following types:-(normal file), D (directory), L (symbolic Link), B (block device file), C (character device file). The following 9 characters represent the access rights of the file, divided into 3 groups and 3 bits per group. The first group represents the permissions of the file owner, the second group represents the permissions of the same group of users, and the third group represents the permissions of the other users. The three characters of each group represent the read (R), write (w), and Execute permissions (x) of the file respectively. For the directory, the access permission is indicated. s indicates that the UID or GID of the file is assigned to the UID (user ID) or GID (group ID) of the executing process when the file is executed. T indicates that the flag bit is set (left in memory, not swapped out). If the file is a directory, the files in that directory can only be deleted by the superuser, the directory owner, or the file owner. If it is an executable file, the pointer to its body segment remains in memory after the file executes. This will enable the system to load the file more quickly when it is executed again. The file size, build time, file, or command name are then displayed.

Diff

2. Use

The diff command is used for comparisons between two files, and indicates the difference between the two, and it uses permissions for all users.

2. Format

Diff

[Options] Source file destination file

3.[options] Main parameters

-A: treats all files as text files.

-B: Ignores the difference caused by whitespace.

-B: Ignores the difference caused by empty rows.

-C: Use the schema output format.

-H: Use heuristics to accelerate searches of large files.

-I: Ignores case changes.

-N

--RCS: Output RCS format.

Cmp

2. Use

The CMP ("compare" abbreviation) command is used to briefly indicate whether there are differences between the two files, and its use rights are for all users.

2. Format

Cmp[options] File name

3.[options] Main parameters

-L: Outputs bytes in decimal mode and facilitates the output of different two files in eight binary.

Cat

2. Use

The Cat ("concatenate" abbreviation) command is used to connect and display information about one or more of the files specified, and its use rights are for all users.

2. Format

Cat

[Options] File 1 file 2 ...

3.[options] Main parameters

-N: The number of rows for all outputs, starting with the first line.

-B: Similar to-n, except that blank lines are not numbered.

-S: A blank line that is substituted for a row when there are more than two consecutive lines of blank rows.

4. Application examples

(1) One of the simplest uses of the cat command is to display the contents of a text file. For example, we want to take a look at the contents of the Readme file at the command line and use the command:

    1. $ cat README

(2) Sometimes several files need to be processed into a single file, and the results of such processing will be saved to a separate output file. The cat command accepts one or more files on its input and prints them as a separate file to its output. For example, after adding a line number (blank line) to the file contents of the Readme and install, append the contents to a new text file File1:

    1. $ cat README INSTALL File1

(3) Another important feature of cat is the ability to number rows, as shown in Figure 2. This functionality is convenient for programming documentation, as well as legal and scientific documentation, and printing on the left line number makes it easy to reference a portion of a document, which is important in programming, scientific research, business reporting, and even legislative work.

Figure 2 Numbering with the cat command/etc/named.conf file

The line numbering function has a-B (can only be numbered for non-blank lines) and-n (can be numbered for all rows) two parameters:

    1. $ cat-b/etc/named.conf

ln

1. Role

The ln command is used to create a link between files, and its use rights are for all users.

2. Format

ln

[Options] source file [link name]

3. Parameters

-F: The source file is deleted when the link is closed.

-D: Allows system administrators to hard-link their own directories.

-S: Soft link (symbolic link).

-B: Files that will be overwritten or deleted at the time of the link are backed up.

There are two types of links, one called hard links , and the other called Symbolic links (symbolic link). By default, the LN command produces a hard link.

A hard join refers to a connection made through an index node. In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index). In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. In other words, the file is actually deleted.

In contrast to a hard connection, there is another connection in the Lnux system, called a symbolic connection (SYMBILC link), also known as a soft connection. A soft link file is a bit like a shortcut to Windows. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

Practice

Above we introduced the Linux file Processing command, here are a few examples, you can practice the command you just said.

1. Quick access to key directories with symbolic links

Symbolic links are a very useful feature. Suppose there are directories or files that need to be used frequently, but because of the file and directory structure of Linux, this file or directory is in a very deep subdirectory. For example, Apache

The Web server document is in the/usr/local/httpd/htdocs of the system and does not want to enter such a long path every time from the home directory (in fact, this path is very difficult to remember).

To solve this problem, you can create a symbolic link in the home directory so that you only have to enter the link when you need to enter the directory.

For easy access to the directory where the Web server (/usr/local/httpd/htdocs) document is located, the following commands can be used in the home directory:

    1. $ ln-s/usr/local/httpd/htdocs GG

This way, every time you enter the GG directory, you can access the Web server's documents, and if you no longer have access to the Web server 's documents, delete GG, and the real Web server's documentation is not deleted.

2. Use the DD command to import root.ram content in INIT.RD format into memory

DD if=/dev/fd0 OF=FLOPPY.FD

DD If=root.ram OF=/DEV/RAM0 #

3.grep Command System call

grep is one of the most widely used commands in Linux/unix, which can be called inside many Linux systems.

(1) If you want to query the directory in the directory list, the method is as follows:

    1. $ ls-l | grep ' ∧d '

(2) If you are querying a directory for all files that do not contain a directory, the method is as follows:

    1. $ ls-l | grep ' ∧[∧d] '

(3) Use the Find command to invoke grep, such as "Chinput" in all c source code, by doing the following:

    1. $find/zhxwin-name *.c-exec grep-q-s Chinput {} \;-print

written at the end: for Freedom look outside the world, as well as it this line, not to go to Google data, finally, Amway a V--PN agent. a red apricot VPN, to Google to check the information is the absolute first choice, the connection speed, the use is also convenient. I bought is 99¥ a year, through this link (http://my.yizhihongxing.com/aff.php?aff=2509) registered after the Member center to lose the coupon code, split down, only 7 yuan per month, special benefits.

This article tags: getting started with Linux Linux system Linux file system Linux Linux Common command internet talk

Turn from SUN's BLOG-focus on Internet knowledge, share the spirit of the Internet!

The original address: theLinux file system has a good structure, providing a lot of file handlers, so what are the commonly used file processing commands? "

Related reading: Aaron swartz– The internet genius of the life course: every moment asked himself, now the world what is the most important thing I can participate in doing? "
Related reading: " site environment Apache + PHP + mysql xampp, how to implement a server on the configuration of multiple sites?" "

Related reading: What is the engineer culture? Why are the engineers alive? Why do I need an engineer culture as an IT or internet company? "

Related reading: useful for programmers: 2017 latest in Google's Hosts file download and summary of the various hosts encountered the problem of the solution and configuration of the detailed

related reading: the Win10 perpetual activation tutorial and how can I see if the Windows system is permanently activated? 》

Related blog:SUN ' S blog -Focus on Internet knowledge and share the spirit of Internet! Go and see:www.whosmall.com

Original address: http://whosmall.com/?post=206

Linux file system has a good structure and provides a lot of file handlers, so what are the commonly used file processing commands?

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.