2

I am using 2to3 to fix my script library, and it seems to be a command line thing, not a shell thing.

I want to do all files from /home/me/scripts downward, assuming they end in .py. Is there an easy way to do 2to3 -y filename for each file under my folder in the shell?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
codyc4321
  • 9,014
  • 22
  • 92
  • 165

2 Answers2

4

There's find command:

 find /home/me/scripts  -iname "*.py" -exec 2to3 {} \;

The -exec argument tell it to execute the command that follows after this argument, which is 2to3 {} in this case. For each file found, {} is replaced by the name of that file.

kfx
  • 8,136
  • 3
  • 28
  • 52
  • 3
    `-exec 2to3 {} +` would be a bit more efficient; it runs `2to3` on as many files as possible at one time, rather than running it once per file. – chepner Feb 05 '16 at 19:19
4

bash 4 provides a way of doing recursive globbing.

shopt -s globstar
2to3 /home/me/scripts/**/*.py
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    This is a good answer, but to get the same effect as the `-iname` option in the accepted answer it is also necessary to use `shopt -s nocaseglob`. – pjh Feb 05 '16 at 19:39
  • 1
    Based on the question alone, I didn't see any need to ignore case. But yes, `nocaseglob` would allow matching `*.PY` (and `*.pY` and `*.Py`) as well as `*.py`. – chepner Feb 05 '16 at 19:47
  • I didn't test yours, I accepted his answer since it worked and I already migrated the files to py3 – codyc4321 Feb 08 '16 at 14:47