Linux基本命令

ls命令

此命令意义:显示指定目录里的文件及文件夹清单

 

root@iZ259pk6ziaZ:/home# ls

haocai

说明:显示此目录下的文件或文件夹。

 

wcjdeMBP:~ wcj$ ls -a

. .matplotlib Pictures

.. .mysql_history Public

.CFUserTextEncoding .python27_compiled PycharmProjects

说明:显示此目录下的文件或文件夹。(包括隐藏)

 

root@iZ259pk6ziaZ:/home# ls -l

total 4

drwxr-xr-x 4 root root 4096 Jun 20 17:30 haocai

说明:显示此目录下的文件或文件夹的权限、所有者、大小、时间戳、文件名

 

root@iZ259pk6ziaZ:/home# ls -R

.:

haocai

 

./haocai:

log  skynet

 

./haocai/log:

skynet.log  skynet.log.2016-05-26  skynet.log.2016-06-03  skynet.log.2016-06-13

 

./haocai/skynet:

account  comment       ingredient  sample       util.py

activity  config        menu  search       util.pyc

application.py  cookrecord    oplog  static       weixin

application.pyc  database.py   permission  storage.py

cache.pyc  database.pyc  push  storage.pyc

categoryLatest  feedback      README.md  templates

column  firmware      requirements.txt  user

 

./haocai/skynet/account:

__init__.py  __init__.pyc  module.py  module.pyc  view.py  view.pyc

说明:显示此目录下及子目录的文件或文件夹

 

root@iZ259pk6ziaZ:/home# ls -lR

.:

total 4

drwxr-xr-x 4 root root 4096 Jun 20 17:30 haocai

 

./haocai:

total 8

drwxr-xr-x  2 root root 4096 Jun 14 20:06 log

drwxr-xr-x 22 root root 4096 Jun 15 16:42 skynet

 

./haocai/log:

total 128

-rw-r--r-- 1 root root 102387 Jun 15 16:30 skynet.log

-rw-r--r-- 1 root root   9668 Jun  3 17:14 skynet.log.2016-05-26

-rw-r--r-- 1 root root    661 Jun 13 10:47 skynet.log.2016-06-03

-rw-r--r-- 1 root root   9528 Jun 15 09:52 skynet.log.2016-06-13

说明:-l-R的合并,同时具有这两个命令的功能

 

cat命令

此命令意义:显示文件内容

 

root@iZ259pk6ziaZ:/# cat text1.txt

2016-01

2016-02

2016-03

明:文件的全部内容

 

root@iZ259pk6ziaZ:/# cat >text4.txt

说明:创建文件4

 

 

 

 

root@iZ259pk6ziaZ:/# cat -n text1.txt>text2.txt

说明:把文件1内容和行号传给文件2,如果2不存在则创建,如果没有>指向文件2的话,文件2必须存在

 

root@iZ259pk6ziaZ:/# cat >>text1.txt<<EOF

> 2016-01

> 2016-01

> 2016-03

> EOF

说明:创建文件1并输入内容,EOF可以换成任何相同的字符

 

head命令

此命令意义:显示文件头部信息

 

root@iZ259pk6ziaZ:/# head text1.txt 

2016-01

2016-02

2016-03

2016-04

2016-05

2016-06

2016-07

2016-08

2016-09

2016-10

说明:显示文件110信息(默认)

 

root@iZ259pk6ziaZ:/# head -5 text1.txt

2016-01

2016-02

2016-03

2016-04

2016-05

说明:显示文件15信息。

root@iZ259pk6ziaZ:/# head -n -10 text1.txt

2016-01

2016-02

说明:显示文件1去掉末尾10信息。

 

2016-0root@iZ259pk6ziaZ:/# head -c 12 text1.txt

2016-01

2016

说明:显示文件112字节的信息。

 

2016-root@iZ259pk6ziaZ:/# head -c -74 text1.txt

2016-01

2016-02

2016-0

说明:显示文件1去掉74字节的信息。

 

tail命令

此命令意义:显示文件尾部信息

 

root@iZ259pk6ziaZ:/# tail text1.txt 

2016-03

2016-04

2016-05

2016-06

2016-07

