zoukankan      html  css  js  c++  java
  • linux笔记3(过滤器、管道、常用文件管理操作指令)

    过滤器

    Linux中的应用工具

    Linux中的应用工具分为三种:交互工具,过滤器,编辑器

    输入:过滤器的数据来源

    输出:过滤器的数据去向

    重定向:标准输入、输出与错误输出,都可用特定符号改变数据来源或去向。

     

    标准输入、标准输出、标准错误输出,例:

    [root@redhat root]# ls -l /dev/std*

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stderr -> ../proc/self/fd/2

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stdin -> ../proc/self/fd/0

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stdout -> ../proc/self/fd/1

    以上分别人标准输入(stdin(0))、标准输出(stdout(1))、标准错误输出(stderr(2)),所指向的文件。

     

    输入重定向:

    使用"<"来重定向输入源,大多数据工具都会以其后的文件名作为输入源,例:

    [root@redhat a]# cat test.txt

    HELLO WORLD !

    I AM LUOWEI !

    WELCOME TO LINUX'S WORLD !

    [root@redhat a]# tr 'A-Z' 'a-z' < test.txt     test.txt文件中的内容中的大写改变成小写输出

    hello world !

    i am luowei !

    welcome to linux's world !

    [root@redhat a]#

     

    从当前文档输入:使用 << 让系统将一次键盘的全部输入,先送入虚拟的"当前文档",再一次性输入。

    需要一对字母、符号或字符串作为起始终结符。可以选择任意符号作为起始终结标识符。例:

    [root@redhat a]# cat test2.txt

    [root@redhat a]# cat > test2.txt <<!

    > this is test.

    > writted by luowei !

    > !

    [root@redhat a]# cat test2.txt

    this is test.

    writted by luowei !

    [root@redhat a]#

     

    输出重定向:

    例:

    [root@redhat a]# ls -l > test3.txt     将输出的内之余输出到test3.txt文件中

    [root@redhat a]# cat test3.txt

    总用量 20

    drwxrwxr-- 2 root root 4096 10 25 00:32 a_dir

    drwxr-xr-x 2 root root 4096 10 25 00:32 b_dir

    -rw-rw-r-- 1 root root 34 10 25 01:14 test2.txt

    -rw-r--r-- 1 root root 0 10 25 01:17 test3.txt

    -rw-r--r-- 1 root root 206 10 25 01:04 test.txt

    请尝试执行'ls --help'来获取更多信息。

    [root@redhat a]# ls --help >>test3.txt

    [root@redhat a]# cat test3.txt         把输出的内容追加到test3.txt

    总用量 20

    drwxrwxr-- 2 root root 4096 10 25 00:32 a_dir

    drwxr-xr-x 2 root root 4096 10 25 00:32 b_dir

    -rw-rw-r-- 1 root root 34 10 25 01:14 test2.txt

    -rw-r--r-- 1 root root 0 10 25 01:17 test3.txt

    -rw-r--r-- 1 root root 206 10 25 01:04 test.txt

    用法:ls [选项]... [文件]...

    列出<文件>的信息 (默认为目前的目录)

    如果不指定 -cftuSUX --sort 任何一个选项,则根据字母大小排序。

     

    长选项必须用的参数在使用短选项时也是必须的。

    -a, --all 不隐藏任何以 . 字符开始的项目

    -A, --almost-all 列出除了 . .. 以外的任何项目

    ………….

    [root@redhat a]# ls-l 2>test.txt 将标准错误输出到文件中,在">"符号前加个"2"

    [root@redhat a]# cat test.txt

    -bash: ls-l: command not found

    [root@redhat a]# su - luowei

    [luowei@redhat luowei]$ find /etc -name passwd > stdout

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$ cat stdout

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ find /etc -name passwd 2>stderr

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ cat stderr

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$

     

    双重定向输入输出

    例:

    [luowei@redhat luowei]$ find /etc -name passwd 2> stderr > stderr > stdout

    [luowei@redhat luowei]$ cat stderr

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$ cat stdout

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ ls

    stderr stdout test.txt

    [luowei@redhat luowei]$

     

    将正确定的输出信息与错误的信息输出到同一个地方(文件)

    [luowei@redhat luowei]$ find /etc -name passwd &> allout

    [luowei@redhat luowei]$ cat allout

    find: /etc/sysconfig/pgsql: 权限不够

    /etc/passwd

    find: /etc/default: 权限不够

    /etc/pam.d/passwd

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

     

    使用 &> 也可所有的输入都送到同一个地方

    [luowei@redhat luowei]$ find /etc -name passwd &> allout > allout 2>&1

    [luowei@redhat luowei]$

     

    管道

    使用 | 将前现代战争过滤器的输出直接送入后一个过滤器的输入。

    例:

    允许多重管道

    注意管道前过滤器的输出与管道后过滤器的输入数据类型匹配。

    例:

    [root@redhat root]# ls -l /etc |more 列出目录并实现翻页的效果

    [root@redhat root]# ls -l /etc |more |grep fstab         使用多重管道

    拷贝

    [root@redhat root]# echo "hello" > filea

    [root@redhat root]# cat filea

    hello

    [root@redhat root]# touch fileb

    [root@redhat root]# cat < filea > fileb        filea里的内容拷贝到fileb

    [root@redhat root]# cat fileb

    hello

    [root@redhat root]#

    将多个文件的内容输出合并到同一个文件

    [root@redhat root]# cat filea;cat fileb         查看两个文件的内容

    hello

    hello

    [root@redhat root]# cat filea fileb > file    fileafileb的内容输出到file

    [root@redhat root]# cat file

    hello

    hello

    [root@redhat root]# echo "hello luowei" > fileb

    [root@redhat root]# cat file* > file            将当前目录下以file打头的文件内容输出到file

    cat: file: input file is output file

    [root@redhat root]# cat file

    hello

    hello luowei

    [root@redhat root]#

     

    文件查找与文件管理

    可执行文件的搜索

    Which whereis 指令

    例:

    [root@redhat root]# which ls         显示ls的完整路径

    alias ls='ls --color=tty'

    /bin/ls

    [root@redhat root]# echo $PATH    系统路径

    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

    [root@redhat root]# alias ls 指示了指令ls --color=tty别名为ls

    [root@redhat root]# whereis ls        显示ls的路径及该文件的一些相关帮助信息

    ls: /bin/ls /usr/share/man/man1/ls.1.gz

    [root@redhat root]#

     

    Slocate 指令 显示含有关键字的所有文件名以及所在路径包含有关键字的文件与目录都会显示。

    [root@redhat root]# ls -l /usr/bin/locate

    lrwxrwxrwx 1 root slocate 7 10 25 00:57 /usr/bin/locate -> slocate

    [root@redhat root]#slocat passwd

    /var/lib/menu/kde/Applications/Preferences/redhat-userpasswd.desktop

    /var/www/manual/programs/htpasswd.html

    /etc/sysconfig/yppasswdd

    /etc/passwd

    ……

    find [路径] [参数]

    从指定路径下递归向下搜索文件

    支持按照各种条件方式搜索

    支持对搜索到的文件进一步用指定操作

    例:

    [root@redhat root]# find /root -user root        root目录下查找拥有者是root的文件

    [root@redhat root]# find /etc -user luowei        ect目录下查找拥有者是luowei的文件

    [root@redhat root]# find /etc -name services    ect目录下查找文件名是services的文件

    [root@redhat root]# find /etc -size +1000k        ect目录下查找文件大于1000k的文件

    [root@redhat root]# find /etc -size -1000k        ect目录下查找文件小于1000k的文件

    [root@redhat root]# find /root -type f            ect目录下查找正规的文件

    [root@redhat root]# find /dev -type b            ect目录下查找块设备文件

    [root@redhat root]# find /dev -type l                ect目录下查找链接文件

    [root@redhat root]# find /dev -type c            ect目录下查找字符文件

    [root@redhat root]# find /etc -nouser            ect目录下查找没有拥有者文件

    [root@redhat root]# find /etc -nogroup            ect目录下查找没有群组文件

    [root@redhat root]# find /home -perm 0644    /home目录下查找权限是读写读读的文件

    [root@redhat root]# su - luowei

    [luowei@redhat luowei]$ mkdir test

    [luowei@redhat luowei]$ cd test

    [luowei@redhat test]$ touch 6000            创建文件名为6000的文件        

    [luowei@redhat test]$ touch 2000            创建文件名为2000的文件

    [luowei@redhat test]$ touch 6600            创建文件名为6600的文件

    [luowei@redhat test]$ touch 4000            创建文件名为4000的文件

    [luowei@redhat test]$ ls

    2000 4000 6000 6600

    [luowei@redhat test]$ chmod 2000 2000        2000文件设置了gid

    [luowei@redhat test]$ chmod 4000 4000     4000文件设置了uid

    [luowei@redhat test]$ chmod 6000 6000     6000文件设置了giduid

    [luowei@redhat test]$ chmod 6600 6600     6600文件设置了giduid

    [luowei@redhat test]$ ls -l

    总用量 0

    ------S--- 1 luowei luowei 0 10 26 00:24 2000

    ---S------ 1 luowei luowei 0 10 26 00:24 4000

    ---S--S--- 1 luowei luowei 0 10 26 00:24 6000

    -rwS--S--- 1 luowei luowei 0 10 26 00:24 6600

    [luowei@redhat test]$ find /home/luowei/test -perm 6000     查找权限为6000的文件

    /home/luowei/test/6000

    [luowei@redhat test]$ find /home/luowei/test -perm -6000        '-' 表示查找文件权限与6000相与运算后权限仍为6000的文件

    [luowei@redhat test]$ find /home/luowei/test -perm +6000    '+' 表示查看文件权限与6000相或运算后权限仍为这个文件的权限的文件

     

    操作找到的文件

    语法:

        -find [路径] [参数]

        -exec 指令 {} \;

        -{} 代表find找到的文件

        -\ 禁止转意

        -; 表示本行指令结束

    例:

    [root@redhat root]# find /home/luowei/test -perm 6000 -exec chown test1.test {} \;

    /home/luowei/test目录下找权限为6000的文件,并将它改为test组下的test1用户

    [root@redhat root]# ls -l /home/luowei/test

    总用量 0

    ------S--- 1 luowei luowei 0 10 26 00:24 2000

    ---S------ 1 luowei luowei 0 10 26 00:24 4000

    ---S--S--- 1 test1 test 0 10 26 00:24 6000

    -rwS--S--- 1 luowei luowei 0 10 26 00:24 6600

     

    常用的文件操作指令

    wc

    统计文件中的行数,字数,占用字节数

    例:

    [root@redhat root]# wc test1.txt

    8 40 149 test1.txt

    8表示这个文件中的字符行数,40表示这个文件中的字数,149表示这个文件所占用的字节数,最后一个是文件名

     

    grep

    显示文件中匹配关键字的行

    例:

    [root@redhat root]# grep "2011" test1.txt     显示test1.txt文件中含有"2011"的行

    十月 2011

    [root@redhat root]# grep -n "2011" test1.txt     显示test1.txt文件中含有"2011"的行及行号

    1: 十月 2011

    [root@redhat root]# grep -v "2011" test1.txt     显示test1.txt文件中不含有"2011"的行

    1

    2 3 4 5 6 7 8

    9 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30 31

     

    Sort

    按序重排文本并送回显示

    例:

    [root@redhat root]# sort -t: -k3 /etc/passwd        以冒号为分隔,对第3栏进行排序

     

    diff

    报告文本差异内容

    例:

    [root@redhat root]# diff test1.txt test2.txt

    4,5c4,5            表示两个文件的第4行和第5行有不一样

    < 02 3 4 5 6 7 8    "<"表示第一个文件test1.txt

    < 09 10 11 12 13 14 15

    ---

    > 2 3 4 5 6 7 8    ">"表示第二个文件test2.txt

    > 9 10 11 12 13 14 15

     

    cmp

    报告文本差异位置

    例:

    [root@redhat root]# cmp test1.txt test2.txt

    test1.txt test2.txt differ: byte 60, line 4    在文件中第60个字节、第4行处出现了不同

     

    uniq

    去除文件中重复的行

    例:

    [root@redhat root]# vi test1.txt

     

    十月 2011

    1

    1

    02 3 4 5 6 7 8

    09 10 11 12 13 14 15

    09 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30 31

    ~

    "test1.txt" 10L, 191C 已写入

    [root@redhat root]# uniq test1.txt     去除test1.txt中的重复行

    十月 2011

    1

    02 3 4 5 6 7 8

    09 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30 31

     

    cut

    显示文件中的某一列

    例:

    [root@redhat root]# vi cut_test

     

    l u o

    w e i

    1 2 3

    a b c

    ~

    "cut_test" [] 4L, 24C 已写入

    [root@redhat root]# cut -f3 cut_test    显示cut_test3列的内容

    o

    i

    3

    c

    [root@redhat root]# vi cut1

     

    luowei@test.com

    luowei,luowei@test.com

    admin,admin@test.com

    [root@redhat root]# cut -f2 -d, cut1     显示cut1中以 , 分隔的第2列的内容

    luowei@test.com

    admin@test.com

    [root@redhat root]# cut -c4-8 cut1        显示cut1中第4-8个字符

    wei,l

    in,ad

     

    paste

    将文本按列拼接

    例:

    [root@redhat root]# paste test1.txt test2.txt > test3.txt    将两个文件的内容水平拼接到test3.txt

    [root@redhat root]# cat test3.txt

    十月 2011 十月 2011

    1 1

    1 2 3 4 5 6 7 8

    02 3 4 5 6 7 8 9 10 11 12 13 14 15

    09 10 11 12 13 14 15 16 17 18 19 20 21 22

    09 10 11 12 13 14 15 23 24 25 26 27 28 29

    16 17 18 19 20 21 22 30 31

    23 24 25 26 27 28 29

    30 31

    [root@redhat root]# cat test1.txt test2.txt > test4.txt    将两个文件的内容垂直拼接到test4.txt

    [root@redhat root]# cat test4.txt

    十月 2011

    1

    1

    02 3 4 5 6 7 8

    09 10 11 12 13 14 15

    09 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30 31

    十月 2011

    1

    2 3 4 5 6 7 8

    9 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30 31

     

     

     

     

     

     

     

  • 相关阅读:
    D2. Remove the Substring (hard version)(思维 )
    暑假集训
    AcWing:167. 木棒(dfs + 剪枝)
    AcWing:109. 天才ACM(倍增 + 归并排序)
    AcWing:99. 激光炸弹(前缀和)
    B. Interesting Array(线段树)
    Best Reward HDU
    G. Swapping Places
    How many HDU
    GSS4&&花仔游历各国
  • 原文地址:https://www.cnblogs.com/luowei010101/p/2230868.html
Copyright © 2011-2022 走看看