gpt4 book ai didi

linux - 打印文件名列表(文本文件中指定的文件名除外)

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:19 26 4
gpt4 key购买 nike

我有一个文件夹test,其中

file1.txt
file2.wav
file3.txt
file4.py

我在 test 文件夹外有一个包含内容的 dont_include.txt 文件

file1.txt
file4.py

如何打印 test 文件夹中除 dont_include.txt 中列出的文件名之外的所有文件名

期望的输出:

file2.wav
file3.txt

最佳答案

您可以使用 grep:

ls test | grep -xvFf dont_include.txt -
  • -f 表示模式列表取自文件,在本例中为 dont_include.txt
  • -F 表示模式被解释为文字字符串而不是正则表达式,因此 a.txt 不会匹配 abtxt
  • -x 仅匹配整行,即 other_file.txt 不会与 file.txt 匹配
  • -v 表示我们要打印不匹配
  • -(可选)表示我们要过滤的列表是标准输入,即本例中 ls 的输出。

它不适用于名称中包含换行符的文件(但将它们存储在每行一个文件中已经是错误的)。

关于linux - 打印文件名列表(文本文件中指定的文件名除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51328374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com