Linux | Redirection | File concepts | View files | View time | Find files | zip

Linux | Redirection | File concepts | View files | View time | Find files | zip

Continuing from the previous chapter, we will continue to talk about instructions in this chapter. Without further ado, let’s get started~~

1. more

Syntax: more [Option][File]
Function: more command, similar to cat< /span>
Common options:

-n Number all lines of output
q ​​Exit more

  • The command we are going to learn next is more, but if this more wants to view a file, we have to create a file. The file we create this time is different from before. Let's learn a new thing called重定向

1.1 Exit weight direction>sum>>

echo "hello Linux" > log.txt
  • The echo here is to print things to the screen.

Insert image description here

  • Then we add a symbol at the end> log.txt
  • After executing this command, you will find that nothing is output.
  • Look at the current path and there is one more file name after >
  • Then wecat print this file, and you will find that what was supposed to be printed on the screen was written to the file~~

Insert image description here

Then can we remove the previous oneecho?

  • Let's try again
>hello.txt

Insert image description here

  • As you can see, the file can also be created in this way, but it is an empty file.

Linux has a design concept, which is that some files arefiles

For any file, read and write files
Display device->Display file->Print to display, write to display file


  • For the above> it is output redirection. It should have been written to the monitor file, but after passing> it was output to a normal file< /span>

  • Then if we input the string into the file again, should we append the content or delete the original content and then add the file?

let's try it

Insert image description here

  • As you can see, every time the string is redirected, the content inside will be modified. The original content will be deleted first, and then a new string will be written.

  • The last operation was to input an empty character into the file, but there was still a newline in the file and it still had a size.

  • We once again use> to create a file, but this file has no size~~

Insert image description here

So we conclude that > output redirection starts writing the file from 0. In other words, it deletes the original string and then writes a new string.

  • So what should we do if we don’t want to delete the original file and continue to write data?
  • Watch next
echo "hello linux" >> log.txt
  • Here>> isappend redirection, which means that the original file will not be deleted and the original file will be continued. Add data later

Insert image description here

The one> symbol here is equivalent to the file operation in C language written in the w way, and the file will be cleared every time it is written a>
A >> symbol here is equivalent to the file operation in C language written in the a way, and it will not be cleared every time it is written. File, write data directly to the back

For example, the cat command here is written to the monitor file by default~~

Insert image description here

  • We can also just enter a cat command
cat
  • Here characters are read from the keyboard and then output to the screen
  • Pressctrl + zto exit

Insert image description here

1.2 Input redirection<

cat < log.txt
  • Here< means input redirection

  • The cat command is supposed to read data from the keyboard, but this input redirects reading data from text to the display.

Insert image description here

To summarize:

command > filename #输出重定向
command >> filename #追加重定向
command < filename #输入重定向

2. Again, everything is a document

  • Our terminal actually has a file in the Linux system. What we say is unfounded. Let’s prove it.
ll /dev/pts/
  • There are only these few files here

Insert image description here

  • Let's open another window
  • You can see one more file

Insert image description here

  • At this time we can use commands to enter the content into the file we just viewed.
echo "hello linux" > /dev/pts/1
  • Then we can see that after executing the command, the output can be directly output there.

Insert image description here


3. less command [Important]

  • The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful.
  • The usage of less is more flexible than that of more. When it comes to more, we have no way to turn forward, we can only look back.
  • But if you use less, you can use the [pageup][pagedown] and other key functions to browse the file forward and backward, which is easier to view the contents of a file!
  • In addition, you can have more search functions in less, not only you can search down, but you can also search up.

Language:
less [number] Sentence

Function:
Less is similar to more, but you can use less to browse files at will, while more can only move forward, but not backward, and less will not move before viewing. Load the entire file.
Options:

-i Ignore case when searching
-N Display the line number of each line
/String: Search down for "string" Function
?String: Function to search upward for "string"
n: Repeat the previous search (related to / or ?)< a i=5> N: Repeat the previous search in reverse (related to / or ?) q:quit

  • Next, we will create a large file to demonstrate
i=1;while [ $i -le 10000 ]; do echo "hello Linux $i"; let i++;done > big.txt

