find in linux

注意:

1、find 有一个精简替代品: fd-find,可以替代它的80%的功能,用起来很简单且带彩色显示。https://github.com/sharkdp/fd。       后来发现fd-find有一些bug,至少有两个bug,一是不能查找不带后缀的文件,二是 --changed-within 不能查找文件。后来发现fdmin不需要了,详情见 find in linux 2

不能查找 --changed-within 的问题,可以使用下面的方法代替,下面的方法是查找 N 分钟之内被修改过的文件或者文件夹:

function fdmin --description 'find files changed within ... minutes. usage: fdmin <-N> [dir].  N is minutes'
    if test (count $argv) -lt 1
        echo "example: fdmin -6 ./"
        return 0
    end

    if test (count $argv) -eq 1
        find . -mmin $argv
    else
        find $argv[2] -mmin $argv[1]
    end

end

 

  

2、fzf是Linux终端下的一款交互式的模糊搜索神器,速度极快,还可以配合vim以及其他软件使用,可以说是终端党的必备神器。 fzf 是一个 交互式的 可以用于任何列表,文件,命令行历史,进程,主机名等的模糊搜索神器。它基于GO语言,没有其他依赖,速度非常快。

fzf模糊匹配功能非常好用,但是它没有搜索功能,只能作为字符匹配使用,因此必须和find、fd-find、ripgrepGit-grep等输出为文本的软件配合使用,其实凡是文本输出都可以交给fzf来进行模糊匹配。如果你不和别的搜索工具配合使用,在命令行直接运行fzf则是搜索当前目录下的所有文件,我自己做了一个可以搜索所有文件夹的fish shell 函数,叫 fzf.fish,具体见后面

配置一下fzf,这样匹配窗口更好看:

set -x FZF_DEFAULT_OPTS '--height 40% --layout=reverse --border'

fzf.fish:

function fzf     --description 'fzf with directory option. usage: fzf ../../a/b/'
    set -l params 
    set -l directory 

    for a in $argv
        # A -- argument tells the option parser to stop looking for flags and treat all subsequent arguments as non-flag args even if they would otherwise look like a flag.
        set -l first_char (string sub --length 1 -- $a)
        if test $first_char = '-'
            set -a params $a
        else
            set directory $a
        end
    end

    if set -q directory[1]
        find $directory | /home/hzh/soft/my-bin/fzf $params
    else
        # set directory "./"
        /home/hzh/soft/my-bin/fzf $params
    end
end

 

 

fzf搜索字符时的匹配规则:

Search syntax

Unless otherwise specified, fzf starts in "extended-search mode" where you can type in multiple search terms delimited by spaces. e.g. ^music .mp3$ sbtrkt !fire

TokenMatch typeDescription
sbtrkt fuzzy-match Items that match sbtrkt
'wild exact-match (quoted) Items that include wild
^music prefix-exact-match Items that start with music
.mp3$ suffix-exact-match Items that end with .mp3
!fire inverse-exact-match Items that do not include fire
!^music inverse-prefix-exact-match Items that do not start with music
!.mp3$ inverse-suffix-exact-match Items that do not end with .mp3

If you don't prefer fuzzy matching and do not wish to "quote" every word, start fzf with -e or --exact option. Note that when --exact is set, '-prefix "unquotes" the term.

A single bar character term acts as an OR operator. For example, the following query matches entries that start with core and end with either gorb, or py.

 

fzf自身带有fish包,不过jethrokuan/fzf是更好的替代品[注意,这两者是不兼容的,只能安装其一]。它提供一些额外的功能(需要先安装fisher,一个fish插件管理工具):

    • 键补全
    • 搜索命令行历史记录
    • 搜索并打开文件
    • 搜索并进入目录

下面是fzf的一些其他功能,自己暂时用不到,列在这里仅作参考,详细内容移步fzf主页

    • fzf-git
    • fzf-vim
    • fzf-tmux
    • 在fzf的交互环境中执行外部命令
    • 模糊补全(在Bash和Zsh中有效,在Fish中不可用:sad:,用fish包插件或替代品)

3、还有个小工具叫up (有fish和bash版)和bd (也有fish和bash版), 地址是:https://github.com/shannonmoeller/up  、https://github.com/0rax/fish-bd、https://github.com/welhzh/fish-bd和  https://github.com/vigneshwaranr/bd,可以用于方便的回到上级目录的任意目录,可以与pushd (回到任意目录)搭配使用。

我自己只使用bd,不使用up。但是bd不好敲且字面意思没有up准确,所以我使用alias把up指定为bd:   (其中的 -s 和 -i 不能换顺序!)

alias up 'bd -s -i'

 

https://github.com/shannonmoeller/up里的 up.fish代码如下:

up.fish就是单个文件,放在~/.config/fish/functions 下面:

function up            --description 'up to parent directory'
  # up one 
  if not count $argv > /dev/null
    cd ..
    return 
  end

  # go back 
  if test $argv[1] = '-' -o -d $argv[1] 
    cd $argv[1]
    return
  end

  # check for num
  set --local regex '^[0-9]+$'

  if string match -rq $regex $argv[1]
    set d (_upnum $PWD $argv[1])     
  else
    set d (_updir $PWD $argv[1])
  end 

  if test -d $d
    cd $d
    return
  end

  echo -e "usage: up [dir|num|-]\npwd: $PWD"
end

4、还有几个工具叫 exa,bat,其中 exa命令(比ls更好的展示文件体验)只能说还行,而bat  (https://github.com/sharkdp/bat)是 cat 的几乎完美的替代,非常好用(bat可以使用 --style=plain 参数隐藏行号及网格,方便文字拷贝)。

 

 

 

 

 

 

 

find命令的作用是在目录中根据文件名搜索文件
find 列出当前目录及其子目录的所有文件和文件夹的完整路径。
find -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件。
find . -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件(同上)。
find / -name Help.java 在整个硬盘中搜索文件名为Help.java的文件。
find -perm 755 在当前目录及其子目录中查找指定权限的文件
find -type b 在当前目录及其子目录下查找块设备文件。
find -type d 在当前目录及其子目录下查文件夹。
find -type c 在当前目录及其子目录下查找字符设备文件。
find -type p 在当前目录及其子目录下查找管道文件。
find -type l 在当前目录及其子目录下查找符号链接文件。
find -type f 在当前目录及其子目录下查找普通文件。
find -type d -exec ls -l {} \; 查找当前目录及其子目录下的文件夹,并将查找结果以ls -l的方式展现。
find -type d -ok rm -rf {} \;查找当前目录及其子目录下的文件夹,并将查找结果依次执行rm -rf命令,但是在执行命令前会有确认提示。

 

find . -type f  \( -name "*.class" -o -name "*.sh"  \)                           查找多个文件,注意\( 和 \) 前后的空格
find . -type f  \( -name "a.class" -o -name "b.sh"  \)                           查找多个文件,注意\( 和 \) 前后的空格
find . -type f  -exec ls -l {}  \;                                                         注意后面的\;
find . -name  "*.log" -exec cp {}  test3 \;
find  /tmp/   -path "/tmp/123"  -prune -o -type f  -print                   查找/tmp下所有文件时,排除指定的文件或者目录,这里是排除掉 /tmp/123
find  /tmp/  \( -path "/tmp/123" -o -path "/tmp/234" -o -path "/tmp/345" \)  -prune -o -type f -print               排除多个文件或目录
find ./cache  ! -name  '*.html'  -type f                                               查找cache目录下不是html的文件

 

# 在当前目录,排除 out 目录, 且查找不是*.c×  *.h*   *.*o*   *.a   *.xml   *.java   *.d* 类型的所有文件。注意 "./out" 里的这 ./ 很重要
$ find
. -path "./out" -prune -o ! -name "*.c*" ! -name "*.h*" ! -name "*.*o*" ! -name "*.a" ! -name "*.xml" ! -name "*.java" ! -name "*.d*" -type f
# 通俗的解释就是: 查找某些文件,它不在 out 目录里,且不是这些文件:*.c* *.h* *.*o* *.a *.xml *.java *.d*
# 在当前目录,排除 out 目录, 且查找不是 *.xml *.java 类型, 但是文件名字为 makefile 或 *.mk  或  *.sh 或 *config 的所有文件。 注意 "./out" 里的这 ./ 很重要
find
. -path "./out" -prune -o -iname "makefile" -o -iname "*.mk" -o -iname "*.sh" -o -iname "*config" ! -iname "*.xml" ! -iname "*.java" -type f

 

 

查找`2013-08-08`到`2013-09-01`号之间的文件,使用如下命令即可:

find /log/ -name 'production.log-2013*' -newermt '2013-08-08' ! -newermt '2013-09-0

找出 3 天”以前”被改动过的文件:

find /var/log/ -mtime +3 -type f -print

找出 3 天內被改动过的文件:

find /var/log/ -mtime -3 -type f -print

找出当前时间以前的第 3 天被改动过的文件:

find /var/log/ -mtime 3 -type f -print

找出第 3 天被改动过的文件:

find /var/log/ -mtime +2 -mtime -4 -type f -print

查找10分钟之内被修改过的文件:

find . -mmin -10

查找当前文件夹下的文件(不包括目录),要求这些文件在20分钟之内被修改过,且文件名不是 "*.o"   "*.cmd"  这种结尾的文件:

find . -mmin -20 ! -name "*.o" ! -name "*.cmd"  -type f

 

 

查看文件中的一段文字,输出到标准输出:

# 打印从第 20 行开始到第 60 行结束的所有行

sed -n '20,60p;61q' file_name                    # 一般显示终端只能显示40行,所以就显示40行就可以了

posted @ 2014-06-17 13:36  微信公众号--共鸣圈  阅读(261)  评论(0编辑  收藏  举报