Basic operations of directories and files in Linux

1. Linux directory structure

Linux is a tree-shaped directory. The
root directory
-the starting point of all partitions, directories, files, etc.
-In the entire tree-shaped directory structure, a separate "/" is used to indicate
common subdirectories-
/root ;/bin; /boot ;/dev ; /etc-
/home; /var; /usr; /sbin
structure as shown below
Insert picture description here

Linux directory effect
/root Administrator's host (home) directory
/home/xxx Home directory of ordinary users
/bin Command file directory, storing all user executable commands
/sbin Administrator operation directory, storing management commands that can only be executed by the administrator
/boot System kernel, startup file directory
/dev (device) Store device files (CD-ROM, hard disk, etc.)
/etc Store configuration files for system programs and most applications (rpm/yum installation)
/where (Variable) Store files that can be changed, including various log files
/lib (library) The dynamic link shared library file that stores the system program (similar to the DLL file in windows)
/usr Store system user tools and programs
/media Removable media attachment point, such as U disk, CD-ROM, etc.
/proc File to store the information of the mapping system
/ mnt Directory for temporarily mounting storage devices
/opt (optional) The directory where the third-party application is installed
/tmp Temporary files stored in the system

2. View the contents of the file

2.1:cat

Function: directly display the content of the entire file

格式:cat [选项] 文件名
Options effect
-n Number all output lines
-b Do not number blank lines
-s Replace all consecutive blank lines with one blank line

Insert picture description here
Number all output lines No number
Insert picture description here
for blank lines
Insert picture description here
Replace all consecutive blank lines with one blank line
Insert picture description here

2.2 more

■Full-screen display of file content in pages

格式:more [选项] 文件名

■Interactive operation mode

Operation method effect
Enter Scroll down line by line
space bar Scroll down one screen
b Flip up one screen
q drop out

◆When turning to the last page, it will automatically exit

◆When used in conjunction with pipeline operations (for example: ls -R /etc | more), page up cannot be paged

2.3 less

■Same as the more command, but with more extended functions

格式:less [选项] 文件名
Operation method effect
Page Up Page up
Page Down Page down
/ Find content
n Next content
N Previous content
Scroll up
Scroll down
Enter Scroll down line by line
space bar Scroll down one screen
b Flip up one screen
q drop out

Contrast the difference between more

◆It will not automatically exit after
turning down to the last page. ◆It can be used in combination with pipe operation to turn pages up.

2.4 head、tail

■head command

Purpose: View part of the content at the beginning of the file (default is 10 lines)

格式:head -n 文件名         (n是行数)

■tail
function: view a small part of the content at the end of the file (the default is 10 lines)

格式:tail -n 文件名
     tail -f 文件名             (跟踪文件尾部内容的动态更新)

View the contents of the 5 lines at the beginning of the file, use cat -n to facilitate viewing
Insert picture description here

3. The content of the statistical file----wc

Function: Count the number of words in the file (word count) and other information

格式:wc [选项] 目标文件

Options effect
-l Count the number of rows
-w Count the number of words
-c Count the number of bytes

If you enter the wc command without any options, the default -lwc three options
are used at the same time
. The first number 14 of the statistics of the bb.txt file here is the number of lines; the second number 15 is the number of words; the third number 36 Is the number of bytes; the fourth is the name of the file
Insert picture description here

4. Retrieve and filter file content: grep

■Search and display the line including the specified character string in the file

格式:grep [选项] 查找条件 目标文件
Command options Description
-i Not case sensitive when searching
-v Display all lines that do not contain matching files (reverse query, reverse matching)
-c Only output the total number of matched rows (not the number of matched rows)
-n Show matching line and line number
-e Realize the matching of multiple search conditions, logical or relationship
-E Support the use of extended regular expressions, which is equivalent to using the egrep command
-The Exact match, which means "match only"

查找条件设置
–要查找的字符串以双引号括起来
–“^……”表示以……开头,
“……$”表示以……结尾
“^$”表示空行

示例:grep -ie "^NAME" -e "7$" CentOS-Vault.repo

5.压缩命令:gzip、bzip2

■制作压缩文件、解开压缩文件
压缩格式:

gzip [-9] 文件名