Insert image description here

  • Let’s go back to the first command above:more
more big.txt
  • Enter is to scroll down line by line and read line by line.
  • Press q to exit

Insert image description here

  • We recommend the next command,less

The usage is basically the same asmore, but this is more convenient. Press the arrow keys on the keyboard to view line by line, and use spaces to view page by page

4. head command

Head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. Head is used to display the beginning of the file to the standard output, while tail is used to read the file. end.

Syntax: head [parameter]… [file]…
Function:
head is used to display files to standard output, the default head command prints the first 10 lines of its corresponding file.
Options:

  • -n<Number of lines> Number of lines to display
head big.txt

Insert image description here

  • If we want to view the first few lines of the file content, how can we view it?
  • For example, view the first 20 rows
head -20 big.txt

Insert image description here

5. tail command

The tail command writes the file to the standard output starting from the specified point. Use the -f option of the tail command to conveniently check the changing log file. tail -f filename will display the last content of filename on the screen, and not only refresh , allowing you to see the latest file content.

Syntax: tail[required parameters][select parameters][file]
Function: used Displays the content at the end of the specified file. If no file is specified, it will be processed as input information. Commonly used to view log files.
Options:

-f loop reading
-n<number of lines> display number of lines

tail big.txt
  • View the last 10 lines by default

Insert image description here

  • Specify the last number of rows to view
tail -20 big.txt

Insert image description here

5.5 Pipes

  • Then I want to view the middle row, what should I do?
  • Here we need to introduce a concept管道
head -8010 big.txt | tail -11

Insert image description here

  • Here| means pipeline, passing the output command of the previous command to the second command

5.6 wc -l [Supplementary command]

  • Count how many lines there are in the file
wc -l big.txt

Insert image description here

  • Then we have a new way to play
  • How many rows of statistics can we output through the pipeline?
head -8010 big.txt | tail -11 | wc -l

Insert image description here

  • We also want to invert the output
  • We can also output through pipes
head -8010 big.txt | tail -11 | tac

Insert image description here

We can assemble instructions through pipelines

6. Time-related instructions

date display
date Specifies the format to display time: date +%Y:%m:%d
date usage: date [OPTION]… [ +FORMAT]

  1. In terms of display, users can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of commonly used marks is as follows:

%H: Hours (00…23)
%M: Minutes (00…59)
%S: Seconds (00…61)
%X: Equivalent to %H:%M:%S
%d: Day (01…31)
% m: month (01…12)
%Y: complete year (0000…9999)
%F: equivalent to %Y-%m-%d< /span>

  1. In terms of setting time

date -s //Set the current time. Only root permissions can set it, others can only view it.
date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00
date -s 01:01:01 / /Set the specific time without changing the date
date -s “01:01:01 2008-05-23″ //This way you can set the entire time
date -s “01:01:01 20080523″ //This can set the entire time
date -s “2008-05-23 01:01:01″ //This can set the entire time
date -s “20080523 01:01:01″ //This way you can set the entire time

  1. Timestamp

Time->Timestamp: date +%s
Timestamp->Time: date -d@1508749502
Unix timestamp ( Unix epoch, Unix time, POSIX time or Unix timestamp in English) is the number of seconds that have elapsed since January 1, 1970 (UTC/GMT
midnight), regardless of leap seconds.

  • If we want to check the time, we can execute the following command:
date

Insert image description here

  • This display is not very convenient for viewing. We can customize the viewing format.
  • Check it out in formatted form
  • hereY must be in uppercase, and时分秒
date +%Y-%m-%d_%H:%M:%S

Insert image description here

6.1 Timestamp

we can use

date +%s

to view the timestamp

  • It is said that the timestamp will not be enough by 2038. You can check Baidu for details. This is very important for computers~~

Insert image description here

7. Cal instruction

The cal command can be used to display the Gregorian (Solar) calendar. The Gregorian calendar is the calendar currently used internationally, also known as the Gregorian calendar and commonly known as the Gregorian calendar. The "Gregorian calendar", also known as the "solar calendar", is based on the earth's orbit around the sun as one year. It is common in Western countries, so it is also called the "Western calendar".

