Linux File Management II

Posted by mmonaco on Mon, 10 Jan 2022 15:29:11 +0100

Linux File Management II

1, View file contents

1. cat command

Function: output the contents of files in positive order

Basic syntax:

# cat file name

Case: viewing the / tmp/passwd file

# cat /tmp/passwd

2. head command

Function: view the first n lines of a file. If n is not specified, the first 10 lines will be displayed by default

Basic syntax:

# head -n file name

Case: view the first ten lines of / tmp/passwd

# head /etmp/passwd

3. tail command

Function: view the last n lines of a file. If n is not specified, the last 10 lines will be displayed by default

Basic syntax:

# tail -n file name

Case: view the last ten lines of / tmp/passwd

tail /tmp/passwd

tail -f special usage: dynamically view the output information of a file content. It is often used to view logs

Case: query log information of / var/log/messages file

# tail -f /var/log/messages
 To exit, press the shortcut key directly: Ctrl + C

4. less command

Function: split screen display of file content

Basic syntax:

# less file name

Note: the less command does not load the entire file into memory, but loads it bit by bit. Relatively speaking, it is more efficient when reading large files.

The execution of the less command will open an interactive interface. Here are some common interactive commands (similar to more):

Keyfunction
enter keyMove down one line.
dMove half a page down.
Space barMove down one page.
bMove up one page.
Up and down direction keysMove up and down, function keys unique to the less command.
less -N file nameDisplays the line number.
/StringSearches for the specified string.
qExit less

2, File statistics command

1. wc command

Function: count the number of lines, words and bytes of text.

Basic syntax:

# wc [options] file name
 Option Description:
-l: express lines,Number of lines (by carriage return)/Line breaks are standard)
-w: express words,The number of words is determined by the number of spaces
-c: express bytes,Bytes (space, carriage return, line feed)

Case: count the total number of rows in / tmp/passwd file

# wc -l /tmp/passwd

2. du command

Function: view the disk space occupied by files or directories

Basic syntax:

# du [options] files or folders for statistics
 Option Description:
-s : summaries,Only the size of the summary is displayed, and the size of the folder is counted
-h : Displays the size of a file or folder with high readability( KB/MB/GB/TB)

Case: display the size of / var/log/messages file (disk space occupied, display the unit of file size)

#  du -h messages
8.0K    messages

Case: count the size of / var/log folder

# du -sh /var/log
16M     /var/log

3, File processing command

1. find command

Function: file search.

Basic syntax:

# find search path [options]
Option Description:
-name: Specify the name of the file to search, support*Asterisk wildcard
-type: Represents the type of file searched, f For ordinary documents, d Represents folders, etc

Case: search for boot. In / var directory Log file

# find /var -name "boot.log" -type f

Case: full search ssh directory

# find / -name "ssh" -type d 

Case: search all file information ending with ". Log" in the / var/log directory

# find /var/log -name "*.log" -type f

2. grep command

Function: directly find the lines containing the specified keywords in the file and highlight these information.

Basic syntax:

# grep [options] the keyword to search for and the file name to search for
 Option Description:
-n : Represents the display of line number information containing keywords

Case: search for a line containing the keyword "network" in the / var/log/messages file

# grep network /var/log/messages

Case: search the / var/log/messages file for a line containing the keyword "network", and then display the line number information

# grep -n network /var/log/messages

Case: search all files in the / var/log directory and find all lines containing the keyword "network"

# grep network /var/log/*

3. echo command

Function: input the specified text content in the terminal

Basic syntax:

# echo "text content"

Case: in the terminal, output the hello world string

# echo "hello world"

4. Output redirection

Scenario: the output of general commands will be displayed in the terminal. Sometimes the execution results of some commands need to be saved to a file for subsequent analysis / statistics, so the output redirection technology needs to be used at this time.

>: standard output redirection: overwriting the output will overwrite the original file content

>>: append redirection: append output. The original file content will not be overwritten. It will continue to be added at the end of the original content

Case: write "hello world" output from echo to readme Txt file

# cat readme.txt
111
222
333
# echo "hello world" > readme.txt
# cat readme.txt
hello world   #readme. The original contents of TXT file have been overwritten

Case: write "hello linux" output from echo to readme Txt. It is required that the original content cannot be overwritten

# echo "hello linux" >> readme.txt
# cat readme.txt
hello world
hello linux  #readme. The original contents of TXT file have not been overwritten and are added on the original basis

Topics: Linux Operation & Maintenance