bzip2 [-9] 文件名 (这里的-9是压缩比例,9的压缩比例最高,耗时比前面的长)
这样压缩的文件都带有默认的扩展名分别为“.gz”和“.bz2”,且原始文件不再保留
解压缩格式:
gzip -d .gz格式的压缩文件 等同于 gunzip 文件名.gz
bzip2 -d .bz2格式的压缩文件 等同于 bunzip2 文件名.bz2
Insert picture description here

6.归档命令:tar

制作归档文件、释放归档文件

  tar   [选项]...  归档文件名  源文件或目录

        tar   [选项]...  归档文件名  [-c目标目录]
选项 作用
-c 创建.tar格式的包文件
-x 解开.tar格式的包文件
-C 解压时指定释放的自标文件夹
-f 表示使用归档文件
-p 打包时保留文件及目录的权限
-P 打包时保留文件及目录的绝对路径
-t 列表查看包内的文件
-v 输出详细信息、(Verbose )
-j 调用bzip2程序进行压缩或解压
-z 调用gzip 程序进行压缩或解压

Insert picture description here
Insert picture description here

7.文件编辑器:vi

■文件编辑器的作用

  • 创建或修改文本文件

  • 维护Linux系统中的各种配置文件
    ■linux中最常用的文本编辑器

  • vi:类似UNIX操作系统的默认文本编辑器

  • vim:vim是vi文本编辑器的增强版
    ■三种工作模式:命令模式、输入模式、末行模式
    ■不同模式之间切换
    ■命令模式切换至输入模式

按键 作用
a 在当前光标位置之后插入内容
i 在当前光标位置之前插入内容
o 在光标所在行之下插入一个新行内容
O 在光标所在行之上插入一个新行内容
A 在所在行的行尾插入内容
I 在所在行的行首插入内容

命令模式

操作类型 操作键 功能
翻页移动 Page Down健或Ctrl+F 向下翻动一整页内容
Page Up键或Ctrl+B 向上翻动一整页内容
行内快速跳转 Home键或^键、数字0键 跳转到本行的行首
End键或$键 跳转到本行的行尾
行间快速跳转 1G或gg 转到文件内容的第1行
G 跳转到文件的最后一行
#G 跳转到文件中的第#行(其中"#”号用具体数字替换)
M 跳转至当前页的中间位置
显示行号 : set nu 在编辑器中显示行号
: set nonu 取消行号显示
删除 x或Delete键 删除光标处的单个字符
dd 删除当前光标所在行(有剪切功能)
#dd 删除从光标处开始的#行内容
d^ 删除当前光标之前到行首的所有字符
d$ 删除当前光标处到行尾的所有字符
dw 删除光标处的整个单词
替换字符 R或Shift+r 替换当前光标处字符
复制 yy 复制当前行整行的内容到剪贴板
#yy 复制从光标处开始的#行内容
粘贴 p(小写) 粘贴到光标所在行之下
P(大写) 粘贴到光标所在行之上
查找 / word 从当前光标处开始向后进行查找字符串"word"
?word 从当前光标处开始向前进行查找
n 定位下一个匹配的被查找字符串
N 定位上一个匹配的被查找字符串
撤销 u 按一次取消最近的一次操作;重复u键,恢复多步操作
U 用于取消对当前行所做的所有编辑
保存退出 : w 保存修改的内容
:w 新文件名 另存为其它文件
:q
:q! 放弃对文件内容的修改并退出
ZZ或:wq、:x 保存当前的文件内容并退出vi编辑器
打开新文件 :e 其它文件名 打开新的文件进行编辑
读入文件内容 :r 其它文件名 在当前文件中读入其他文件内容
文件内容替换 :s /old/ new 将当前行中查找到的第一个字符串"old”串替换为"new"
:s /old/ new/ g 将当前行中查找到的所有字符串 “old”替换为"new”
:#,# s/old/ new/ g Replace all strings "old" with "new" in the range of line number "#, #"
:% s/old/ new / g Replace all strings "old" with "new" in the entire file
:s /old/new/ c Add the c command at the end of the replacement command, and the user will be prompted for confirmation for each replacement action

Guess you like

Origin blog.csdn.net/zhangyuebk/article/details/113361250