Command format: cal [parameter][month][year]
Function: For viewing Calendar and other time information, if there is only one parameter, it represents the year (1-9999), if there are two parameters, it represents the month and year
Common options:

  • -3 Display the system’s calendar of the previous month, current month, and next month
  • -j displays the day of the year (the date of the year is calculated in days, starting from January 1st, and the number of days of the current month in the year is displayed by default)
  • -y displays the calendar for the current year
  • This command is very simple, you can directly view the current date
cal

Insert image description here

  • We can also display the system's monthly calendar for the previous month, current month, and next month
cal -3

Insert image description here


  • This is the number of days in the year displayed (dates in a year are calculated in days, starting from January 1st, and the number of days in the current month in the year is displayed by default)
cal -j

Insert image description here


  • Display calendar for current year
cal -y

Insert image description here

8. Find command: [very important]-name

  • The find command under Linux searches for files in the directory structure and performs the specified operation.
  • The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it also has many options, most of which are worth our time to understand.
  • Even if the system contains a Network File System (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.
  • When running a very resource-consuming find command, many people tend to execute it in the background because traversing a large file system may take a long time (here refers to a file system of more than 30G bytes

Syntax: find pathname -options
Function: Used to find files in the file tree and make Corresponding processing (may access disk)
Common options:

  • -name searches for files by file name.
find 路径 -name 名称

Insert image description here

  • Or we can use wildcards*
find / -name *.c
  • is to query all files ending with .c

Insert image description here

9. grep command

Syntax: grep [options] Search string files
Function: Search strings in files , print out the found lines
Common options:

  • -i: Ignore the difference in case, so the cases are treated as the same
  • -n: Output the line number by the way
  • -v: reverse selection, that is, display the line without the ‘search string’ content
  • Let’s look at the simplest one first, which is to search directly by matching keywords
  • 999 is the keyword to be found, followed by the file name
grap "999" big.txt

Insert image description here

  • We created a file with upper and lower case and then demonstrated

Insert image description here

Let’s look at the first option

  • -i: Ignore the difference in case, so the cases are treated as the same
  • Both uppercase and lowercase can be printed

Insert image description here

  • If not added-i will only match keywords
grep "bit" tmp.txt

Insert image description here

Let’s look at the second option

  • -n: Output the line number by the way
grep -i -n "bit" tmp.txt

Insert image description here

Let’s look at the third option

  • -v: reverse selection, that is, display the line without the ‘search string’ content

  • Don’t print the ones you just had, and match them in reverse.

Insert image description here

  • The following ones- can be written continuously
grep -inv "bit" tmp.txt

Insert image description here

  • We can also pipe commands for reverse output
grep -inv "bit" tmp.txt | tac

Insert image description here

  • can also be combined withcat and count rowswc -lcommands
cat tmp.txt | grep -inv "bit" tmp.txt | tac | wc -l

Insert image description here


Next let’s talk about packaging and compression

10. zip/unzip command:

Syntax: zip compressed file.zip directory or file
Function: Compress the directory or file into zip format
Common options:

  • -r recursively processes all files and subdirectories in the specified directory.
  • Let’s first see if there are these two commands
which zip
which unzip

Insert image description here

  • If it looks like the picture above, you need to install it first.
yum install -y zip unzip
  • Prompt complete means the installation is successful

Insert image description here

  • We create these files to demonstrate
touch file1 file2 file3 file4 file5

Insert image description here

Insert image description here

  • We want to package the entire directory and the contents want to package
  • -r recursively processes all files and subdirectories in the specified directory.
  • If you do not add -r, it will only package a directory, and the files in the directory will not be printed.
zip -r 111.zip 111

Insert image description here

  • Unzip the zip package
unzip 111.zip

Insert image description here

  • You can see that the file you just packaged has been decompressed.

Insert image description here

This article focuses on redirection | file concept | viewing files | viewing time | finding files | basic decompression
If it is helpful, I hope you can connect it with me after reading it~ ~Thank you everyone! ! !

Guess you like

Origin blog.csdn.net/2201_76004325/article/details/134589918