如果整个文件夹包含文件,则将其移动到新目录中

我有一个目录

test2具有以下内容:

file1
  test.pdb
  xyz.txt
  1.txt

file2
  test.pdb
  xyz.txt

file3
  test.pdb
  xyz.txt 
  1.txt

我想将包含1.txt的文件夹(这里是文件1和文件3)移动到名为“ complete”的新目录中。

所需的输出:

完整

file1
  test.pdb
  xyz.txt
  1.txt

file3
  test.pdb
  xyz.txt 
  1.txt

test2

file2
  test.pdb
  xyz.txt

到目前为止的代码:

find . -maxdepth 2 -name "*ionized.pdb" -print| sed "s|^\./||"
jordandick 回答:如果整个文件夹包含文件,则将其移动到新目录中

假设您正在使用bash,则可以从 test2/的父目录使用此命令:

while IFS= read -rd '' d; do
   [[ -f $d/1.txt ]] && echo mv "$d" complete
done < <(find test2 -type d -print0)

对输出满意后,可以在上述命令中将echo之前的mv删除。

如果不使用bash,请使用以下管道:

find test2 -type d -print0 |
while IFS= read -rd '' d; do [ -f "$d/1.txt" ] && echo mv "$d" complete; done

这假定目录test2complete处于同一级别。

本文链接:https://www.f2er.com/3107506.html

大家都在问