常用基本命令,Linux上常用的基本命令

grep命令

常用基本命令,Linux上常用的基本命令。功能:从文本文件或管道数据流中筛选相配的行及数据,协作正则表明式一齐利用,成效尤为强有力。

格式:

grep [options] [pattern] [file]

 

1,相称包涵”ghostwu”的行

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    what's your name?
     6    my name is ghostwu2
     7    
ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2

 

2,-v: 不含有,相当于取反

ghostwu@dev:~/linux/grep$ grep -v "ghostwu" ghostwu.txt 
how are you
fine think you
My name is Ghostwu
what's your name?

ghostwu@dev:~/linux/grep$ 

 

3,-n 增多行号

ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 
1:my name is ghostwu
6:my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 
2:how are you
3:fine think you
4:My name is Ghostwu
5:what's your name?
7:

 

4,-E,使用增加的egrep命令,情势中能够用正则表明式

ghostwu@dev:~/linux/grep$ cat ghostwu.txt 
my name is ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2

ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt 
my name is ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt 
how are you
fine think you
My name is Ghostwu

ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt 
1:my name is ghostwu
5:what's your name?
6:my name is ghostwu2

 

ca686亚洲城官网,5,-i选项,不区分轻重缓急写

ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt 
my name is ghostwu
My name is Ghostwu
my name is ghostwu2

 

6,-c :总括相称的行数,不是相配字符串的次数

ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 
2
ghostwu@dev:~/linux/grep$ grep -ci "ghostwu" ghostwu.txt 
3

ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 
2
ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu, nice to meet you,ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    what's your name?
     6    my name is ghostwu2
     7    

 

7,-o: 只输出相配到的字符串

ghostwu@dev:~/linux/grep$ grep -o "ghostwu" ghostwu.txt 
ghostwu
ghostwu
ghostwu@dev:~/linux/grep$ grep -oi "ghostwu" ghostwu.txt 
ghostwu
Ghostwu
ghostwu

 

8,-w: 只相当过滤的单词,类似于正确相配

ghostwu@dev:~/linux/grep$ grep -w "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
ghostwu@dev:~/linux/grep$ grep -wi "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
My name is Ghostwu
ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu, nice to meet you,ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    what's your name?
     6    my name is ghostwu2
     7    

 

9,常用的一招小技术,去除文件的注释和空行,在运营中,能够用那条命令把铺排文件的空行和注释去掉,然后用管道生成。那样布署文件相比易于查看和配置

ghostwu@dev:~/linux/grep$ grep -Ev "^$|#" ghostwu.php
<?php
    class Person {
        public $name = 'ghostwu';
        public $age = 20;
        public function showinfo(){
            echo $this->name . PHP_EOL;
            echo $this->age. PHP_EOL;
        }
    }
ghostwu@dev:~/linux/grep$ cat -n ghostwu.php 
     1    <?php
     2    
     3        class Person {
     4    
     5            #人名
     6            public $name = 'ghostwu';
     7    
     8            #年龄
     9            public $age = 20;
    10    
    11            #显示信息 
    12            public function showinfo(){
    13                echo $this->name . PHP_EOL;
    14                echo $this->age. PHP_EOL;
    15            }
    16        }

 

grep命令

那二日有俩男士问了自身linux的事,问作者在职业中需没有须要用到,需没有要求学会

复制:copy

[keysystem@localhost happydzy]$ cp file1 file2
[keysystem@localhost happydzy]$ ll 
total 8
-rw-rw-r--. 1 keysystem keysystem 12 Dec  3 01:24 file1
-rw-rw-r--. 1 keysystem keysystem 12 Dec  3 01:24 file2
[keysystem@localhost happydzy]$ 

功能:从文本文件或管道数据流中筛选相配的行及数据,合营正则表明式一同行使,功效更是强有力。

一个是做事一年不到的,小编跟她说,提议您读书,在今后料定是要用到的,就算应用的时机不多,不过会总比不会好

移动:move

[keysystem@localhost happydzy]$ mv file1 ..  
[keysystem@localhost happydzy]$ mv file2 dir/
[keysystem@localhost happydzy]$ 

格式:

另多少个是做事陆年的,居然不会linux,跟小编说他很排斥linux,为什么呢,因为做事中不供给利用,而且企业有运行,运转直接管linux,也是醉了

重命名:rename

[keysystem@localhost happydzy]$ mv file1 file2 #将file1重命名为file2
[keysystem@localhost happydzy]$ mv dir1 dir2 #dir2存在,则为移动操作

grep [options] [pattern] [file]

实则linux并不是有多难,对于做开辟的来说会或多或少基本才具就足以了

移除:remove

[keysystem@localhost happydzy]$ rm file1 #移除file1文件
[keysystem@localhost happydzy]$ rm -r dir #移除dir目录

1,相配包蕴”linuxidc”的行

引入一下自己利用的:

创造文件:touch 和 >

[keysystem@localhost happydzy]$ touch a.txt    #创建a.txt文件
[keysystem@localhost happydzy]$ >b.txt         #创建b.txt文件 
[keysystem@localhost happydzy]$ ll
total 0
-rw-rw-r--. 1 keysystem keysystem 0 Dec  3 01:41 a.txt
-rw-rw-r--. 1 keysystem keysystem 0 Dec  3 01:41 b.txt
[keysystem@localhost happydzy]$ 

ghostwu@dev:~/linux/grep$ cat -n linuxidc.txt
  my name is linuxidc
  how are you
  fine think you
  My name is linuxidc
  what’s your name?
  my name is linuxidc2
 
ghostwu@dev:~/linux/grep$ grep “linuxidc” ghostwu.txt
my name is linuxidc
my name is linuxidc2

centos6.5 + putty

创设目录:mkdir

[keysystem@localhost happydzy]$ mkdir dir
[keysystem@localhost happydzy]$ ll
total 4
drwxrwxr-x. 2 keysystem keysystem 4096 Dec  3 01:43 dir
[keysystem@localhost happydzy]$ 

2,-v: 不含有,相当于取反

先前用的centos七,然而以为不是很好广大跟6.5有异常的大的界别,相当不顺手,最终依然用回陆.5

查阅文件:cat

[keysystem@localhost happydzy]$ cat a.txt 
hello world
[keysystem@localhost happydzy]$