简体   繁体   中英

shell script which will move all executable files in the current directory to a seperate folder

I trying to write shell script which will move all executable files in the current directory to a folder called "executables".

  1   for f in `ls`
  2    do
  3     if [ -x $f ]
  4      then
  5       cp -R $f ./executable/
  6     fi
  7    done

when executed ,it says

cp: cannot copy a directory, 'executable', into itself, './executable/executable'.

so how i avoid checking the 'executable' folder in the if condition. or there is any other perfect solution for this.

  1. Don't parse the output of ls .
  2. Most directories have the executable bit set.
  3. cp is copying, mv is moving.

Adapting your script:

for f in *; do
  if [ -f "$f" ] && [ -x "$f" ]; then
    mv "$f" executables/
  fi
done

With GNU find :

$ find . -maxdepth 1 -type f -perm +a=x -print0 | xargs -0 -I {} mv {} executables/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM