Linux file creation, viewing, editing commands

Source: Internet
Author: User
Tags first string touch command

First, create a file command

1. Touch command

Linux touch commands are not commonly used, and are generally used when using make, to modify file timestamps, or to create a new file that does not exist.
Touch [-acm][-r ref_file (Reference file) |-t time (value)] File (file name)
Example
Touch file1.txt update file1.txt access and modification times
Touch-c file1.txt If File1.txt does not exist, the file is not created
Touch-r ref_file file1.txt update file1.txt timestamp and Ref+file same
Touch-t 0811142234.50 File1.txt The time to set the file is November 14, 08 22:34 40 seconds

Touch filename Creates an empty text file named filename.

2, VI

For example, create a new 1.txt text file.
Direct #vi 1.txt is possible.

3. CP Copy

4, MV

Second, view the file command

1. Cat (View only)

The Cat ("concatenate" abbreviation) command is used to connect and display the contents of the specified file or files, and its usage rights are for all users.

Usage: Cat [Options] file 1 file 2 ...

If you use a pipe to receive information, the file name is not followed by cat.

[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.

This command can read multiple files sequentially, usage: Cat file1 file2

Example A: Add the Textfile1 file contents to the line number and enter the Textfile2 file.
#cat-N textfile1 > Textfile2

Example B: Append the contents of the Textfile1 and Textfile2 files to the textfile3 after adding the line number (blank line not added)
#cat-B textfile1 textfile2 >> textfile3

Example C: Displays the contents of the/etc/fstab file.
#cat/etc/fstab

For files with great content, cat can be routed through the pipeline | To more tools, and then to a page-by-page view;
[Email protected] ~]# Cat/etc/fstab/etc/profile | More

2, vi (View and edit)
such as: Vi/etc/fstab

3, head view file Header content command.
Command format: # head [[Options]] < file list >
Common options:

-C,--bytes=size, prints the size byte content at the beginning of the file.

-N,--lines=number, prints the number line at the beginning of the file, and defaults to the first 10 lines of output.

Example:

Head-c 300/etc/inittab Show First 300 bytes

Head-n 5/etc/inittab Show Top 5 lines

Head/etc/inittab Show Top 10 lines

4, tail view file tail content command.
Command format: # tail [Options] < file list >
Common options:
-C,--bytes=size, prints the last size byte content of the file.

-N,--lines=number, prints the last number line of the file, the default is 10 lines after the output.

Example:
Tail/etc/inittab Show last 10 lines
Tail-n 5/etc/inittab Show last 5 rows

Tail-c 10/etc/inittab Show last 10 bytes


5, more paging display file command

Command format: More [options] < file name >

If you use a pipe to receive information, the file name is not followed by more.

Common options:

-N, used to create a display window that can display n rows.

-D, which displays the action prompt: "Press space to continue, ' Q ' Quit ', in place of the more default prompt.

-S to compress contiguous blank lines into one line.

More Action commands:

Enter down n rows, need to be defined, default to 1 rows;

Ctrl f (or SPACEBAR) scrolls down one screen;

Ctrl-B returns to the previous screen;

= Output the line number of the current line;

V Call VI Editor;

Q Exit More

Example:

[[email protected] ~]# More-dc/etc/profile Note: Display the prompt and display from the terminal or the top of the console;

[[email protected] ~]# more-4/etc/profile Note: 4 rows per screen;

[Email protected] ~]# More/mail/etc/profile Note: Starting from the first two lines in profile;

Examples of other commands used in combination with piping and more;

For example, we list a directory of files, because the content is too much, we should learn to use more to page display. This has to do with plumbing | Together, for example:

[Email protected] ~]# ls-l/etc |more

6, less paging display file command
The less tool is also a tool for paging through files or other output, as much as more, but more powerful, and should be said to be a tool for Linux to view the contents of a file, which is extremely powerful; because less content too much, the most commonly used to introduce;

The syntax format of less;

Less [parameter] file

If you use a pipe to receive information, the file name is not followed by less.

-I ignores case when searching, unless the search string contains uppercase letters;

-I ignores case when searching, unless the search string contains lowercase letters;

-m displays the percentage of files read;

-m explicit read the file percentage, line number and total number of rows;

-N Enter the travel number in front of each line;

