When getting started with Linux, 60 file processing commands are required.

Source: Internet
Author: User
Tags control characters printable characters egrep
When getting started with Linux, 60 file processing commands are required.

Linux system information is stored in files, which are similar to common official files. Each file has its own name, content, storage address, and other management information, such as the user and size of the file. A file can be a letter, an address book, or the program's source statement, program data, or even executable programs and other non-body content. The Linux File System has a good structure and provides many file processing programs. This section describes common file processing commands.

File

1. Role

File determines the file type by detecting the file content. The permission is applied to 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 the compressed file type.

-L: the connection is allowed.

-F name: read the list of file names to be analyzed from the file namefile.

4. Simple Description

You can use the file command to know whether a file is an executable file in binary (ELF format), a shell script file, or other formats. File can recognize the following file types: Directory, shell script, English text, binary executable file, C language source file, text file, DOS executable file.

5. Application Instance

If we see a file grap without a suffix, we can use the following command:

$ File grap

GRAP: English text

This is an English text file. It must be noted that the file command cannot detect multimedia file types, such as graphics, audio, and video.

Mkdir

1. Role

The mkdir command is used to create a subdirectory named dirname. Similar to the MD command in ms dos, the command has the permission of all users.

2. Format

Mkdir [Options] Directory Name

3. [Options] Main Parameters

-M, -- mode = mode: Set the permission <mode>;, similar to CHMOD.

-P, -- parents: Create the upper-level directory when necessary. If the directory already exists, it is not treated as an error.

-V, -- verbose: information is displayed every time a new directory is created.

-- Version: displays the version information and leaves.

4. Application Instance

You can set the directory permission when creating a directory. The "-M" parameter is used ". Assume that the directory to be created is named "Tsk", so that all users have the rwx (read, write, and execute permissions), you can use the following command:

$ Mkdir-M 777 TSK

Grep

1. Role

The grep command can specify the file to search for specific content and output the row standard output containing the content. Grep stands for global regular expression print, which indicates the global regular expression version. Its permission is granted to all users.

2. Format

Grep [Options]

3. Main Parameters

[Options] main parameters:

-C: only counts matching rows are output.

-I: case-insensitive (only applicable to single characters ).

-H: When querying multiple files, the file name is not displayed.

-L: When querying multiple files, only names containing matching characters are output.

-N: displays matching rows and row numbers.

-S: the error message that does not exist or does not match the text is not displayed.

-V: displays all rows that do not contain matched text.

Main Parameters of the regular expression pattern:

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

^: Match the start line of the regular expression.

$: Matches the end row of the regular expression.

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

\>;: Ends with the row that matches the regular expression.

[]: A single character. For example, [a] indicates that a meets the requirements.

[-]: Range, such as [A-Z], that is, a, B, c Until Z all meet the requirements.

. : All single characters.

*: It can contain 0 characters.

Regular Expressions are very important concepts in Linux/Unix systems. A regular expression (also known as "RegEx" or "Regexp") is a pattern that describes a type of strings ). If a string can be described using a regular expression, we can say that this character matches the regular expression ). This is similar to the wildcard "*" that represents any character in DOS. In Linux, regular expressions are usually used to find the text mode and perform the "search-Replace" operation and other functions on the text.

4. Application Instance

