60 required Linux commands [2] file processing

Source: Internet
Author: User
Tags printable characters
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.

Linux system information is stored in files, which are similar to common official files. Each file has its own name
Storage address and other management information, such as the file user and file size. 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. The system provides many file processing processes and File Processing commands.

File

1. determine the file type of the role Content. The permission is granted to all users.

2. Format

File

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) or a Shell.
Script file, or other formats. File can recognize directories, Shell scripts, and English files.
Binary executable files, C language source files, text files, DOS executable files.

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 images, audios,
Video and other multimedia file types.

Mkdir

1. Role

The mkdir command is used to create a subdirectory named dirname. similar to the md command in ms dos, mkdir has the permission to use it.
All users.

2. Format

Mkdir [options] directory name

3. [options] main parameters

-M, -- mode = mode: set permissions <模式> , 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
Is "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
Global Regular Expression Print, which indicates the version of the Global Regular Expression. its permission is applied 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 matching 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. Regular expressions (also known as "regex" or"
Regexp ") is a Pattern that describes a type of strings ). If a string can use a positive
Then the expression is used to describe. we will say that this character matches the regular expression (Match ). This can be used with DOS users
Use the wildcard "*" to represent any character. In Linux, regular expressions are usually used to search for the module of text.
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 IP address
More than 2000. If you want to view the nnn. nnn network address, but forget the rest of the second part, you only know that
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 the fix grep, allowing you to search for strings instead of
Mode; egrep is an extended grep that supports basic and extended regular expressions, but does not support the application of q mode range and
Corresponding to 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 = <字节> And obs = <字节> .

Cbs = byte: the specified <字节> .

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

Count = number of blocks: only copy specified <块数目> .

Ibs = byte: Read the specified <字节> .

If = File: Read <文件> Content, rather than non-standard input data.

Obs = byte: specify <字节> .

Of = File: write data <文件> Instead of standard output.

Seek = number of blocks: skipped the specified value in obs. <块数目> .

Skip = number of blocks: skipped the specified value in ibs <块数目> .

4. application instance

Dd commands are often used to create Linux boot disks. First, find a bootable kernel to direct its root device to the correct root partition.
And then use the dd command to write it to a floppy disk:

$ Rdev vmlinuz/dev/hda

$ Dd if = vmlinuz of =/dev/fd0

The above code indicates that the rdev command directs the root device in the kernel vmlinuz to/dev/hda"
Replace it with your own root partition, and then use the dd command to write the kernel into 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 list of paths used by each other.
Space separation. 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 it is only found 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 expressions
. 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 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.
Linux file systems and large-capacity hard drive files are placed in deep subdirectories. If we know that this file is stored in
In a directory, you only need to look down in this directory to save a lot of time. For example, the smb. conf file
The file suffix ". conf" can be used to determine that this is a configuration file, so it should be in the/etc directory.
Run the following command:

Find/etc-name smb. conf

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

(3) searching based on some file names

Sometimes we know that only one file package contains the four characters of abvd, so we need to find all the files containing the four characters in the system.
You can enter 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 (
* Is a wildcard), such as abvdrmyz and other eligible files can be displayed.

(4) searching for files using hybrid search

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

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
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 overwrites an existing target file, the system checks whether the target file is duplicated.
Write, ask the user to answer "y" or "n", this can avoid overwrite 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
The number 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: block to specify <大小> In bytes.

-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 Shell-compliant universal characters <样式> Project.

-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, and
The information of the unsigned link.

-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. Use the ls command
There are several different colors, blue indicates a directory, green indicates an executable file, and red indicates a compressed file.
Files, light blue indicates a link file, bold black indicates a symbolic link, and gray indicates a file of other formats. Ls is the most common
Use ls-l, as shown in.

 
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, which can be the following
Type:-(common file), d (directory), l (SYmbolic LinK), B (block device file), c (character device file
). The following nine characters indicate the object access permissions. they are divided into three groups, each with three characters. The first group indicates the ownership 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. Three characters in each group
Read (r), write (w), and execute (x) of files ). For a directory, it indicates the access permission. S indicates that when the file is executed
When running, assign the UID or GID of the file to the UID or GID of the execution process ). T indicates setting the tag
Location (left in memory, not swapped out ). If the file is a directory, the file in this directory can only be named by the Super User.
Delete the directory owner or file owner. If it is an executable file, after the file is executed, it points to its body segment
The pointer 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 is displayed.
Small, generation time, file or command name.
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: format of the output RCS.

Cmp

1. Role

The cmp (abbreviation of "compare") command is used to briefly identify whether two files are different, and its permission to use is all
User.

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 (abbreviation of "concatenate") command is used to connect and display information about one or more specified files. it
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, let's take a look at the command line.
For the content of the README file, run the following command:

$ 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 its input and prints them as a separate file to its input
Output. For example, add the row number to the content of the README and INSTALL files (blank rows are not added), and then add the content
New text file File1:

$ Cat readme install File1

(3) another important function of cat is to number rows, as shown in. This function is applicable to program documents.
The compilation of legal and scientific documents is very convenient, and the line number printed on the left makes a part of the reference document change
It's easy. these are important in programming, scientific research, business reporting, and even legislation.

 
Use the cat command/etc/named. conf file for serial numbers
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 ).
Quantity:

$ 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, the files stored in the disk partition are not
An Index node number (Inode Index) is assigned to all types of tubes ). In Linux, multiple files
A name pointing to the same index node exists. Generally, this type of connection is hard connection. The purpose of hard connection is to allow a file to have
There are multiple valid path names, so that you can create a hard connection to important files to prevent "accidental deletion. Original
Because as described above, there is more than one connection to the index node of the Directory. Deleting only one connection does not affect the index.
The node itself and other connections. only when the last connection is deleted will the data block and directory connection of the file be
Release. 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.
Connection. Soft-link files are similar to Windows shortcuts. It is actually a type of special file. In symbolic connection
The file is actually a text file, which contains the location information of another file.

Hands-on contact

We have introduced the Linux file processing commands above. we will introduce several instances below. you can start to practice them as we mentioned earlier.
.

1. use symbolic links to quickly access key directories

Symbolic links are a very useful feature. Assume that some directories or files need to be frequently used.
File or directory structure. this file or directory is in a very deep subdirectory. For example, Apache Web Server Documentation
Located in/usr/local/httpd/htdocs of the system, and do not want to enter such a long path from the main directory every time
Path (in fact, this path is not easy to remember ).

To solve this problem, you can create a symbolic link in the main directory.
Enter this link.

In order to easily access the directory where the Web server (/usr/local/httpd/htdocs) document is located, you can
Run the following command:

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

In this way, you can access the Web server documentation every time you enter the gg Directory. if you no longer access the Web server documentation, delete
In addition to gg, the documents of the real Web server 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

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.