2016-08

2016-09

2016-10

2016-11

2016-12

说明:显示文件1的尾10信息(默认)

 

 

root@iZ259pk6ziaZ:/# tail -5 text1.txt

2016-08

2016-09

2016-10

2016-11

2016-12

说明:显示文件1的末尾5信息。

 

root@iZ259pk6ziaZ:/# tail -n +11 text1.txt

2016-11

2016-12

说明:显示文件1从第11开始到末尾的所有信息。

 

root@iZ259pk6ziaZ:/# tail -f text1.txt

2016-03

2016-04

2016-05

2016-06

2016-07

2016-08

2016-09

2016-10

2016-11

2016-12

说明:循环显示文件1所有信息。(动态实时)

 

 

wc命令

此命令意义:统计指定文件中的行数、字数、子节数

 

root@iZ259pk6ziaZ:/# wc -l text1.txt

12 text1.txt

说明:显示文件1行数。

 

root@iZ259pk6ziaZ:/# wc -w text1.txt

12 text1.txt

说明:显示文件1的单词数。

 

root@iZ259pk6ziaZ:/# wc -c text1.txt

96 text1.txt

说明:显示文件1字节数。

 

root@iZ259pk6ziaZ:/# wc text1.txt

12 12 96 text1.txt

说明:显示-l,-w,-c的功能

 

root@iZ259pk6ziaZ:/# wc -m text1.txt

96 text1.txt

说明:显示文件1的字符数。

 

root@iZ259pk6ziaZ:/# wc -L text1.txt

7 text1.txt

说明:显示文件1最长行的长度。

 

 

free命令

此命令意义:显示Linux系统中已用的物理内存空闲的、swap内存,及被内核使用的buffer

 

root@iZ259pk6ziaZ:/# free

      total       used       free     shared    buffers cached  

Mem:1025500     926648     98852        0       160176  581224

-/+ buffers/cache:185248  840252

Swap:   0          0          0

说明:默认显示kb为单位的字节内存。

第一行:

total:总计物理内存的大小。

used:已使用多大。

free:可用有多少。

Shared:多个进程共享的内存总额。

Buffers/cached:磁盘缓存的大小。

第二行:

used:已使用多大。

free:可用有多少。

第三行

交换分区SWAP的,也就是我们通常所说的虚拟内存

Page cache实际上是针对文件系统的,是文件的缓存

Buffer cache是针对磁盘块的缓存,也就是在没有文件系统的情况下,直接对磁盘进行操作的数据会缓存到buffer cache

 

 

 

root@iZ259pk6ziaZ:/# free -b

             total       used       free     shared    buffers     cached

Mem:    1050112000  949014528  101097472          0  164020224  595243008

-/+ buffers/cache:  189751296  860360704

Swap:        0          0          0

说明:显示Byte为单位的字节内存。

-m,MB为单位显示内存使用情况。

-g,GB为单位显示内存使用情况。 

 

root@iZ259pk6ziaZ:/# free -s 2

             total       used       free     shared    buffers     cached

Mem:       1025500     926772      98728          0     160176     581292

-/+ buffers/cache:     185304     840196

Swap:            0          0          0

 

             total       used       free     shared    buffers     cached

Mem:       1025500     926780      98720          0     160176     581292

-/+ buffers/cache:     185312     840188

Swap:            0          0          0

 

             total       used       free     shared    buffers     cached

Mem:       1025500     926780      98720          0     160176     581292

-/+ buffers/cache:     185312     840188

Swap:            0          0          0

说明:每2s 执行一次free命令。(循环)

 

 

 

 

find命令

此命令意义:目录结构中搜索文件

 

root@iZ259pk6ziaZ:/# find . -name '*.log'

./root/.pip/pip.log

./home/haocai/log/skynet.log

./var/log/mail.log

说明:find后面的点是当前目录,查找当前目录下以.log结尾的文件。

 

root@iZ259pk6ziaZ:/# find -atime -1

./proc/asound

./proc/asound/pcm

./proc/asound/timers

./proc/asound/modules

./proc/asound/cards

./proc/asound/devices

./proc/asound/version

./proc/asound/seq

说明:查找1天内修改过的文件。

