$cat file
/tmp/foldera/folderb/folderc/file1
/tmp/folderc/folderd/foldere/file2
/tmp/folderf/folderg/folderh/file3
需要将此文件移动到
/tmp/foldera/
/tmp/folderc/
/tmp/folderf/
分别使用循环或任何简单方便的方法来做到这一点.
解决方法:
使用while循环:
while IFS= read -r l; do
mv -v -- "$l" "${l%/*/*/*}/"
done <file
> IFS = read -r l将逐行读取文件.
> mv -v移动文件,-v告诉mv是详细的
>“$l”是源文件名
>“${l%/ * / * / *} /”是目标目录.它从文件名的后端删除了3个斜杠以及它们之间的内容(/ * / * / *).
这将产生:
»/tmp/foldera/folderb/folderc/file1“ -> »/tmp/foldera/file1“
»/tmp/folderc/folderd/foldere/file2“ -> »/tmp/folderc/file2“
»/tmp/folderf/folderg/folderh/file3“ -> »/tmp/folderf/file3“