【问题标题】:using ls with grep to zip selection of files使用 ls 和 grep 压缩文件选择
【发布时间】:2020-02-13 12:40:27
【问题描述】:

我正在尝试通过使用 bash 来提高工作流程的效率。

我有这些文件 20201.csv 20202.csv 20203.csv 20191.csv 20192.csv

ls | grep '2020*' 

我正在提取从 2020 年开始的文件。 我正在与如何将这个较小列表中的文件压缩的​​语法作斗争。

我试过了:

ls | grep '2020*' | zip archive.zip
ls | zip archive.zip grep '2020*'

我已尝试写入变量,并且已阅读 zip 和 grep 的 Man / --help。 我访问了大约 20 个不同的网站和堆栈页面。

https://unix.stackexchange.com/questions/231239/how-to-redirect-output-of-a-program-to-a-zip-file https://unix.stackexchange.com/questions/231239/how-to-redirect-output-of-a-program-to-a-zip-file https://www.geeksforgeeks.org/zip-command-in-linux-with-examples/ 还有很多。

有人可以帮我理解代码吗? 我在哪里失踪或我在哪里做出错误的假设?

非常感谢。

【问题讨论】:

标签: bash grep zip pipe ls


【解决方案1】:

Don't parse ls

zip archive.zip 2020*

就是你所需要的。

当 bash 解析这一行以供执行时,它会在启动 zip 之前扩展文件模式。

关于正则表达式的区别需要注意的重要一点:

  • 正则表达式2020* 匹配字符串中任意位置的“202”后跟零个或多个“0”
    • 字符串“202”匹配正则表达式2020*
    • 字符串“120201”被正则表达式2020*匹配
  • 全局模式2020* 匹配,从开头开始,“2020”后跟零个或多个任意字符
    • 文件“202”与 glob 模式 2020* 匹配
    • 文件“120201”与 glob 模式 2020* 匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多