Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果内容包含特定的单词,如何向文件名递归添加字符串?_Bash - Fatal编程技术网

Bash 如果内容包含特定的单词,如何向文件名递归添加字符串?

Bash 如果内容包含特定的单词,如何向文件名递归添加字符串?,bash,Bash,我有一个具有以下结构的文件: Directory1/Subdir N/filename.ext Directory2/Subdir N/Subdir X/filename.ext 我怎样才能: 查找扩展名为“.ext”的所有文件 在文件内容中查找“字符串”,如果找到“字符串”: 将文件重命名为原始文件名,但使用filename.string.ext 我的目标是在一堆txt文件中搜索几个瑞典语单词,如果找到该单词,请将该文件重命名为filename.sv.ext-如果找不到,请将其重命名为fil

我有一个具有以下结构的文件:

Directory1/Subdir N/filename.ext
Directory2/Subdir N/Subdir X/filename.ext
我怎样才能:

  • 查找扩展名为“.ext”的所有文件
  • 在文件内容中查找“字符串”,如果找到“字符串”:
  • 将文件重命名为原始文件名,但使用filename.string.ext
  • 我的目标是在一堆txt文件中搜索几个瑞典语单词,如果找到该单词,请将该文件重命名为filename.sv.ext-如果找不到,请将其重命名为filename.en.ext

    非常感谢您的帮助

    更新

    下一行匹配一组英文单词,如果文件包含其中任何一个,请将其重命名为filename.en.srt。它不接触已命名为filename.en.srt或filename.sv.srt的文件

    # Rename English subtitles to filename.en.srt
    find . ! -name '*.sv.srt' ! -name '*.en.srt' -name '*.srt' -exec sh -c 'grep -i -q -e yes -e maybe -e right -e left -e friend -e call -e leave -e stupid -e while -e dark -e fool -e mercy -e emotion -e find -e morning -e subtitles -e picture -e say -e nothing -e always -e people -e heart "$1" && mv "$1" "${1%.srt}.en.srt"' x '{}' \;
    
    
    # Rename Swedish subtitles to filename.sv.srt
    find . ! -name '*.sv.srt' ! -name '*.en.srt' -name '*.srt' -exec sh -c 'grep -i -q -e undertexter -e komma -e allt -e kollega -e arbeta -e arbete -e morgon -e lycklig -e kanske -e lugn -e tycker -e liksom -e okej -e orkar -e telefon -e historia -e ingen -e beredd -e kunna -e trodde -e tror "$1" && mv "$1"  ${1%.srt}.sv.srt"' x '{}' \;
    

    你想要下面这样的东西。Pseudocodish,不知道您想要什么字符串/如何告诉文件语言,但应该可以进行一些编辑

    #!/bin/bash
    
    shopt -s globstar
    
    #globstar is essentially the recursive version of *".ext", which gets all .ext in this dir
    #can alternatively use find if you have an old shell version
    #also note this descends the same way as `find .`, but you can change it to 
    #"/home/somedir/"**/*".ext" or something
    for file in **/*".ext"; do 
    
      #check if the file is a regular file (not symlink, dir)
      if [[ -f $file ]]; then
    
        #read in the file line by line (might be a faster way to do this)
        while read line; do
    
          #replace somestring, with whatever string you want to check for
          #this checks if it exists on the current line, mvs the file, and breaks the loop
          [[ $line == *somestring* ]] && echo "$file" "${file%.ext}.string.ext" && break
    
        done < "$file"
      fi
    done
    
    您可以尝试:

    find -name '*.ext'  -exec sh -c 'grep -q string "$1" && mv "$1" "${1%.ext}.string.ext"' x '{}' \;
    

    欢迎来到StackOverflow。你的问题并不难。你已经试过解决它了吗?怎么用?你能给我们看看你的密码吗?有什么问题吗?谢谢你!虽然我还不能测试您的解决方案,但我非常感谢您的努力。事实证明,我的Synology DS411j只运行Bash3.2,它不支持globstar参数(在Bash4中引入)。我将在VM中启动GNU/Linux安装并运行它,只要我安装了VMware或其他东西:-)谢谢@mescon没问题,我更新了我的答案,以防你好奇
    find
    的for循环会是什么样子,尽管只要我们使用
    find
    可能就只需要一行,所以
    Håkon Hægland
    的答案可能是最适合你的。谢谢更新!我最终接受了Håkon的建议。。。下一行重命名包含任何单词的文件。我有一个类似的例子,如果它包含一堆特定的瑞典语单词中的任何一个,它将重命名为.sv.srt。我的丛现在可以识别语言:)查找-名称'.sv.srt'-name'.en.srt'-name'*.srt'-exec sh-c'grep-i-q-e yes-e maybe-e right-e left-e friend-e call-e leave-e dumby-e while-e dark-e fool-e mercy-e emotion-e find-e morning-e字幕-e picture-e say-e nothing-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-everys-e-e-e-e-e-e-e-e-e-e-e-e-heart“$1”${.sr;工作得很有魅力!非常感谢你!!海佳挪威语!:-)+一行1,grep
    。出于某种原因,我希望使用
    break
    读取循环,如果发现它比
    grep
    快,但是现在做一个快速测试,我认为它要慢一点,他们如何优化它会很有趣。
    find -name '*.ext'  -exec sh -c 'grep -q string "$1" && mv "$1" "${1%.ext}.string.ext"' x '{}' \;