[Linux Basics] Basic Linux commands (2) head, tail, Cal, find, grep, zip/unzip, tar, bc, uname

Insert image description here


This article continues the previous article on basic instructions. Click here to jump to an article.

1. 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 the beginning of the file to the standard output. The default head command prints the first 10 lines of the corresponding file.

Options:
-n<number of lines> Number of lines to display

[root@alicloud ~]# head big.txt 
hello linux, hello 0
hello linux, hello 1
hello linux, hello 2
hello linux, hello 3
hello linux, hello 4
hello linux, hello 5
hello linux, hello 6
hello linux, hello 7
hello linux, hello 8
hello linux, hello 9
[root@alicloud ~]# head -5 big.txt 
hello linux, hello 0
hello linux, hello 1
hello linux, hello 2
hello linux, hello 3
hello linux, hello 4

Insert image description here

2. 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 , so that you can see the latest file content.

Syntax: tail [required parameters] [selected parameters] [file]
Function: used to display the content at the end of the specified file. When no file is specified, it is processed as input information. Commonly used to view log files.
Options:
-f loop reading
-n<number of lines> display number of lines

When operating on a file, the last ten lines of the text file are printed by default.

[root@alicloud ~]# tail big.txt 
hello linux, hello 991
hello linux, hello 992
hello linux, hello 993
hello linux, hello 994
hello linux, hello 995
hello linux, hello 996
hello linux, hello 997
hello linux, hello 998
hello linux, hello 999
hello linux, hello 1000
[root@alicloud ~]# tail -5 big.txt 
hello linux, hello 996
hello linux, hello 997
hello linux, hello 998
hello linux, hello 999
hello linux, hello 1000

Insert image description here

Extension: How to get the content of the middle row

For example: How to print 500~510 lines of content on the screen?

Option One:

We use head to get the first 510 lines, redirect to a temporary file, and then use tail to get the last 10 lines. This solves the problem. Let's try it.

[root@alicloud ~]# head -510 big.txt > tmp.txt
[root@alicloud ~]# tail -10 tmp.txt 
hello linux, hello 500
hello linux, hello 501
hello linux, hello 502
hello linux, hello 503
hello linux, hello 504
hello linux, hello 505
hello linux, hello 506
hello linux, hello 507
hello linux, hello 508
hello linux, hello 509

Insert image description here

There is a drawback here. In this case, a file will be created. Although the problem is solved, it is a bit bloated. Let's learn how to print directly without creating a temporary file.

Option II:

We can achieve this by using head and tail together and adding the pipe symbol |.

[root@alicloud ~]# head -510 big.txt | tail -10
hello linux, hello 500
hello linux, hello 501
hello linux, hello 502
hello linux, hello 503
hello linux, hello 504
hello linux, hello 505
hello linux, hello 506
hello linux, hello 507
hello linux, hello 508
hello linux, hello 509

Insert image description here

Let’s briefly explain the pipe symbol here:
Insert image description here

third solution:

Seeing this, we can also use cat, head, and tail together to solve this problem.

[root@alicloud ~]# clear
[root@alicloud ~]# cat big.txt | head -510 | tail -10
hello linux, hello 500
hello linux, hello 501
hello linux, hello 502
hello linux, hello 503
hello linux, hello 504
hello linux, hello 505
hello linux, hello 506
hello linux, hello 507
hello linux, hello 508
hello linux, hello 509

Insert image description here

Additional instructions:

(1) wc -l file name

Function: Count the number of file lines.
Insert image description here

(2) uniq file name

Function: Remove duplicates from adjacent rows.

[root@alicloud ~]# cat file.txt 
111111
111111



222222
222222


333333




444444
[root@alicloud ~]# uniq file.txt 
111111

222222

333333

444444

Insert image description here

(3) sort file name

Function: Sort files.

[root@alicloud ~]# sort file.txt 









111111
111111
222222
222222
333333
444444
[root@alicloud ~]# sort file.txt | uniq

111111
222222
333333
444444

Insert image description here
This is the sorted file content, and we can also use it with deduplication after sorting.

3. Time-related instructions

