Linux File Content Check

Source: Internet
Author: User
Tags touch command

Linux File Content Check
View the content of a file directly: cat/tac/nl command cat (concatenate)

# Cat [-AbEnTv]

Options and parameters:

-A: similar to the integration option of-vET, some special characters can be listed rather than blank;

-B: list the row numbers. Only the row numbers are displayed for non-blank rows, and the blank rows are not marked;

-E: Display The End Of The Broken row byte $;

-N: print the travel number, along with the blank line, there will also be a row number, different from the option of-B;

-T: Click the [tab] button to display it with ^ I;

-V: list special characters that cannot be seen

 

Example 1: Check the content of the/etc/issue file.

# Cat/etc/issue

CentOS release 5.3 (Final)

Kernel \ r on an \ m

 

Example 2: attach the question. What if you want to print the row number?

# Cat-n/etc/issue

1 CentOS release 5.3 (Final)

2 Kernel \ r on an \ m

3

If you do not want to orchestrate the row number of the blank line, you can use "cat-B/etc/issue" to test it by yourself.

 

Example 3: display the content of/etc/xinetd. conf in full (including special bytes)

# Cat-A/etc/xinetd. conf

Tac (reverse list)

# Tac/etc/issue

Kernel \ r on an \ m

CentOS release 5.3 (Final)

Nl (add row number printing)

# Nl [-bnw] File

Options and parameters:

-B: Specifies the row number in two ways:

-B a: indicates that the row number (similar to cat-n) is also listed no matter whether the row is empty or not );

-B t: if there is a blank row, do not list the row number (default) for the empty row );

-N: Used to list row numbers. There are three methods:

-N ln: the row number is displayed on the leftmost side of the screen;

-N rn: the row number is displayed on the rightmost side of its own column without 0;

-N rz: the row number is displayed on the rightmost side of its own column with a value of 0;

-W: The number of digits occupied by the row number column.

 

Example 1: Use nl to list/etc/issue content

# Nl/etc/issue

1 CentOS release 5.3 (Final)

2 Kernel \ r on an \ m

 

Note: This file actually has three rows, and the third row is blank (no bytes). Because it is a blank row, no row is added to nl. If you are sure to add the row number, you can do this:

# Nl-B a/etc/issue

1 CentOS release 5.3 (Final)

2 Kernel \ r on an \ m

3

So what if we want to enable auto-add 0 to the front of the row number? Yes.

# Nl-B a-n rz/etc/issue

000001 CentOS release 5.3 (Final)

000002 Kernel \ r on an \ m

000003

It is automatically set to 0 in the place of its own column. The default column is six digits. If you want to change it to three digits?

[Root @ www ~] # Nl-B a-n rz-w 3/etc/issue

001 CentOS release 5.3 (Final)

002 Kernel \ r on an \ m

003

It becomes only three digits.

 

For more information, see more and less # more/etc/man. config.

#

# Generated automatically from man. conf. inby

# Configure script.

#

# Man. conf from man-1.6d

... (Omitted in the middle )....

-- More -- (28%) <= focuses on this line! Your cursor will be here waiting for your command

Blank key (space): Indicates turning one page down;

Enter: Indicates turning "One Line" down 』;

/String: searches for the keyword "string" in the displayed content;

: F: displays the file name and the number of currently displayed rows immediately;

Q: The system immediately leaves more and the file content is no longer displayed.

B or [ctrl]-B: Indicates turning back the page. However, this action is only useful to files and useless to pipelines.

 

Less (one page and one page flip)

Blank key: Scroll down a page;

[Pagedown]: Scroll down a page;

[Pageup]: returns a page up;

/String: searches down for the "string" function;

? String: the "string" search function is enabled;

N: Repeat the previous search (and/or? Related !)

N: Repeat the previous search (and/or? Related !)

Q: Leave the less program;

 

Data extraction head (extract the first few rows)

# Head [-n number] File

Options and parameters:

-N: Followed by a number, which indicates the meaning of several rows.

 

# Head/etc/man. config

By default, the first 10 rows are displayed. to display the first 20 rows, you must:

# Head-n 20/etc/man. config

 

Example: If the data in the last 100 rows is not printed, only the first few rows of/etc/man. config are printed. How can this problem be solved?

# Head-n-100/etc/man. config

If the parameter after the-n option is followed by a negative number, for example, in the example above-n-100, it indicates all the rows before the column, but does not include the following 100 rows. For example,/etc/man. config has a total of 141 lines, then the above command "head-n-100/etc/man. the first 41 rows are listed, and the last 100 rows are not printed.

Tail (retrieve the following rows)

# Tail [-n number] File

Options and parameters:

-N: Followed by a number, which indicates the meaning of several rows.

-F: indicates the file name followed by the continuous detection. The tail detection will not be completed until you press [ctrl]-c.

 

# Tail/etc/man. config

By default, the last 10 rows are displayed. to display the last 20 rows, you must:

# Tail-n 20/etc/man. config

 

Example 1: If you do not know the number of rows in/etc/man. config, but only want to list data after 100 rows?

# Tail-n + 100/etc/man. config

 

Example 2: continuous detection of/var/log/messages

# Tail-f/var/log/messages

<= The tail command will not be left until you enter [crtl]-c.

 

Non-plain text file: od

# Od [-t TYPE] File