Querying DNS is one of the daily tasks. This means that you need to maintain a large number of IP addresses covering different networks. Sometimes the number of IP addresses exceeds 2000. If you want to view the NNN. NNN network address, but forget the rest of the second part, you only know that there are two periods, such as NNN nn... To extract all the NNN. nnn ip addresses, use [0-9] \ {3 \} \. [0-0 \ {3 \}\. It means that any number appears three times, followed by a sentence, followed by any number three times, followed by a sentence.

$ Grep '[0-9] \ {3 \} \. [0-0 \ {3 \} \ 'ipfile

In addition, the grep family also includes fgrep and egrep. Fgrep is a fix grep that allows you to search for strings rather than a pattern. egrep is an extended grep that supports basic and extended regular expressions, however, it does not support the application of the \ q mode and some more standardized models.

Dd

1. Role

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

2. Format

Dd [Options]

3. [opitions] Main Parameters

BS = byte: Forced IBS = <byte>; and OBS = <byte> ;.

CBS = byte: the specified <byte> for each conversion ;.

Conv = Keyword: convert a file by using a comma-separated keyword.

Count = number of blocks: only copy the input data of the specified <number of blocks>.

IBS = byte: Read the specified <byte>; each time ;.

If = file: Read <File>; content, rather than non-standard input data.

Obs = byte: Specify <byte> for each write ;.

Of = file: write data to the <File>, instead of standard output.

Seek = number of blocks: first, the output data of the specified <number of blocks> In OBS.

Skip = number of blocks: first, the input data of the specified <number of blocks> in IBS unit is skipped.

4. Application Instance

Dd commands are often used to create Linux boot disks. First 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:

$ Rdev vmlinuz/dev/hda

$ Dd If = vmlinuz of =/dev/fd0

The code above indicates that the rdev command directs the root device in the kernel vmlinuz to/dev/hda. Replace "hda" with your own root partition, run the DD command to write the kernel to a floppy disk.

Find

1. Role

The find command is used to search for files in a directory. It has the permission to all users.

2. Format

Find [path] [Options] [expression]

Path specifies the directory path. From here, the system looks down the file in the directory tree. It is a path list separated by spaces. If path is not written, the current directory is used by default.

3. Main Parameters

[Options] parameters:

-Depth: uses the depth-level search process to prioritize file content in a specified directory at a layer.

-Maxdepth levels: the level-level subdirectory of the Start directory. Level is a non-negative number. If level is 0, it indicates that it is only searched in the current directory.

-Mindepth levels: indicates that at least the level subdirectory of the Start directory is found.

-Mount: it is not found in directories and files of other file systems (such as msdos and vfat.

-Version: print the version.

[Expression] is a matching expression accepted by the find command. All operations of the find command are for the expression. It has many parameters. Here we only introduce some common parameters.

-Name: The wildcard * And? are supported ?.

-Atime N: searches for files read in the past n days.

-Ctime N: searches for files modified in the past n days.

-Group grpoupname: searches for all grpoupname files in the group.

-User User name: search for all files whose owner is the user name (ID or name.

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

-Print: outputs and prints the search results.

4. Application Skills

Find command to find files:

(1) Search by file name

For example, to find a file named Lilo. conf, run the following command:

Find/-name Lilo. conf

"/" After the find command indicates searching the entire hard disk.

(2) Quick file search

It takes a long time to find a file based on the file name, especially when large Linux file systems and large-capacity hard disk files are placed in a deep subdirectory. If we know that this file is stored in a directory, we can save a lot of time by searching in this directory. For example, the smb. conf file can be determined by its file suffix ". conf" as a configuration file, which should be in the/etc directory. You can run the following command:

Find/etc-name smb. conf

In this way, you can use the "quick file search" method to shorten the time.

(3) searching based on some file names

Sometimes we know that only one file package contains the four characters of abvd. To find all the files containing the four characters in the system, run the following command:

Find/-name '* abvd *'

After entering this command, the Linux system will find all the files containing the four characters abvd in the/directory (where * is a wildcard ), for example, all files that meet the conditions, such as abvdrmyz, can be displayed.

(4) Searching for files using hybrid search

The find command can use a hybrid search method. For example, we want to search for a file larger than 500000 bytes in the/etc directory and modify it within 24 hours, you can use-and (and) to link two search parameters into a hybrid search method.

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

MV

1. Role

The MV command is used to rename a file or directory, or move the file from one directory to another. Its permission is granted to all users. This command is like a combination of Ren and move in the doscommand.

2. Format

MV [Options] source file or directory target file or directory

3. [Options] Main Parameters

-I: interactive operation. If the MV operation will overwrite the existing target file, the system will ask whether to rewrite the file and ask the user to answer "y" or "N". This will avoid overwriting the file by mistake.

-F: Disable interactive operations. When an MV operation overwrites an existing target file, no indication is given. If this parameter is specified, the I parameter will no longer work.

4. Application Instance

(1) Move all files in/usr/CBU to the current directory (represented:

$ MV/usr/CBU /*.

(2 rename the file cjh.txtto wjz.txt:

$ MV cjh.txt wjz.txt

Ls

1. Role

The LS command is used to display the directory content. Similar to the Dir command in DOS, it has the permission to use all users.

2. Format

Ls [Options] [filename]

3. Main options Parameters

-A, -- all: Do not hide any items starting with the "." character.

-A, -- almost-ALL: list any projects except "." and.

-- Author: prints the author of each file.

-B, -- escape: an octal overflow sequence to indicate non-printable characters.

-- Block-size = size: the block size is measured in bytes of the specified <size>.

-B, -- ignore-backups: do not list any ~ The project whose character ends.

-F: No sorting is performed. The-Au parameter takes effect and the-LST parameter is invalid.

-F, -- classify: add the file type indicator (*/= @ | one of them ).

-G: Like-l, but do not list owner.

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

-I, -- inode: List inode numbers of each file.

-I, -- ignore = style: do not print any project that meets the shell universal character <style>.

-K: -- block-size = 1 K.

-L: Used to list information in a long format.

-L, -- dereference: when the file information of the symbolic link is displayed, the object indicated by the symbolic link is displayed, not the information of the symbolic link itself.

-M: All items are separated by commas (,) and the whole row width is filled.

-N, -- Numeric-UID-gid: similar to-l, but list UID and GID.

-N, -- literal: List unprocessed project names, for example, not specially processed control characters.

-P, -- file-type: add the file type indicator (/= @ | one of them ).

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

-R, -- reverse: in reverse order.

-R, -- Recursive: Lists All Sub-directory layers at the same time.

-S, -- size: Order the block size.

4. Application Example

The LS command is the most frequently used command in Linux, and its parameters are also the most frequently used commands in Linux. When using the LS command, there will be several different colors, in which Blue indicates a directory, Green indicates an executable file, red indicates a compressed file, and light blue indicates a linked file, the bold black indicates the symbolic link, and the gray indicates other format files. The most common use of LS is LS-l, as shown in figure 1.

Figure 1 use the LS-l command

The start of the file type is a string consisting of 10 characters. The first character indicates the file type. It can be one of the following types:-(common file), D (directory), L (symbolic link), and B (Block device file) and C (character device files ). The following nine characters indicate the object access permissions. They are divided into three groups, each with three characters. The first group indicates the permissions of the file owner, the second group indicates the permissions of users in the same group, and the third group indicates the permissions of other users. The three characters in each group indicate the read (R), write (w), and execution permission (x) of the object respectively ). For a directory, it indicates the access permission. S indicates that when the file is executed, the UID or GID of the file is assigned to the UID (User ID) or GID (group ID) of the execution process ). T indicates that the flag is set (left in memory and not swapped out ). If the file is a directory, the files in the directory can only be deleted by the Super User, directory owner, or file owner. If it is an executable file, after the file is executed, the pointer pointing to its body segment remains in the memory. In this way, the system will be able to mount the file more quickly when you execute it again. The file size, generation time, file or command name is displayed.

Diff

1. Role

The diff command is used to compare two files and indicate the differences between the two. The permission is granted to all users.

2. Format

Diff [Options] target file of the source file

3. [Options] Main Parameters

-A: process all files as text files.

-B: Ignore spaces.

-B: Ignore empty rows.

-C: Use the outline output format.

-H: Use the test method to accelerate search for large files.

-I: Ignore case sensitivity changes.

-N -- RCS: the format of the output RCS.

CMP

1. Role

The CMP ("Compare") command is used to briefly identify whether two files are different. Its permission is granted to all users.

2. Format

CMP [Options] File Name

3. [Options] Main Parameters

-L: outputs bytes in decimal format, and outputs different octal values in two files.

Cat

1. Role

The cat ("concatenate") command is used to connect to and display information about one or more specified files. It has the permission to use all users.

2. Format

Cat [Options] File 1 file 2 ......

3. [Options] Main Parameters

-N: Number of all output rows starting from the first line.

-B: similar to-N, except that the blank row is not numbered.

-S: When there are two or more consecutive blank rows, it is replaced by a blank row.

4. Application Example

(1) The simplest use of the cat command is to display the content of a text file. For example, you can run the following command to view the content of the README file:

$ Cat readme

(2) Sometimes several files need to be processed into one file, and the processing result is saved to a separate output file. The cat command accepts one or more files on the input and prints them as a separate file to its output. For example, add the row number to the content of the README and install files (without blank lines), and then append the content to the new text file file1:

$ Cat readme install file1

(3) Another important function of cat is to number rows, as shown in figure 2. This function is convenient for the compilation of program documents and legal and scientific documents. printing the row number on the left makes it easy to compile a part of the reference document, these are important in programming, scientific research, business reports, and even legislation.

Figure 2 use the cat command/etc/named. conf file for numbering

The row numbering function has two parameters:-B (only numbers can be performed on non-blank rows) and-N (numbers can be performed on all rows:

$ Cat-B/etc/named. conf

Ln

1. Role

The ln command is used to create links between files. Its permission is for all users.

2. Format

Ln [Options] source file [Link name]

3. Parameters

-F: when the link is completed, the source file is deleted first.

-D: allows system administrators to hard link their directories.

-S: Symbolic Link ).

-B: Back up the files that will be overwritten or deleted during the link.

There are two types of links: hard link and symbolic link ). By default, the ln command generates a hard link.

