linux笔记(2)文件搜索与bash快捷键

定位与查找

文件查找

搜索命令有专门找文件的,还有路径中含有关键字的。

可用的有locate(快,能否找到取决于索引数据库中是否存放),find(慢但是一定能找到),whereis,which,type .

  • locate ***********
  • find *************
  • whereis (仅限文件) ***
  • which (仅限文件) ***
  • type (仅限文件) ***

find 命令

使用locate 查找文件

hicode@test:~/Desktop/temp$ locate run.py
/home/hicode/venv/lib/python2.7/site-packages/opentaxii/cli/run.py
/home/hicode/venv/lib/python2.7/site-packages/opentaxii/cli/run.pyc
/home/hicode/venv/lib/python2.7/site-packages/sqlalchemy/testing/distutils_run.py
/home/hicode/venv/lib/python2.7/site-packages/sqlalchemy/testing/distutils_run.pyc

使用whereis,which,typefind 查找文件,没有找到

  • which使用path去查找
  • whereis 用数据库去查找
  • find和locate 适合于找自己创建的文件,locate,which,whereis 适用于找系统文件,type 居中。
  • 查找的时候使用的是通配符
laber@ubuntu:~$ locate /sources.list
/etc/apt/sources.list
/etc/apt/sources.list.bak
/etc/apt/sources.list.d
/etc/apt/sources.list~
/home/laber/sources.list
/usr/share/doc/apt/examples/sources.list
/usr/share/man/de/man5/sources.list.5.gz
/usr/share/man/es/man5/sources.list.5.gz
/usr/share/man/fr/man5/sources.list.5.gz
/usr/share/man/it/man5/sources.list.5.gz
/usr/share/man/ja/man5/sources.list.5.gz
/usr/share/man/man5/sources.list.5.gz
/usr/share/man/pl/man5/sources.list.5.gz
/usr/share/man/pt/man5/sources.list.5.gz
laber@ubuntu:~$ locate /*sources.list       在通配符中*即表明任意,也表明了以后方结尾。用正则表示则为/.*source.list$/
/etc/apt/sources.list
/home/laber/sources.list
/usr/share/doc/apt/examples/sources.list
/var/lib/dpkg/info/python-pkg-resources.list

hicode@test:~/Desktop/temp$ whereis run.py
run:
hicode@test:~/Desktop/temp$ which run.py
hicode@test:~/Desktop/temp$ type run.py
bash: type: run.py: not found
hicode@test:~/Desktop/temp$ file run.py
run.py: ERROR: cannot open `run.py' (No such file or directory)

使用find 命令查找文件,一定查找的到,速度是问题

hicode@test:~/Desktop/temp$ find ~ -type f -name run.py
/home/hicode/test/run.py
/home/hicode/venv/lib/python2.7/site-packages/opentaxii/cli/run.py

列出/test/run.py文件所在目录的其他文件

hicode@test:~/Desktop/temp$ find ~ -type f -name run.py | head -n 1 |sed 's/run.py//g' |xargs ls -l 
total 12
-rw-r--r-- 1 hicode hicode   0 Jun 26 23:54 a.txt
-rw-rw-r-- 1 hicode hicode 157 Jul  1 18:07 result.txt
-rw-rw-r-- 1 hicode hicode  90 Jul  1 17:27 run.py
-rw-rw-r-- 1 hicode hicode  23 Jul  1 14:07 ss.sh

locate 不是基于PATH 去查找的,而是将文件索引建立成数据库,我的系统中Ubuntu 14.04 amd64 Server LTS 存放在/var/lib/mlocate/mlocate.db' 下。阮老师说每日更新,如果没找到可以使用updatedb 立即更新。用locate 去查找,速度快。

laber@ubuntu:~/test$ touch a.txt
laber@ubuntu:~/test$ locate /a.txt
laber@ubuntu:~/test$ sudo updatedb
[sudo] password for laber:
laber@ubuntu:~/test$ locate /a.txt
/home/laber/test/a.txt
/home/laber/test/no/a.txt
/home/laber/test/yes/a.txt
laber@ubuntu:~/test$

文件内容查找(update 07-19)

上述查找都是文件名查找,今天用到了内容查找,这里记录下。
我使用的是grep 命令。

查找当前目录下文件中含有`aim_chars` 的文件名。
grep aim_chars -H *

注意,不能使用cat * |grep aim_chars -H
因为这里用的是管道命令,第一条cat输出的是文件内容,而不会包含文件名。


bash快捷键

Linux -Bash keyboard Shortcut
http://ahei.info/bash.htm/comment-page-1#comment-436366
[删除操作]
ctrl+l              清屏
ctrl+u              清空光标左侧所有内容
ctrl+k              清空光标右侧所有内容       ***remember
    [单词删除,以空格做作删除边界]
    ctrl+w              删除光标所在单词左侧内容
    左alt+d               删除光标所在单词右侧内容
    [字符删除]
    ctrl+h              删除光标左侧字符      **
    ctrl+d              删除光标右侧字符      

[移动操作]
ctrl+a              光标移动到最左端
ctrl+e              光标移动到最右端
ctrl+b              光标向左移动 (behind)    **
ctrl+f              光标向右移动(forward)    **
alt+f               光标移动到当前单词末尾
alt+b               光标移动到当前单词开头


[历史操作]
ctrl+ r             查询历史记录
ctrl+/              undo  撤销操作
alt+.               取出上条命令中最后一个参数

ctrl+p                历史上一条记录
ctrl+n                历史下一条记录     **
ctrl+p 后不确认,然后ctrl+o  ctrl+p用于生成历史命令列表重复执行历史命令列表中的命令
                             ctrl+o 顺序执行ctrl+p形成的历史命令,回车执行则清空ctrl+p历史命令表    


[其他操作]
ctrl+m/j            执行命令


[控制操作]
ctrl+z              挂起当前程序(stopped,but exist)
ctrl+c              结束当前程序
ctrl+d              删除光标所在处字符,若命令为空,则退回会话


【others进程替换<(command)/>(command)】
 diff <(ls dir1) <(ls dir2)
 wget -q -O >(cat) http://baidu.com

文中参考链接

高效操作Bash

Linux的五个查找命令

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值