Options or parameters:

-T: can be followed by various types of output, such:

A: use the default byte for output;

C: Uses ASCII bytes for output.

D [size]: Uses decimal to output data. Each integer occupies size bytes;

F [size]: floating is used to output data. Each number occupies size bytes;

O [size]: Uses octal to output data. Each integer occupies size bytes;

X [size]: Uses hexadecimal to output data. Each integer occupies size bytes;

 

Example 1: use ASCII to display/usr/bin/passwd content!

# Od-t c/usr/bin/passwd

0000000 177 e l f 001 001 001 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0

0000020 002 \ 0 003 \ 0 001 \ 0 \ 0 \ 0 260 225 004 \ B 4 \ 0 \ 0 \ 0 \ 0

0000040 020 E \ 0 \ 0 \ 0 \ 0 \ 0 4 \ 0 \ 0 \ a \ 0 (\ 0

0000060 035 \ 0 034 \ 0 006 \ 0 \ 0 \ 0 4 \ 0 \ 0 \ 0 4200 004 \ B

0000100 4 200 004 \ B 340 \ 0 \ 0 340 \ 0 \ 0 \ 0 005 \ 0 \ 0 \ 0 \ 0

... (Omitted later )....

The first column on the left represents the number of bytes in octal. For the above example, column 0000020 in the second column indicates that

The meaning of the 16th byes (2x8) content.

 

Example 2: Use the/etc/issue file to list the storage value and ASCII table in octal.

# Od-t oCc/etc/issue

0000000 103 145 156 164 117 040 123 162 145154 145 141 163 040 065

C e n t o s r e l e a s e 5

0000020 056 062 040 050 106 151 156 141 012 154051 113 145 162 156 145

. 2 (F I n a l) \ n K e r n e

0000040 154 040 134 162 040 157 156 040 141156 040 134 012 012

L \ r o n a n \ m \ n

0000057

As shown above, we can find out what values can be mapped to each byte. For example, the record value of e is 145, which is converted to decimal place: 1x8 ^ 2 + 4x8 + 5 = 101.

 

Modify the file time or create a new file: touch

There are actually three major changes in linux.

Modification time (mtime ):
This time will be upgraded when the "content data" of this file is changed! Content data refers to the file content, not the attributes or permissions of the file!

Status time (ctime ):
When the "status" of the file changes, the time will be upgraded. For example, if the permissions and attributes are changed, the time will be upgraded.

Access time (atime ):
When "the content of the file is used", the read time (access) is upgraded ). For example, if you use cat to read/etc/man. config, The atime of the file will be upgraded.

 

# Ls-l/etc/man. config

-Rw-r -- 1 root 4617 Jan 6 2007/etc/man. config

# Ls-l -- time = atime/etc/man. config

-Rw-r -- 1 root 4617 Sep 25 17:54/etc/man. config

# Ls-l -- time = ctime/etc/man. config

-Rw-r -- 1 root 4617 Sep 4 18:03/etc/man. config

By default, ls displays the mtime of the file, that is, the last time the file content was changed.

 

# Touch [-acdmt] File

Options and parameters:

-A: only modify accesstime;

-C: only the time when the file is modified. If the file does not exist, no new file is created;

-D: the date to be revised, instead of the current date, can be followed by -- date = "date or time"

-M: only modify mtime;

-T: The time to be modified later than the current time. The format is [YYMMDDhhmm]

 

Example 1: Create an empty file and observe the time

# Cd/tmp

# Touch testtouch

# Ls-l testtouch

-Rw-r -- 1 root 0 Sep 25 21: 09 testtouch

Note that the file size is 0. By default, if there is a file after touch, the three times (atime/ctime/mtime) of the file will be upgraded to the current time. If the file does not exist, a new empty file is automatically created.

 

Example 2 ~ /. Copy bashrc to bashrc. Assume that the replication is complete and check its date.

# Cp-~ /. Bashrcbashrc

# Ll bashrc; ll -- time = atime bashrc; ll -- time = ctime bashrc

-Rw-r -- 1 root 176 Jan 6 2007 bashrc <= This is mtime

-Rw-r -- 1 root 176 Sep 25 21: 11 bashrc <= This is atime

-Rw-r -- 1 root 176 Sep 25 21: 12 bashrc <= This is ctime

 

Example 3: Modify the bashrc file of Case 2 and set the date to two days ago.

# Touch-d "2 days ago" bashrc

# Ll bashrc; ll -- time = atime bashrc; ll -- time = ctime bashrc

-Rw-r -- 1 root 176 Sep 23 21: 23 bashrc

-Rw-r -- 1 root 176 Sep 23 21: 23 bashrc

-Rw-r -- 1 root 176 Sep 25 21: 23 bashrc

 

Example 4: Change the bashrc date of the previous example

# Touch-t 1509150202 bashrc

# Ll bashrc; ll -- time = atime bashrc; ll -- time = ctime bashrc

-Rw-r -- 1 root 176 Sep 15 2015 bashrc

-Rw-r -- 1 root 176 Sep 15 2015 bashrc

-Rw-r -- 1 root 176 Sep 25 21: 25 bashrc

Note that the date changes in atime and mtime, but ctime records the current time.

 

In any case, the touch command is most commonly used:

Create an empty file;

Revise a file to the current date (mtime and atime)

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.