[英]shell script which will move all executable files in the current directory to a seperate folder
我试图编写shell脚本,它将当前目录中的所有可执行文件移动到名为“ executables”的文件夹中。
1 for f in `ls`
2 do
3 if [ -x $f ]
4 then
5 cp -R $f ./executable/
6 fi
7 done
当执行时,它说
cp: cannot copy a directory, 'executable', into itself, './executable/executable'.
因此,如何避免在if条件下检查“ executable”文件夹。 或对此有任何其他完美的解决方案。
ls
的输出。 cp
正在复制, mv
正在移动。 修改脚本:
for f in *; do
if [ -f "$f" ] && [ -x "$f" ]; then
mv "$f" executables/
fi
done
使用GNU find
:
$ find . -maxdepth 1 -type f -perm +a=x -print0 | xargs -0 -I {} mv {} executables/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.