-amin n 查找系统中最后N分钟访问的文件

 

root@iZ259pk6ziaZ:/# find . -perm 777

./dev/stderr

./dev/stdout

./dev/stdin

./dev/fd

./dev/core

说明:查找当前目录下权限777文件。

 

 

 

root@iZ259pk6ziaZ:/# find . -type d | sort

./var/spool/exim4/msglog

./var/spool/rsyslog

./var/tmp

说明:查找当前目录下所有目录并从小到大排序。f 是普通文件。

 

grep命令

此命令意义:文本搜索工具,能使用正则表达式搜索文本

grep的规则表达式:

^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。    

$  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。    

.  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p    

 #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。    

.*   #一起用代表任意字符。   

[]   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grepgrep    

[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-RT-Z的一个字母开头,紧跟rep的行。    

\(..\)  #标记匹配字符,如'\(love\)'love被标记为1    

\<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。    

\>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。    

x\{m\}  #重复字符xm次,如:'0\{5\}'匹配包含5o的行。    

x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5o的行。    

x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10o的行。   

\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p   

\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   

\b    #单词锁定符,如: '\bgrep\b'只匹配grep  

POSIX字符:

为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:][A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9][[:alnum:]]。在linux下的grepfgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符   

[:alpha:]    #文字字符   

[:digit:]    #数字字符   

[:graph:]    #非空字符(非空格、控制字符)   

[:lower:]    #小写字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #标点符号   

[:space:]    #所有空白字符(新行,空格,制表符)   

[:upper:]    #大写字符   

[:xdigit:]   #十六进制数字(0-9a-fA-F  

 

 

 

 

 

 

 

root@iZ259pk6ziaZ:/# ps -ef|grep python

root      8328     1  0 Jun24 ?        00:00:29 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8331  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8332  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root     13906 13855  0 09:48 pts/0    00:00:00 grep python

说明:查找指定进程(这里是python进程)最后一个是grep进程得去除

 

root@iZ259pk6ziaZ:/# ps -ef|grep python -c

4

说明:查找指定进程(这里是python进程)数量,其中也包括了grep进程本身哦。

 

root@iZ259pk6ziaZ:/# cat text1.txt | grep -f text2.txt

hahah

xixixi

说明:输出文件1中含有文件2中读取出的关键词的内容行

 

root@iZ259pk6ziaZ:/# cat text1.txt | grep -nf text2.txt

13:hahah

14:xixixi

说明:输出文件1中含有文件2中读取出的关键词的内容行显示行号。

 

root@iZ259pk6ziaZ:/# grep 'xi' text1.txt

xixixi

说明:再文件1找包含xi字符的内容行。

root@iZ259pk6ziaZ:/# grep 'xi' text1.txt text2.txt

text1.txt:xixixi

text2.txt:xi

说明:多个文件找包含xi字符的内容行。

 

root@iZ259pk6ziaZ:/# ps -ef|grep \[p]ython

root      8328     1  0 Jun24 ?        00:00:29 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8331  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8332  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

说明:查找python进程并去除grep进程。

 

root@iZ259pk6ziaZ:/# ps -ef|grep python|grep -v 'grep'

root      8328     1  0 Jun24 ?        00:00:29 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8331  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

root      8332  8328  0 Jun24 ?        00:00:40 /usr/bin/python /usr/bin/gunicorn --workers=2 -b :8000 application:app

说明:查找python进程并去除grep进程。-v不包含grep字符的内容行。

 

root@iZ259pk6ziaZ:/# cat text1.txt |grep ^x

xixixi

说明:查找限制开头是x的内容行。

 

 

root@iZ259pk6ziaZ:/# cat text1.txt |grep ^[^x]

2016-01

2016-02

2016-03

说明:查找不限制开头是x的内容行。

 

root@iZ259pk6ziaZ:/# cat text1.txt |grep xi$

xixixi

说明:查找以xi结尾的内容行。

 

root@iZ259pk6ziaZ:/# cat text1.txt |grep -E 'xi|ha'

hahah

xixixi

说明:查找包含xiha的内容行。

 

root@iZ259pk6ziaZ:/#  grep '[a-z]\{3\}' *.txt

