linux详细入门教程(三)------linux Shell命令操作(2)

目录

1浏览及搜索文件系统

 2、阅读文本文件

3、编辑文本文件

4、文件内容操作命令

5、文件的复制、移动、链接和归档


1浏览及搜索文件系统

        1、which

在环境变量 $PATH 设置的目录里查找符合条件的文件,查看当前生效的命令是哪个。
        格式 which  programname [...]
示例

         

        2、whereis

               根据文件名搜索二进制文件、手册页文件或源代码文件。

        格式:  whereis  [-bms]  filename [...]

      常用选项:

-b  只找二进制文件

-m  只找手册页文件   

-s  只找源文件

                 示例

                 3、locate

                        在保存文档和目录名称的系统数据库(/var/lib/mlocate/mlocate.db)内查找文件名符合指定模式的文件。数据库中的条目一般每天自动更新一次。

速度比 find 快,但可以搜索不到最新变动过的文件。
格式 locate [OPTION]... [PATTERN]
常用选项
-e, --existing  只显示当前存在的文件条目
- i , --ignore-case  匹配模式时忽略大小写区别
-l LIMIT  限制仅显示 LIMIT 条结果
示例

 

 

 2、阅读文本文件

        1、cat                 

将文件或标准输入连接合并,然后输出到标准输出。
两项功能,一是显示文件内容,二是连接合并文件内容。
选项 -n ,其作用是在每行前面显示行号。

                 

        创建新文件

                cat命令不带文件名参数时,接受标准输入作为其输出内容。

 

 2、more & less

more
分页显示文件内容,每次一屏。
基本操作按键
空格  向前翻页
向后翻页
退出。到最后一页后会自动退出。
选项
+<num>  从第 num 行开始显示。

        less

        和more命令基本一样, 但功能更强。

        基本操作按键

        空格  向前翻页

        b  向后翻页

        k  or ↑  向上滚动一行

        j   or ↓  向下滚动一行

        /<word>  搜索<word>

        q  退出

 3、head & tail

head
在屏幕上显示指定文件的开头若干行,默认 10 行。
选项
-<num>  显示 <num>

                 tail

在屏幕上显示指定文件的开头若干行,默认10行。 

option

-<num>  显示<num>

3、编辑文本文件

                 1、vi

“Visual Interface” 的缩写。类 Unix 系统中最常用的编辑器。
vim: VI Improved.
Linux 下的 vi 命令实际是一个 vim 命令别名或到 vim 命令文件的符号连接。
和一般文本编辑器的不同之处:多模式编辑器,尤其适合编码。
命令模式
输入模式
末行模式

 

move cursorcommand mode

[NUM]h or Left Arrow(←)

