2016-07-28 52 views
0

我试图编写shell脚本,它将当前目录中的所有可执行文件移动到名为“executables”的文件夹中。shell脚本,将当前目录中的所有可执行文件移动到一个单独的文件夹

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'. 

我尽量避免因此如何检查的,如果条件的“可执行文件”文件夹中。 或有任何其他完美的解决方案。

+0

尝试使用'find'工具这样的事情。它提供了一些文件处理特定功能,而不是基于字符串的操作。例如,您可以将搜索限制为普通文件。 – arkascha

回答

0
  1. 不要分析ls的输出。
  2. 大多数目录都设置了可执行位。
  3. 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/ 
相关问题