text1.txt:hahah

text1.txt:xixixi

text1.txt:oooooo

text1.txt:llllll

text3.txt:     4 sad

text3.txt:     8      4 sad

说明:查找包含以txt结尾的文件里面至少有三个小写字母连续。

 

 

sed命令

此命令意义:逐行处理文件(或输入),并将结果发送到屏幕。

 

 

[root@www ~]# sed [-nefr] [动作]

选项与参数:

-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到端上。但如果加上 -n 参数后,只有经过sed 特殊理的那一行(或者)才会被列出来。

-e :直接在命令列模式上 sed 编辑

-f :直接将 sed 作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;

-r sed 作支持的是延伸型正表示法的法。(是基表示法)

-i :直接修改取的文件内容,而不是出到端。

 

动作说明: [n1[,n2]]function

n1, n2 :不得会存在,一般代表『选择进作的行数』,例来,如果我的作是需要在 10  20 行之间进行的, 10,20[动作行为]

 

function

a :新增, a 的后面可以接字串,而些字串会在新的一行出(目前的下一行)

c :取代, c 的后面可以接字串,些字串可以取代 n1,n2 的行!

d 除,因除啊,所以 d 后面通常不接任何咚咚

i :插入, i 的后面可以接字串,而些字串会在新的一行出(目前的上一行)

p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

s :取代,可以直接行取代的工作哩!通常 s 作可以搭配正表示法!例如 1,20s/old/new/g 就是啦!

 

root@iZ259pk6ziaZ:/# sed -n '3p' text1.txt

2016-03

说明:打印3行数据。

root@iZ259pk6ziaZ:/# sed -n '3,6p' text1.txt

2016-03

2016-04

2016-05

2016-06

说明:打印3行到底6数据。

 

root@iZ259pk6ziaZ:/# sed '/sad/p' text3.txt

     1 2016-01

     2 2016-02

     3 2016-04

 

     4 sad

     4 sad

     5      1 2016-01

     6      2 2016-02

     7      3 2016-04

 

     8      4 sad

8      4 sad

说明:每行中有sad的行打印两遍。(默认输出)

 

root@iZ259pk6ziaZ:/# sed -n '/sad/p' text3.txt

     4 sad

8      4 sad

说明:只打印行中有sad的行。(取消默认输出)

 

root@iZ259pk6ziaZ:/# sed '$d' text3.txt

     1 2016-01

     2 2016-02

     3 2016-04

 

     4 sad

     5      1 2016-01

     6      2 2016-02

     7      3 2016-04

 

说明:删除最后一行。显示其他的。(就是不显示最后一行)

root@iZ259pk6ziaZ:/# sed  's/sad/hahaha/g' text3.txt

     1 2016-01

     2 2016-02

     3 2016-04

 

     4 hahaha

     5      1 2016-01

     6      2 2016-02

     7      3 2016-04

 

8      4 hahaha

说明:把每行中sad替换成hahahas是行,g是所有。

 

root@iZ259pk6ziaZ:/# sed '15,16d' text1.txt

2016-01

2016-02

说明:删除文件11516

 

root@iZ259pk6ziaZ:/# sed '5,$d' text1.txt

2016-01

2016-02

2016-03

2016-04

说明:删除文件15最后行。

 

root@iZ259pk6ziaZ:/# sed '2,5c hahahaha' text1.txt

2016-01

hahahaha

2016-06

2016-07

说明:25替换成hahahaha

 

 

awk命令

此命令意义:逐处理文件(或输入),并将结果发送到屏幕。

 

 

 

 

 

root@iZ259pk6ziaZ:/# awk '$3>0 {print $1,$2*$3}' emp.data

kang 40

hu 100

fang 165

yang 170

说明:如果第三列大于0话,第一列显示文件中第一列,第二列显示文件第二列与第三列的乘积

 

root@iZ259pk6ziaZ:/# awk '{ print NF, $1, $NF }' emp.data

3 wcj 0

3 tao 0

3 kang 10

3 hu 20

3 fang 30

3 yang 40

说明:如果第三列大于0话,第一列显示文件中第一列,第二列显示文件第二列与第三列的乘积

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值