zoukankan      html  css  js  c++  java
  • linux文件操作

    # 知识要点

    1. 查找命令
    2. 管道
    3. 输出
    4. vim 编辑器
    5. alias别名
    6. 安装python

    主要命令: `find` , `locate` , `grep` , `echo` , `vim`

    # 1.查找命令

    # 命令搜索

    `whereis` 搜索命令位置和帮助文档的位置

    `which` 搜索位置和命令的别名

    1 [tree@tree ~]$ whereis ls
    2 ls: /bin/ls /usr/share/man/man1/ls.1.gz
    3 [tree@tree ~]$ which ls
    4 alias ls='ls --color=auto'
    5     /bin/ls
    View Code

    # 文件查找

    `find` 命令格式:`find [-path] -options`

    path :要查找的目录,默认是当前目录
    option:
    -name 按文件名的某种规则的查找
    -type 按文件类型查找
    -size 按文件大小查找
    通配符:
    *匹配任意内容
    ?匹配任意一个字符
    []匹配任意一个中括号内的字符

     1 [tree@tree ~]$ find /bin -name 'ping*'
     2 /bin/ping6
     3 /bin/ping
     4 [tree@tree ~]$cd /bin
     5 [tree@tree bin]$ find -name 'ping*'
     6 ./ping6
     7 ./ping
     8 [tree@tree bin]$ find -name 'ping?'
     9 ./ping6
    10 [tree@tree ~]$ find -name 'f*'
    11 ./f2
    12 ./f1
    13 [tree@tree ~]$ find /bin -name 'f*'
    14 /bin/find
    15 /bin/fgrep
    16 /bin/findmnt
    17 /bin/false
    18 /bin/fusermount
    19 [tree@tree bin]$ find -size 60k
    20 ./sed
    View Code

    `locate`命令

    安装: yum install mlocate

    locate 是在数据库中按文件名搜索,要查找的文件名中含有的字符串,搜索数据速度更快。搜索的数据库是: /var/lib/mlocate/mlocate.db ,这个数据库,每天自动更新一次,在使用locate之前,可以使用updatedb命令,手动更新数据库。

    初始化: sudo updatedb

    1 [tree@tree ~]$ sudo yum install mlocate
    2 
    3 [tree@tree ~]$ sudo updatedb
    4 [tree@tree ~]$ locate ff*
    5 /home/tree/ff1.txt
    6 /home/tree/ff2.txt
    7 /home/tree/ff3.txt
    8  
    View Code
     1 [tree@tree ~]$ ls
     2 a  b  ff.py  pass
     3 [tree@tree ~]$ locate ff.py
     4 /home/tree/ff.py
     5 /usr/lib/python2.6/toaiff.py
     6 /usr/lib/python2.6/toaiff.pyc
     7 /usr/lib/python2.6/toaiff.pyo
     8 [tree@tree ~]$ touch shishi.py
     9 [tree@tree ~]$ ls
    10 a  b  ff.py  pass  shishi.py
    11 [tree@tree ~]$ locate shishi.py
    12 [tree@tree ~]$ locate shishi.py
    13 [tree@tree ~]$ sudo updatedb
    14 [sudo] password for taka: 
    15 [tree@tree ~]$ locate shishi.py
    16 /home/tree/shishi.py

    `grep` 字符串搜索命令

    在文件中搜索符合条件的字符串,包含匹配,包含字符串中的行
    可使用正则表达式来匹配的内容。

    命令格式:grep [选项] 字符串 文件名

    常用选项:
    -n 显示行号
    -i 忽略大小写
    -v 排除指定字符串

    1 [tree@tree ~]$ grep root /etc/passwd
    2 root:x:0:0:root:/root:/bin/bash
    3 operator:x:11:0:operator:/root:/sbin/nologin
    4 [tree@tree ~]$ grep -n root /etc/passwd
    5 1:root:x:0:0:root:/root:/bin/bash
    6 11:operator:x:11:0:operator:/root:/sbin/nologin
    7 [tree@tree ~]$ grep -v root /etc/passwd

    # 2.管道

    将一个程序或命令的的输出作为另一个程序或命令的的输入

    管道可以把一系列命令连接起来,可以将前面的命令的输出作为后面命令的输入。使用管道符‘|‘来建立一个管道行。

    1 [tree@tree ~]$ man man        

     2 [tree@tree ~]$ man man | more 

    #### 3.输出

    ```
    标准输出 echo
    输出重定向:
    > 将内容写入一个文件中,如果这个文件存在则会删掉原来的内容
    >> 将内容写入一个文件的末尾

     1 [tree@tree ~]$ echo 12345
     2 12345
     3 [tree@tree ~]$ ls
     4 a b ff.py pass shishi.py
     5 [tree@tree ~]$ echo 12345 > ff.py
     6 [tree@tree ~]$ cat ff.py
     7 12345
     8 [tree@tree ~]$ cat /etc/passwd >> ff.py 
     9 [tree@tree ~]$ cat ff.py
    10 
    11 [tree@tree ~]$ cat ff.py | grep -n root
    12 2:root:x:0:0:root:/root:/bin/bash
    13 12:operator:x:11:0:operator:/root:/sbin/nologin

    # 4.vim编辑器

    安装vim

    yum install vim

    工作模式:命令模式、输入模式、末行模式

    模式之间切换,
    多次按ESC可以进入命令模式
    在命令模式下,按 i或o或a进入输入模式
    在命令模式下,按shift+; ,末行出现:冒号则进入末行模式
    按ESC回到命令模式

    进入与退出:
    vi filename 进入
    当打开一个文件时处于命令模式

    在末行模式下输入q退出文件
    wq 保存退出
    q! 不保存退出

    移动光标
    命令模式和编辑模式下都可以用上下左右键(或者h,j,k,l)

    输入文本
    在命令模式下
    按 i 从光标所在位置前面开始插入资料
    按 a 从光标所在位置后面开始输入资料
    按 o 在光标所在行下方新增一行并进入输入模式
    进入输入模式后,在最后一行会出现--INSERT--的字样

    复制与粘贴
    在命令模式下
    yy 复制整行内容到vi缓冲区
    yw 复制当前光标到单词尾内容到vi缓冲区
    y$ 复制当前光标到行尾的内容到vi缓冲区
    y^ 复制当前光标到行首的内容到vi缓冲区

    p 读取vi缓冲区的内容,并粘贴到光标当前的位置

    删除与修改
    命令模式下
    dd 删除光标所在行
    x 删除光标所在字符
    u 撤销上一次操作

    保存文档
    :q 结束编辑不保存退出,如果有修改不会退出
    :q! 放弃所做的更改强制退出
    :w 保存更改
    :wq 保存更改并退出

    # 5.alias别名

    查看别名`alias`

    定义命令别名格式:alias 新的命令='原命令 -选项/参数'

    [tree@tree ~]$ alias ll
    alias ll='ls -l --color=auto'
    [tree@tree ~]$ alias la='ls -a'

    取消别名:`unalias` 别名  

    1 [tree@tree ~]$ unalias la

    2 [tree@tree ~]$ la

    3 -bash: la: command not found 

    这种定义别名的方式只在当次登录有效,如果要永久定义生效,可以修改用户(非全部用户)自己的`alias` ,修改~/.bashrc文件,在文件中加上自己定义的`alias` 。

    这个修改要在下次登录才能生效,如果要立即生效则输入`source ~/.bashrc` 。

  • 相关阅读:
    实验一 命令解释程序
    复利计算器3.0更新版
    复利计算器3.0总结
    复利计算器2.0
    0414复利计算5.1-美观、输入更新
    0408结对汉堡-结对2.0
    0406复利计算程序5.0-结对编程
    《构建之法》第四章读后感
    复利计算4.0-单元测试
    操作系统实验一、 命令解释程序的编写
  • 原文地址:https://www.cnblogs.com/bytree/p/9346740.html
Copyright © 2011-2022 走看看