date displays
date and specifies the format to display time: date +%Y:%m:%d
date usage: date [OPTION]… [+FORMAT]
1. In terms of display, the user can set the format to be displayed. The format is set to A plus sign is 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
2. In the setting In terms of fixed time,
date -s //Set the current time. Only root permissions can set it, and 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, and the date will not be changed
date -s “01:01 :01 2008-05-23″ //This way you can set the entire time
date -s “01:01:01 20080523″//This way you can set the entire time
date -s “2008-05-23 01:01:01″ // This way you can set the entire time
date -s “20080523 01:01:01″ //This way you can set the entire time
3. Timestamp
Time -> Timestamp: date +%s
Timestamp -> Time: date -d@1508749502
Unix timestamp (Unix epoch, Unix time, POSIX time or Unix timestamp in English) is from January 1, 1970 (UTC/ The number of seconds since midnight GMT, not counting leap seconds.

When we want to get the time of the day, we can execute the following command:

[root@alicloud ~]# date +%Y-%m-%d
2023-09-18
[root@alicloud ~]# date +%Y-%m-%d_%H:%M:%S
2023-09-18_19:48:31

Insert image description here

Get timestamp:

[root@alicloud ~]# date +%s
1695037872

Insert image description here

It is a cumulative number of seconds, from 0:00:00 on January 1, 1970, to a cumulative number of seconds at this moment. It grows linearly. Each moment is unique and can be used to mark something.
Convert timestamp to year, month, day, hour, minute and second:

[root@alicloud ~]# date -d@1695037872
Mon Sep 18 19:51:12 CST 2023
[root@alicloud ~]# date +%Y-%m-%d_%H:%M:%S -d@1695037872
2023-09-18_19:51:12

Insert image description here

4. Cal command

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: used to view calendar and other time information. If there is only one parameter, it means the year (1-9999). If there are two parameters, it means the month and year.
Common options :
-3 Display the system's calendar of the previous month, current month, and next month
-j Display the day of the year (the date in a year is calculated by day, starting from January 1st, and the default display of the current month in the year number of days in)
-y displays the calendar for the current year

The use of cal is very simple, let’s take a look
Insert image description here
Insert image description here
Insert image description here

5. 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 perform corresponding processing (may access disk)
Common options:
-name Find files according to file names

In a certain directory, search by name. Let's take the following example.

[root@alicloud ~]# find /root/ -name file.txt
/root/file.txt

Insert image description here

Supplement: which, ctrl+r

You can check where the system commands are stored and display them according to the absolute path.

[root@alicloud ~]# which ls
alias ls='ls --color=auto'
	/bin/ls
[root@alicloud ~]# which ll
alias ll='ls -l --color=auto'
	/bin/ls
[root@alicloud ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
	/bin/alias
	/usr/bin/which

Insert image description here
ctrl+r, you can search for instructions based on previous instruction fragments.
Insert image description here

6. grep command

Syntax: grep [options] Search string files
Function: Search for strings in the file and print out the found lines
Common options:
-i: Ignore the difference in case, so uppercase and lowercase 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

grep is a line filtering tool command.

[root@alicloud ~]# grep '99' big.txt

Insert image description here

[root@alicloud ~]# grep 'main' -n mytest.c 

Insert image description here

You can also search in two files together

[root@alicloud ~]# grep -n 'main' mytest.c newfile.txt

Insert image description here

It can also use the result of the instruction output as the input source of the pipeline to filter in units of rows
. For example:

[root@alicloud ~]# ps ajx | grep sshd

Insert image description here

For the -i and -v options, -i ignores case and searches, and -v filters out the lines containing the target and does not print them. The two options can be used together. Let’s take a look:

[root@alicloud ~]# grep -vi 'lcx' newfile.txt 

Insert image description here

7. zip/unzip command

Syntax: zip compressed file.zip Directory or file
function: compress a directory or file into zip format.
Common options:
-r recursive processing, processing all files and subdirectories in the specified directory together.

Here we package and compress all the files in the directory, and then unpack and decompress them to the specified directory.

打包压缩:zip -r name.zip dir/file //打包压缩一个目录下的所有文件/一个文件
解包解压缩:unzip name.zip -d dir//解包解压缩到指定目录dir

(1) First package everything in the current directory

[root@alicloud lesson2]# zip test.zip ./*

Insert image description here

(2) Move the packaged and compressed files to the upper-level directory

[root@alicloud lesson2]# mv test.zip ../

Insert image description here

(3) Unzip test.zip

[root@alicloud ~]# unzip test.zip 

Insert image description here
Let’s see if there is any content in dir1:
Insert image description here
it shows that there are no files or directories. This is because we did not add the -r option when compressing. Packaging and compression do not have recursive packaging and compression, so the content in dir1 has not been packaged. For compression, let's try adding the -r option.
Insert image description here
This time there are files in our decompressed dir1 directory, so when packaging and compressing everything in a directory, there are subdirectories in this directory. We must add the -r option when packaging, otherwise the files in the subdirectory If it is not packaged and compressed, it is incomplete.
In the decompression operation above, we first move the compressed file to the upper-level directory and then decompress it. This is too troublesome. We can specify the directory when decompressing. Let's take a look: Currently we are in
lesson There is a test.zip compressed file in the directory. We want to unpack and decompress it to the upper-level directory. You can do this

[root@alicloud lesson2]# unzip test.zip -d ../

Insert image description here
When unpacking and decompressing to the specified directory, add the -d option.

8. tar command (important): pack/unpack, do not open it, read the content directly

tar [-cxtzjvf] Files and directories... Parameters:
-c: Parameter command to create a compressed file (meaning create);
-x: Parameter command to unzip a compressed file!
-t: View the files in tarfile!
-z: Does it also have the attribute of gzip? That is, do I need to use gzip compression?
-j: Does it also have the attributes of bzip2? That is, do I need to use bzip2 compression?
-v: Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
-f: Use the file name. Please note that the file name must be followed immediately after f! Don't add any more parameters!
-C: Extract to the specified directory

The most commonly used is packaging, compression, unpacking and decompression. Here we list two instructions:

tar -czf test.tar.gz 目录/文件 //打包或压缩一个目录/文件,以.tar.gz为后缀的压缩文件
    						  //也可以写作.ta.gz
tar -xzf test.tar.gz -C 目录/文件 //这里解压到指定目录的时候需要加上-C选项,默认为当前目录

Next we use the packaging, compression and unpacking and decompression instructions:
the suffix of the compressed file here can be .tar.gz or .tgz, both forms are acceptable. We will use tgz here.
Pack and compress all of the current directory:

[root@alicloud lesson2]# tar -zcf test.tgz ./*

Insert image description here
Unpack and extract to the specified directory:

[root@alicloud lesson2]# tar -xzf test.tgz -C ../

Insert image description here
For other options, just look at the ones listed above.

9. bc command

The bc command can easily perform floating point operations. When bc is entered, a simple calculator in Linux is entered.
Insert image description here
It's very simple, so I won't go into details.

10. uname command

Syntax: uname [option]
Function: uname is used to obtain information about the computer and operating system.
Additional notes: uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.
Common options:
-a or –all output all information in detail, in order: kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name

uname -r/-a can view the computer's architecture under the Linux operating system.
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/Ljy_cx_21_4_3/article/details/133079351
Recommended