AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 526936
Accepted
Teddy C
Teddy C
Asked: 2019-06-26 18:04:01 +0800 CST2019-06-26 18:04:01 +0800 CST 2019-06-26 18:04:01 +0800 CST

查找 - 我如何创建别名来执行类似 (find . -iname '*$1*') 的操作?

  • 772

我有一个findn功能:

findn () {
    find . -iname "*$1*"
}

如果文件名包含空格,则使用此函数有一个缺点,我无法使用-print0 | xargs -0 command(我正在使用 mac)来扩展 find 命令的功能。findn filename

那么,无论如何我可以同时保持方便的功能-iname "*$1*"吗| xargs command?

我正在考虑使用别名来做到这一点,但它不一定是别名。

find alias
  • 2 2 个回答
  • 245 Views

2 个回答

  • Voted
  1. Freddy
    2019-06-26T18:20:30+08:002019-06-26T18:20:30+08:00

    您的解决方案适用于xargs:

    $ echo "foo bar one" > foobarone
    $ echo "foo bar two" > fooBARtwo
    $ findn "bar"
    ./fooBARtwo
    ./foobarone
    $ findn "bar" | xargs cat
    foo bar two
    foo bar one
    

    还是我错过了什么?

    如果你稍微修改你的函数,你可以在你的find命令中添加额外的参数:

    findn () {
      local name=$1
      shift
      find . -iname "*$name*" "$@"
    }
    

    例子:

    $ findn bar ! -name '*two' -print0 | xargs -0 cat
    foo bar one
    
    • 4
  2. Best Answer
    Stéphane Chazelas
    2019-06-26T22:22:56+08:002019-06-26T22:22:56+08:00

    使用 GNUfind或兼容(-iname无论如何已经是 GNU 扩展)的一种方法可能是将函数定义为:

    findn() (
      if [ -t 1 ]; then # if the output goes to a terminal
        action=-print  # simple print for the user to see
      else
        action=-print0 # NUL-delimited records so the output can be post-processed
      fi
      first=true
      for arg do
        if "$first"; then
          set -- "$@" '('
          first=false
        else
          set -- "$@" -o
        fi
        set -- "$@" -iname "*$arg*"
        shift
      done
      "$first" || set -- "$@" ')'
      exec find . "$@" "$action"
    )
    

    然后您可以将其用作:

    findn foo bar
    

    查看包含or的文件名(如果您想要同时包含and的文件名,请将其更改为上面的名称)。foobar-o-afoo bar

    和:

    findn foo bar | xargs -r0 cat
    

    如果要对找到的每个文件应用命令findn。

    对于同时执行和不执行的变体:

    findn() (
      if [ -t 1 ]; then # if the output goes to a terminal
        action=-print  # simple print for the user to see
      else
        action=-print0 # NUL-delimited records so the output can be post-processed
      fi
      first=true
      for arg do
        if "$first"; then
          set -- "$@" '('
          first=false
        else
          set -- "$@"
        fi
        if [ "$arg" = ! ]; then
          set -- "$@" !
        else
          case $arg in
            (*[][*?\\]*)
              # already contains wildcard characters, don't wrap in *
              set -- "$@" -iname "$arg"
              ;;
            (*)
              set -- "$@" -iname "*$arg*"
              ;;
          esac
        fi
        shift
      done
      "$first" || set -- "$@" ')'
      exec find . "$@" "$action"
    )
    

    接着:

    findn foo bar ! baz
    

    对于同时包含fooandbar和 not的文件名baz。

    在那个变体中,我还做了这样的设置,如果参数包含通配符,则按原样处理,因此您可以执行以下操作:

    findn foo ! 'bar*'
    

    查找不以 bar开头的文件。如果您使用的是zshshell,则可以创建一个别名:

    alias findn='noglob findn'
    

    要禁用允许您编写的该命令的通配符:

    find foo ! bar*
    

    您可能希望创建一个脚本(这里一个sh脚本就足够了,因为该语法是 POSIX)而不是一个函数,因此可以从任何地方调用它,而不仅仅是您的 shell。

    • 2

相关问题

  • 将变量从子shell打印到父shell [重复]

  • 检查某个文件夹是否存在于某个目录中

  • 从命令行查找和替换 CSS 文件中的颜色

  • Bash:为什么远程运行时忽略换行符后的别名?

  • GNU find:在-exec中获取绝对和相对路径

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve