0

I am trying to clean up my directories and would like to move all executable files to a subdirectory. I know 'find -executable' will find all the files and I know I'm suppose to use mv to move them but I can't really pipe from find into mv?

Is there a way to do this instead of me typing so many exec files one by one into mv.

I also can not use * as they are all named different and don't have mutual extension. It's easy to do that for all .c or .py files but with exec files I'm having hard time figuring it out.

Mathemats
  • 1,185
  • 4
  • 22
  • 35
Julee
  • 43
  • 1
  • 3

1 Answers1

3

Try this: find search_dir -executable -exec mv {} target_dir \;

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
kristaps
  • 1,705
  • 11
  • 15
  • 1
    This is the right approach, but note that it is recursive and if `search_dir/a/file` and `search_dir/b/file` both exist, one of them will wind up be overwritten in `target_dir`. – William Pursell Jun 28 '17 at 00:32
  • something weird happened. it moved some executable files that were not in the search_dir – Julee Jun 28 '17 at 00:40
  • yes it moved exec files from subdirectory of the search_dir too – Julee Jun 28 '17 at 00:42
  • If you don't want recursion then perhaps this will help: https://stackoverflow.com/questions/3925337/find-without-recursion – kristaps Jun 28 '17 at 00:44
  • It should be noted that `-executable` will also match directories that the user has "execute" permissions for. You may want to add `-type f` to only consider files. Also, I don't know how tidy OP's directories are, but I encounter a lot of non-executable files that for whatever reason have the executable bit enabled. Point being, there's no magic that `find` is using to determine if something is an executable. It's just looking at permissions. So `touch foo; chmod +x foo` will create a file that `find` considers executable. Still, this is most likely a good enough solution. – Mike Holt Jun 28 '17 at 00:55
  • thx that worked!! I just added -type f so it doesn't move subdirectories too. So it looks like this: `find serch_dir -maxdepth 1 -executable -type f -exec mv {} target_dir/ \;` – Julee Jun 28 '17 at 00:58