我有以下结构的文件:
Directory1/Subdir N/filename.ext
Directory2/Subdir N/Subdir X/filename.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 '{}' \;
答案 0 :(得分:1)
您可以尝试:
find -name '*.ext' -exec sh -c 'grep -q string "$1" && mv "$1" "${1%.ext}.string.ext"' x '{}' \;
答案 1 :(得分:0)
你想要下面这样的东西。 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
所以你不要破坏任何东西,我在上面的脚本中有echo
而不是mv
,当你认为它会起作用时替换它,并且可能在短的目录树上测试它。< / p>
更新:使用find
更旧的bash
#!/bin/bash
IFS=$'\n'
for file in $(find . -name '*.ext' -type f); do
while read line; do
[[ $line == *test* ]] && echo "$file" "${file%.ext}.string.ext" && break
done < "$file"
done