move left(注:[NUM] is an optional digitdenotes the repetition times of the commande.g., 10h means 10 characters to the left

[NUM]j or Down Arrow(↓)

move down

[NUM]k or Up Arrow(↑)

move up

[NUM]l or Right Arrow(→)

move right

[Ctrl] + [f] or [PgDn]

scroll down one page

[Ctrl] + [b] or [PgUp]

scroll up one page

[Ctrl] + [d]

scroll down half a page

[Ctrl] + [u]

scroll up half a page

0 or [Home]

to the first character of the line

$ or [End]

to the end of the line

^

 to the first non-blank character of the line

H

To the top line of window

M

To middle line of window

L

To bottom line of window

G

to the last line of the document

[NUM]G

to the NUMth line of the document

gg

to the first line of the document.  Equivalent to 1G.

[NUM]<Enter>

Num lines down

w

to next word

b

to previous word

find & replacecommand mode

/word

search forward for <word> from the cursor position.

?word

search backward for <word> from the cursor position

n

repeat the last searching.

N

repeat the last searching, but in the opposite direction.

find & replacelast line mode

:n1,n2s/word1/word2/g

n1 and n2 are digit. n2 can be ‘$’ which denotes the last line.

replace all word1 between line n1and line n2 to word2

e.g., :20,30s/If/if/g

: n1,n2s/word1/word2/gc

same as above.

c means confirmation is required before replacing. Enter ‘y’ or ‘n’ to agree or deny the replacement.

deletecopy & paste

[NUM]x[Delete]

delete <num> character under and after the cursor

[NUM]X[Backspace]

delete <num> character before the cursor

[NUM]dd

delete <num> line under and after the cursor

dgg

delete all lines from the first line to the line under cursor.

dG

delete all lines from the cursor to the last line

d$

delete all characters from cursor to the end of the line

d0

delete all characters from curosr to the first character of the line

[NUM]yy

copy <num> line under and after the cursor

ygg

copy all lines from the first line to the line under cursor

yG

copy all lines from the cursor to the last line.

y$

copy all characters from cursor to the end of the line

y0

copy all characters from cursor to the first character of the line

p

paste what copied to position after cursor or the line after cursor

P

paste what copied to position before cursor or the line before cursor

J

join the line under cursor and the line after cursor

u

cancel the last operation.

[Ctrl]+r

redo the operation canceled.

.

redo the last operation.

other commands (last line mode)

:w

:w!

:w [filename]

:r [filename]

:n1,n2 w [filename]

:! command

:set nu

:set nonu

:set ic

:set noic

 2、文本编辑器(gedit)

GUI
单模式文本编辑器
易于学习和使用

4、文件内容操作命令

1、grep

最常用的命令之一。
在文件中或标准输入中查找某项内容。
可使用正则表达式进行匹配查找,功能强大。
格式 grep [option] pattern [file]
三类正则表达式
BREs  Basic Regular Expression ,默认。
EREs  Extended Regular Expression.
PREs  Perl Regular Expression.
使用选项 -E 选用 EREs, -P 选用 PREs, “– F 不使用正则表达式。
使用基本正则表达式时,必须在字符 ?,+,|,{,}, , 前加上转义字符( '\' )。
常放在管道符号的右边以对前一命令的输出进行过滤。

         

选项
-f, --file=FILE  FILE 中取得 PATTERN
- i , --ignore-case  忽略大小写
-w, --word- regexp  强制 PATTERN 仅完全匹配字词 .
-x, --line- regexp  强制 PATTERN 仅完全匹配一行 .
-v, --invert-match  匹配与 PATTERN 相反的模式
-m, --max-count=NUM  查找到 NUM 次匹配后即停止查找
-n, --line-number  输出行号
-R, -r, --recursive  递归到子目录中查找 .
--include=FILE_PATTERN  仅在文件名与 FILE_PATTERN 匹配  的文件中查找
--exclude=FILE_PATTERN  不查找文件名与 FILE_PATTERN 匹  配的文件

                                                示例

 

 

 

 2、sort

对文本文件的各行进行排序。文本的顺序由系统所使用的字符集决定。
格式:  sort [option] [file]
如果不指定文件,则排序内容来自标准输入。
排序的比较操作是依据从每一行中提取的一个或多个字段来进行的。默认情况下,以空白字符分隔每个字段。
选项
-b, --ignore-leading-blanks  忽略前导的空白区域
-f, --ignore-case  忽略字母大小写
-n, --numeric-sort  比较数值而非字符串值
-r, --reverse                逆序输出排序结果
-c, --check  检查输入是否已排序
-k, --key=POS1[,POS2]  比较从字段 1 开始到字段 2 之间的内容,字段 省略时默认为行尾
-o, --output=FILE  将结果写入到文件而非标准输出
-t, --field-separator=SEP  使用 SEP 字符作为字段分隔符
-u, --unique                在输出结果中去除重复行
示例

        

 

 

 

 3、diff

按行比较两个文本文件,找出它们的不同之处。
格式  diff [option] file1 file2
如果两个文件完全一样,则命令不显示任何输出。
选项
- i   --ignore-case    忽略大小写的区别
-b    忽略由空格数不同造成的差异
-y  --side-by-side  以两列并排的方式显示
-W NUM  每行显示最多 NUM ( 默认 130) 个字符
示例

         

tosort.txt

 tosort2.txt

 

 



4、wc

统计文件的行数、字数和字节数。如果未指定文件,则对标准输入进行统计。
格式:  wc [option] [FILE]
是指由空白字符(如空格、制表符、换行符等)分隔的字符串。
选项
        -c, --bytes           统计字节数
        -m, --chars          统计字符数
        -l, --lines              统计行数
        -w, --words  统计字数

5、文件的复制、移动、链接和归档

        1、cp

将源文件复制至目标文件,或将多个源文件复制至目标目录。
格式                  cp [OPTION]... SOURCE DEST

                  或    cp [OPTION]... SOURCE... DIRECTORY

                  或    cp [OPTION]... -t DIRECTORY SOURCE...

                 或    cp [OPTION]... –r SOURCE_DIR DEST_DIR

                 或    cp [OPTION]... –a SOURCE_DIR DEST_DIR

选项
-d  保留符号链接
-f, --force  覆盖目标文件并且不提示
- i , --interactive  覆盖前询问
-n  不覆盖已存在的文件
-p  保留文件的属性
--parents  复制前在目标目录创建源文件路径中的所有目录
-u, --update  只在源文件比目标文件新,或目标文件不存在时才进行  复制
   -v, --verbose  显示详细的进行步骤

 

 

 

         2.mv

将源文件重命名为目标文件,或将源文件移动至指定目录。
格式          mv [OPTION]... SOURCE DEST

          或      cp [OPTION]... SOURCE... DIRECTORY

            或    cp [OPTION]... -t DIRECTORY SOURCE...

选项
-f, --force  覆盖前不询问
- i , --interactive  覆盖前询问
-v, --verbose  显示详细信息
示例

 

 3、ln

创建硬连接或符号连接
格式  ln [OPTION]... TARGET LINK_NAME
连接的对象可以是文件也可以是目录。创建硬连接会使用文件的连接数增加。
选项
-f, --force          删除已存在的目标文件
- i , --interactive  提示是否要删除已存在的目标文件
-s, --symbolic      创建符号连接而非硬连接
示例

 4、tar

将多个文件保存至归档文件中,或从归档文件中还原所需的文件。
格式  tar [OPTION...] [FILE]...
options
-c, --create  创建一个新归档
-r, --append  将文件添加到归档的末尾
-t, --list                   列出归档内容
-x, --extract         从归档中解出文件
-k, --keep-old-files  解压时不要替换存在的文件
-j, --bzip2                  通过 bzip2 压缩或解压归档
-J, -- xz                     通过 xz 压缩或解压归档
-z, -- gzip , -- gunzip    通过 gzip 压缩或解压归档
-Z, --(un)compress     通过 compress 压缩或解压归档
-C, --directory=DIR   解压到 DIR 目录而非当前目录
-f, --file=ARCHIVE  指定归档文件或 ARCHIVE 设备

示例

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重生之我是泰勒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值