Hard connection refers to the connection through the index node. In a Linux File System, a file stored in a disk partition is assigned with an index node number (inode index) No matter what type it is ). In Linux, multiple file names direct to the same index node. Generally, this type of connection is hard connection. A hard connection allows a file to have multiple valid path names. In this way, you can create a hard connection to an important file to prevent "accidental deletion. The reason is as described above, because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the connection between the index node and other nodes. Only when the last connection is deleted will the connection of the data block and directory of the file be released. That is to say, the object will be deleted.

In contrast to hard connection, there is another connection in the lnux system, called symbilc link. Soft-link files are similar to Windows shortcuts. It is actually a type of special file. In a symbolic connection, a file is actually a text file containing the location information of another file.

Hands-on contact

We have introduced the Linux File Processing commands. The following describes several instances. You can practice the commands you just mentioned.

1. Use symbolic links to quickly access key Directories

Symbolic Links are a very useful feature. It is assumed that some directories or files need to be frequently used, but these files or directories are in deep subdirectories due to Linux Files and directory structures. For example, the Apache Web Server File is located in/usr/local/httpd/htdocs of the system and does not want to enter such a long path from the main directory every time (in fact, this path is also hard to remember ).

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

To conveniently enter the directory of the Web server (/usr/local/httpd/htdocs) document, run the following command in the main directory:

$ Ln-S/usr/local/httpd/htdocs gg

In this way, you can access the Web server documents every time you enter the GG directory. If you no longer access the Web server documents, you can delete the GG files, but the real Web Server documents are not deleted.

2. Run the DD command to import the root. Ram content in init. RD format to the 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. It can be called in many Linux systems.

(1) to query the directories in the directory list, use the following method:

$ LS-L | grep 'cmdd'

(2) If you query all files in a directory that does not contain the directory, the method is as follows:

$ LS-L | grep 'partition [partition D]'

(3) Use the find command to call grep, such as "chinput" in all C source code. The method is as follows:

$ Find/zhxwin-name *. C-exec grep-Q-s chinput {}\;-print

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.