View the entire file under Linux __linux

Source: Internet
Author: User
Tags compressed file types

mainly include: File, Cat, head, tail, more, less, umask, WC

Llinux contains many commands that help us read files in different ways. For example, we can verify the different types of files, you can read from the top or the bottom of the file, and you can limit the reading in a few lines, you can set read-through, of course, we can count the number of lines in a file, words and characters, in addition to some of the search file commands

1.file Command

File type is determined by probing the contents of the file, with permissions being all users.

Usage:

file [options] File name

[Options] Main parameters

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

-Z: Probing for compressed file types.

-L: Allows connections to be met.

-F Name: Reads the list of file names to parse from the file NameFile.

2.cat Command

The Cat ("concatenate" abbreviation) command is used to connect and display information about one or more specified files, and it has permission to use for all users.

Usage:

Cat [Options] file 1 file 2 ...

[Options] Main parameters

-N: Number of rows for all outputs starting with the first line.

-B: Similar to-N, but not numbered for blank lines.

-S: When a blank row with two consecutive lines is encountered, it is substituted to a row of blank lines.

This command can read multiple files sequentially, using: Cate File1 file2

Cat is a text file view and Connection tool. Check the contents of a file, with cat is simpler, is the cat directly after the file name.

Like what:
De>[root@localhost ~]# Cat/etc/fstab de>

In order to facilitate the novice brother to master this tool flexibly, we say a little more commonly used parameters;


2.0 CAT grammatical structure;
de>cat [Options] [file] ... de>

Options

-A,--show-all is equivalent to-vet

-B,--number-nonblank to Non-null output line number-

e equivalent to-ve-

E,--show-ends displays $-N at the end of each line

,--number For all row numbers of the output-

s,--squeeze-blank does not output multiple lines of empty rows-

t and-vt equivalence-

T,--show-tabs displays the jump characters as ^i-

u (ignored)-

V,--show-n Onprinting use ^ and M-references, except LFD and TAB

--help Display this help information and leave


2.1 Cat to view file content instances;
De>[root@localhost ~]# cat/etc/profile Note: View the contents of the pro file file in the/etc/directory;
[Root@localhost ~]# cat-b/etc/fstab Note: View the pro file content in the/etc/directory, and number the nonblank lines, starting with the 1 line number;
[Root@localhost ~]# cat-n/etc/profile Note: A numbered output display for all rows (including blank lines) of pro file in the/etc directory;
[Root@localhost ~]# cat-e/etc/profile Note: View the pro file content under/etc/and append the $ symbol at the end of each line; de>

Cat plus parameter-N and NL tools are similar, the file content output at the same time, will precede each line with line number; De>[root@localhost ~]# cat-n/etc/profile
[Root@localhost ~]# Nl/etc/profile de>

Cat can display the contents of multiple files at the same time, for example, we can display the contents of two files at the same time on a cat command; De>[root@localhost ~]# cat/etc/fstab/etc/profile de>

Cat for the content of a large file, you can through the pipeline | To the more tools, and then a page to view; De>[root@localhost ~]# Cat/etc/fstab/etc/profile | More de>


2.2 Cat's creation, connection file function example;


Cat has the ability to create files, after creating files, to end with EOF or stop;
de>[root@localhost ~]# cat>    linuxsir.org.txt   <<EOF    Note: Create Linuxsir.org.txt file;
> I'll test the cat to create the file and enter the contents for the file;              NOTE: This is for linuxsir.org.txt file input;
> North-South South-North Test;          & nbsp                           NOTE: This is for linuxsir.org.txt file input;
>EOF      Note: Exit Edit status;

[root@localhost ~]# catlinuxsir.org.txt    Note: Let's look at the contents of the Linuxsir.org.txt file;

I'll test the cat to create the file and enter the contents for the file;
North-South south-North test; de>


Cat connects the contents of multiple files and outputs to a new file;

Suppose we have sir01.txt, Sir02.tx and Sir03.txt, and the contents are as follows; De>[root@localhost ~]# Catsir01.txt
123456
I am testing

[Root@localhost ~]# Cat Sir02.txt
56789
Beinan tested

[Root@localhost ~]# Cat Sir03.txt
09876
linuxsir.org Testing de>

I want to connect Sir01.txt, Sir02.txt and sir03.txt three files with cat (that is, put the contents of all three files together) and output them to a new file sir04.txt.

Note: the principle is to connect the contents of three files, create sir04.txt files, and write the contents of several files into Sir04.txt. In particular, it is worth mentioning that

If you enter an existing Sir04.txt file, the Sir04.txt content is emptied. De>[root@localhost ~]# cat Sir01.txtsir02.txt sir03.txt > Sir04.txt

[Root@localhost ~]# more Sir04.txt
123456
I am testing
56789
Beinan tested
09876
linuxsir.org Testing de>

Cat appends one or more existing file contents to an existing file de>[root@localhost ~]# catsir00.txt
Linuxsir.org Forever

[Root@localhost ~]# cat sir01.txt sir02.txt sir03.txt>> sir00.txt

[Root@localhost ~]# Cat Sir00.txt
Linuxsir.org Forever
123456
I am testing
56789
Beinan tested
09876
linuxsir.org Testing de>

3.head and Tail command

The head and tail commands can view the contents of a file from the top or bottom respectively, and the difference is that one is starting from the beginning.

Usage:

Head 300b test.txt View the first 300 bytes of the Text.txt file

Head test.txt View Text.txt file's top 3MB

Tail-n55 Test.txt View the following 55 lines of Text.txt file

Tail 4k test.txt View text.txt file after 4KB bytes

4.more and less command

Similar to head and tail commands but somewhat different, more and less commands are not more or less polarized. They all start at the opening of a text file. When you execute these two commands on a text file, we will look at the contents of the file one page at a time, all of which allow you to use the PageUp and PageDown on the keyboard, and just press Q or CTRL + C to exit. Less command also has the advantage of VI editing program, you can use some VI command to search files

Usage:

More Test.txt View the contents of the file Text.txt

Less test.txt View the contents of the file Text.txt

Description: ①less did not read the entire file at the beginning, so when the large file opened, it will be faster than the General instrument editor (such as VI). ② If less cannot view Chinese, you need to set the environment variable: Export lesscharset=iso8859.

5.umask Command

When creating a new file or directory, the permissions we gain depend on the so-called Umask value, type umask in the command-line interface, and you will see the current digital mask (masked) value of the permission

such as input: #umask

Results obtained: 0022

For the understanding of mask numbers, the first number in Nmask is not currently in use, so the actual umask is 022. Umask does not allow users to configure execution (x=1) permissions on any file, and each file's mask value is like 777 minus 644, which is 022 and rw--r--r. It's also said that for a new file, the owner has read and write permissions, and the owner group and other members only have read rights.

6.WC Command

The WC command is used for statistics, and is easier to understand. The WC command can be a text file for line, character, and word statistics.

Usage: Execution Results

Wc-l Test.txt Statistics test.txt The number of rows in a file

Wc-c Test.txt Statistics Test.txt The number of characters in a file

Wc-w Test.txt Statistics Test.txt The number of words in a file

7. Sed command

To view the middle section of a file, you can use the SED command, such as:

Sed-n ' 5,10p '/etc/passwd

This allows you to view only the 5th line to the 10th line of the file.

Some other tips:

1. Output redirection:

Tail-f a.txt >> B.txt & 2. Exit: Ctrl + C or Ctrl+break

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.