-P Pattern Search pattern, for example, in/etc/profile search word MAIL, use less-p mail/etc/profile

-S displays consecutive blank lines as a blank line;

For example: Display/etc/profile content, let it display line number;

[Email protected] ~]# less-n/etc/profile

Less's action command;

The ENTER key moves down one line;

Y move up one line;

The space bar scrolls down one screen;

b scroll up one screen;

D scroll down half screen;

H less's help;

U scroll up half screen;

W can specify which line to start displaying, which is displayed from the next line of the specified number, for example, 6, which is displayed from line 7th;

G jumps to the first line;

G jumps to the last line;

P n% jumps to n%, such as 10%, which means that it starts at 10% of the entire file content;

/pattern searches for pattern, such as/mail, to search for mail words in a file;

V Call VI Editor;

Q Exit Less

Third, edit the file command

Vi

1. order to enter VI

VI FileName: Opens or creates a new file and places the cursor at the beginning of the first

VI +n FileName: Opens the file and places the cursor at the beginning of nth

VI + FileName: Opens the file and places the cursor at the beginning of the last line

VI +/pattern FileName: Opens the file and places the cursor at the first string that matches the pattern

Vi-r FileName: The last time you were editing with VI, a system crash occurred, restoring filename

VI filename....filename: Open multiple files, edit them sequentially

2. Move Cursor Class command

H: Move the cursor one character to the left

L: Move the cursor right one character

Space: Move the cursor right one character

Backspace: Cursor moves left one character

K or ctrl+p: Move the cursor up one line

J or CTRL + N: Move the cursor down one line

Enter: Move the cursor down one line

W or W: Move the cursor right one word to the beginning of the word

B or B: The cursor moves left one word to the beginning of the word

E or E: Move the cursor right one word to the end of the word

): Move the cursor to the end of the sentence

(: The cursor moves to the beginning of the sentence

}: Move the cursor to the beginning of the paragraph

{: Cursor moves to end of paragraph

NG: Cursor moves to the beginning of nth

n+: The cursor moves down n rows

N: Move the cursor up n rows

n$: Cursor moves to end of Nth line

H: Move the cursor to the top row of the screen

M: Move the cursor to the middle line of the screen

L: The cursor moves to the last line of the screen

0: (note is the number 0) cursor moves to the beginning of the current line

$: Cursor moves to the end of the current line

3, Screen tumbling class command

Ctrl+u: First half screen to file

Ctrl+d: Half-screen to the end of the file

CTRL+F: Flip a screen to the end of a file

ctrl+b; Turn one screen to the top of the file

NZ: Rolls line N to the top of the screen and scrolls the current line to the top of the screen when n is not specified.

4. Inserting text class commands

I: Before the cursor

I: At the beginning of the current

A: After the cursor

A: At the end of the current line

O: A new line below the current line

O: New row above the current line

R: Replace the current character

R: Replaces the current character and its characters until the ESC key is pressed

S: replaces the specified number of characters with the input text starting at the current cursor position

S: Deletes the specified number of rows and replaces them with the input text

NCW or NCW: Modifies a specified number of words

NCC: Modifying a specified number of rows

5. Delete command

NDW or NDW: Delete the n-1 characters at the beginning and after the cursor

Do: Delete to the beginning of the line

d$: Delete to end of line

NDD: Deletes the current line and its subsequent n-1 rows

X or x: Deletes a character, x deletes the cursor, and x deletes the cursor before the

Ctrl+u: Delete text entered under input mode

6. Search and Replace commands

/pattern: Searches for pattern at the end of the file from the beginning of the cursor

? pattern: Searches for pattern from the beginning of the cursor to the top of the file

N: Repeat the last search command in the same direction

N: Repeats the last search command in the opposite direction

: s/p1/p2/g: Replaces all P1 in the current row with P2

: n1,n2s/p1/p2/g: All P1 in line N1 to N2 are replaced with P2

: g/p1/s//p2/g: Replace all P1 in the file with P2

7, vi command Use example

Example one: Create a file a.txt.

VI a.txt

Hello everyone!

The Exit method is: Press ESC when the edit is complete, and then enter: Q is exit, and: Wq is saved after exiting, plus exclamation mark is to indicate mandatory

Linux file creation, viewing, editing 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.