Directory, file management and vi editor
- Linux directory structure
- View file content-cat
- View file content-more
- View file content -less
- View file content-head and tail
- Statistics file content-wc
- Retrieve and filter file content-grep
- Compression commands-gzip, bzip and decompression-d, gunzip, bunzip2
- Archive command-tar
- Text editor-vi, vim
Linux directory structure
The linux directory is a tree directory structure
Root Directory The
starting point of all partitions, directories, files, etc. In the
entire tree-shaped directory structure, an independent "/" is used to indicate
Collection of common subdirectories
table of Contents | Description |
---|---|
/root | Administrator's host (home) directory |
/home/xxx | Home directory of ordinary users other than the root user |
/bin | Store binary files, all user-executable commands. Actually a soft link, link to /usr/bin |
/sbin | Store binary files, only administrative commands that can be executed by the administrator. Actually it is also a soft link, link to /usr/sbin |
/boot | System kernel, startup file directory |
/dev | Store device files (CD-ROM, hard disk, etc.) |
/etc | Store configuration files for system programs and most applications (rpm, yum installation) |
/where | Store files that can be changed, including various log files |
/lib | Store the dynamic link shared library file of the system program (similar to the DLL file of windows) soft link to /usr/lib |
/usr | Store system user tools and programs |
/media | Removable media mount point, such as U disk, optical drive, etc. |
/proc | File to store the information of the mapping system |
/ mnt | Directory for temporarily mounting storage devices |
/opt | The directory where the third-party application is installed |
/tmp | Temporary files stored in the system |
View file content-cat
Function: directly display the content of the entire file (it is recommended to use cat only to view short files with only a few lines)
cat [option] file name...
Common options
Options | Description |
---|---|
-n | Number all output lines |
-b | No number for blank lines |
-s | Replace all consecutive multiple blank lines with one blank line (used to compress blank lines) |
View file content-more
Function: Full screen mode to display the content of the file in pages
more [Option] File name...
Interactive operation method
button | Description |
---|---|
Enter | Scroll down line by line |
space bar | Scroll down one screen |
b | Flip up one screen |
q | drop out |
It will automatically exit when it is turned down to the last page
. It cannot be turned up when used in combination with pipe operation.
"|" pipe symbol
Role: The result of the execution of the command on the left is handed over to the right for processing.
For example: ls -R /etc | more
View file content -less
Function: It has the same function as the more full-screen mode to display the contents of the file in a paging mode, but with more extended functions.
[Options] File name...
Interactive operation method
button | Description |
---|---|
Page Up | Page up |
Page Down | Page down |
/ | Find content |
n | Next content |
N | Previous content |
↑ | Scroll up line by line |
↓ | Scroll down line by line |
Other functions are basically similar
to the more command , it will not automatically exit when the last page is scrolled down
. It can be paged up when used in combination with pipe operations.
View file content-head and tail
head
Function: View part of the content at the beginning of the file (default 10 lines)
head -n file name... (n is the number of lines)
tail
Function: View a small part of the content at the end of the file (the default is 10 lines)
tail -n file name...
tail -f file name (track the dynamic update of the file tail content)
Statistics file content-wc
Function: Count the number of words in the file (Word Count) and other information
wc [Option]... Target file...
Common options
Options | Description |
---|---|
-l | Count the number of rows |
-w | Count the number of words |
-c | Count the number of bytes |
When the wc command does not have any options, the three options -lwc are used at the same time by default
Retrieve and filter file content-grep
Function: Find and display the line containing the specified character string in the file
grep [Options]... Search condition target file
Common options
Options | Description |
---|---|
-i | Not case sensitive when searching |
-v | Display all lines that do not contain matching text (reverse query, reverse match) |
-c | 只输出匹配到的总行数(不是匹配到的次数) |
-n | 显示匹配行及行号 |
-e(组合使用时需放最后) | 实现可多个查找条件的匹配,逻辑or关系 |
-E | 支持使用扩展正则表达式,相当于使用egrep命令 |
-o | 精确匹配,即“仅匹配”的意思 |
查找条件设置
选项 | 说明 |
---|---|
“^…” | 表示以…开头 |
“…$” | 表示以…结尾 |
“^$” | 表示空行 |
所有要查找的字符都要以双引号括起来
例如
grep -ie “^NAME” -e “7$” CentOS-Vault.repo
压缩命令-gzip、bzip与解压缩-d、gunzip、bunzip2
当使用gzip压缩一个纯文本文件时,效果是非常明显的,大约可以减少70%以上的文件大小
制作压缩文件
gzip [9] 文件名
bzip [9] 文件名
gzip 制作的压缩文件默认扩展名为".gz",原文件不再保留
bzip2制作的压缩文件默认扩展名为".bz2",原文件不再保留
“-9”是用来提高压缩的比率。默认值可为1(压缩速度最快,压缩质量最低)至9(压缩速度最慢,压缩率最高)之间的整数,默认为6(速度和质量都较为平衡的一个值)
解开压缩文件
-d:用于解开已经压缩过的文件,相当于使用gunzip、bunzip2命令
例如:
gunzip -d 文件名.gz
gzip -d 文件名.gz
bunzip2 文件名.bz2
bzip2 -d 文件名.bz2
归档命令-tar
归档命令也是一种压缩文件的方式,且可以保留源文件
制作归档文件
tar [选项] …归档文件名 源文件或目录
释放归档文件
tar [选项] …归档文件名 [-C 目标目录]
"[-C 目标目录]"是用来指定某个目录解压缩,默认是解压缩到本地
常用命令选项
选项 | 说明 |
---|---|
-C | 解压时指定释放的目标文件夹 |
-C | 创建.tar 格式的包文件 |
-x | 解开 .tar 格式的包文件 |
-f | 表示使用归档文件 |
-v | 输出详细信息(Verbose) |
-j | 调用 bzip2 程序进行压缩或解压 |
-z | 调用 gzip 程序进行压缩或解压 |
-p | 打包时保留文件及目录的权限 |
-P | 打包时保留文件及目录的绝对路径 |
-t | 列表查看包内的文件 |
例:
cd /etc/
tar -jcvf usershow.tar.bz2 passwd shadow(生成在本目录)
tar -jxvf usershow.tar.bz2 -C /opt/(解压缩到指定目录)
文本编辑器-vi、vim
作用与分类
作用:创建或修改文件;维护linux系统中的各种配置文件
linux中最常用的文本编辑器
vi:类似UNIX操作系统的默认文本编辑器
vim:vim是vi文本编辑器的增强
版本
!vim(返回上一次编辑的文件)
工作模式及切换选项
vi编辑器共有三种工作模式
命令模式:启动vi编辑器后默认进入命令模式,该模式中主要完成如光标移动、字符串查找,以及删除、复制、粘贴文件内容等相关操作
输入模式:该模式中主要的操作就是录入文件内容,可以对文本文件正文进行修改、或者添加新的内容。(处于输入模式时,vi编辑器的最后一行会出现"- - INSERT - -“的状态提示信息)
末行模式:该模式中可以设置vi编辑环境、保存文件、退出编辑器,以及对文件内容进行查找、替换等操作。(处于末行模式时,vi编辑器的最后一行会出现”:"提示符)
命令模式切换至输入模式
选项 | 说明 |
---|---|
a | 在当前光标位置之后插入内容 |
i | 在当前光标位置之前插入内容 |
o | 在光标所在行之下插入一个新行内容 |
O | 在光标所在行之上插入一个新行内容 |
A | 在所在行的行尾插入内容 |
I(大写i) | 在所在行的行首插入内容 |
命令模式
操作类型 | 操作键 | 功能 |
---|---|---|
翻页移动 | Page Down键或Ctrl+F Page Up键或Ctrl+B |
向下翻动一整页内容 向上翻动一整页内容 |
行内快速跳转 | Home键或^键以及数字0键 End键或$键 |
跳转到本行的行首 跳转到本行的行尾 |
行间快速跳转 | 1G或gg G #G M |
跳到文件内容的第1行 跳到文件的最后一行 跳到文件中的第#行(#用具体数字替换) 跳转至当前页的中间位置 |
显示行号 | :set nu :set nonu |
在编辑器中显示行号 取消行号显示 |
删除 | x或Delete键 dd #dd d^ d$ dw |
删除光标处的单个字符 删除当前光标所在行(带有剪切功能) 删除从光标开始的#行内容 删除当前光标之前到行首的所有字符 删除当前光标处到行尾的所有字符 删除光标处的整个单词 |
替换字符 | R或Shift+r | 替换当前光标处字符 |
复制 | yy #yy |
复制当前行整行的内容到剪切板 复制从光标处开始的#行内容 |
粘贴 | P p |
粘贴到光标所在行之下 粘贴到光标所在行之上 |
查找 | /word ?word n N |
从当前光标处开始向后进行字符串"word"的查找 从当前光标处开始向前进行查找 定位下一个匹配的被查找字符串 定位上一个匹配的被查找字符串 |
撤销 | u U |
按一次取消最近的一次操作;重复按恢复 多步操作 用于取消对当前所有的所有编辑 |
保存退出 | :w :w 新文件名 :q :q! zz或:wq、:x |
保存修改的内容 另存为其他新文件 退出 放弃对文件内容的修改并退出 保存当前的文件内容并退出vi编辑器 |
打开新文件 | :e 需要打开的新文件名 | 打开新的文件进行编辑 |
读入文件内容 | :r 需要读入的文件名 | 在当前文件中读入其他文件内容 |
文件内容替换 | :s /old/new :s /old/new/g :#,# s/old/new/g :% s/old/new/g :s /old/new/c (:全为的英文:,因为这里:s会出现表情所以用中文:代替) |
将当前行中查找到的第一个字符串"old"替换为"new" 将当前行中查找到的所有字符串"old"替换为"new" 在行号"#,#"范围内将所有"old"字符串替换为"new" 在整个文件范围内替换所有的"old"字符串为"new" 在替换命令末尾加入c命令,将对用户提醒每个替换动作的确认 (解释中文: ) |
补充 | :5,10 m 2 :5,10 co 2 Shift+3组合键 |
将5-10行的内容剪切到第2行下面 将5-10行的内容复制到第2行下面 在脚本文件中高亮所有的相关字符串 |