Kev 的方式更好,但这也可以:
find "/home/folder" -maxdepth 1 | sed -e "/^\/home\/folder$/d" -e "/^\/home\/folder\/subfolder5$/d" -e "/^\/home\/folder\/subfolder7$/d" -e "s/^/cp \-r /" -e "s/$/ \/home\/target/" | cat
解释:
find "/home/folder" -maxdepth 1 |
// get all files and dirs under /home/folder, pipe output
sed -e "/^\/home\/folder$/d"
// have sed strip the path being searched, or the cp -r we prepend later will pickup the excluded dirs again.
-e "/^\/home\/folder\/subfolder5$/d"
// have sed strip subfolder5
-e "/^\/home\/folder\/subfolder7$/d"
// have sed strip subfolder7
-e "s/^/cp \-r /"
// have sed prepend "cp -r " to each line
-e "s/$/ \/home\/target/" | cat
// have sed append targetdir to each line.
输出:
cp -r /home/folder/subfolder9 /home/target
cp -r /home/folder/subfolder1 /home/target
cp -r /home/folder/file10 /home/target
cp -r /home/folder/subfolder2 /home/target
cp -r /home/folder/file1 /home/target
cp -r /home/folder/subfolder3 /home/target
更改| cat
为| sh
执行命令。
<这里有一个重要的免责声明>
你应该 Kev 的解决方案更好