2016-07-28 121 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/ 
相關問題