For many years I have used a home grown tool that allows me to make simple changes to multiple files with one command. It is called change and has existed as csh, bash and perl incarnations over the years. I recently rewrote it in python and thought that it might be useful to others so I am publishing it. Here is an example of how it works:
1 2 3 4 |
#!/bin/bash files=$(find . -type f) change.py 'Copyright (c) 2010' 'Copyright (c) 2012' $files change.py 'Version 1.0' 'Version 1.2' $files |
I suspect that lots of you have written your own, similar tools. If that is the case, please look it over send me suggestions for improvements.
You can download it here: http://projects.joelinoff.com/change-1.0/change.py.
Make sure that you call it using the python command or edit the first line to correctly access your local version of python.
Have fun!
Here is the on-line help (-h or –help). It explains things in a bit more detail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
% python ./change.py -h USAGE change.py [OPTIONS] DESCRIPTION This a tool that allows you to conveniently change text in a group of files using regular expressions. It will also change file names. You specify the old pattern, the new pattern and the list of files. The patterns are in python regular expression format which makes it easy to change text that contains things like forward slashes. Here is a detailed example thats shows how to change foo to bar. % ls -1 foo.txt foo-bar.txt % cat foo.txt foo foo % cat foo-bar.txt foo foo % change.py -s foo bar foo.txt foo-bar.txt Total Files Analyzed : 2 Files Changed : 2 Files With Content Changes : 2 Files With Name Changes : 2 Total Changes : 4 Elapsed Time : 1.00 msecs Time Per File : 0.50 msecs % ls -1 bar.txt bar-bar.txt If you want to substitute characters that used in regular expressions like '.' you must escape it with a backslash '\.'. Here is an example: % # Change all 3 character strings that start with 'fo'. % change.py 'fo.' 'bar' * % # Only change strings that have 'fo' and a period: 'fo.'. % change.py 'fo\.' 'bar' * If you only want to change a work use the '\b' directive as follows: % # Change 'foo' to 'bar' but leave 'foobar' alone. % change.py '\bfoo\b' 'bar' * OPTIONS -C, --keep-contents Don't change file contents. By default file contents change. -d <dir>, --dir</dir> <dir>Output directory. By default the files are changed</dir> <dir>in place. If this option is specified, then changed</dir> <dir>files appear in <dir>/<xfn> where xfn is the file</dir> <dir>name with the unique portion of directory for all</dir> <dir>file names in the list prepended. This reduces the</dir> <dir>depth of the output direction. This is useful for</dir> <dir>debugging.</dir>-h, --help Display this usage message. -n, --noop No change. It only tells you what would be changed. -N, --keep-names Don't change file names. By default file names change. -s, --stats Print out run stats. -v, --verbose Increase the level of verbosity. This can be specified multiple times. -V, --version Print the program version and exit. EXAMPLES % # Help % change.py -h % # Change foo to bar for the C++ files in this directory. % change.py -s 'foo' 'bar' *.cc *.h % # Change old to new for all Python files in this directory % # tree. % change.py -s 'old' 'new' `find . -type f -name '*.py` % # Change old to new for all Python files but leave the % # file names unchanged. % change.py -s -N 'old' 'new' `find . -type f -name '*.py` AUTHOR Joe Linoff VERSION Version 1.0 LICENSE This for this change tool. Copyright (c) 2011 by Joe Linoff The change tool is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The change tool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the change tool; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
Help ! Maybe ?
I scanned a bunch of pictures into different directories (by year). The file name in each directory came up as “scan001.jpg, scan002.jpg etc. I now realize if I want to move files from 1 directory to another, I will have to rename every one.
I don’t know what “python” is. I am running on Vista 2006 Home”.
Do you know a way to change just the first 4 chacters of the file name (to the year) but retain the number portion of the name. (example: scan001 would become 1963001, 0r scan001 in a diferent directory would become 1985001.
any assist is greatly appreciated !!
THANKS,
> I don’t know what “python” is.
Python is a scripting language like C#, perl or ruby. It is free and open-source (like perl and ruby) so it won’t cost you anything to download and use.
The change tool can definitely do what you want. You simply have to run it as follows:
> cd
> python change.py –keep-contents ‘scan’ ‘1963’ *.jpg
Unfortunately, to make it work you have to download and install python on your windows machine.
If you want to do that navigate to this page: http://www.python.org/download/releases/2.7.2/ and select the Windows MSI (MicroSoft Installer) installer. Once it is installed, you can run change.py from a DOS window as shown above.
If you do not wish to use python, you may want to look at learning PowerShell programming or look for a windows tool that will do what you want. I am not that familiar with those sorts of tools since I do most everything from the command line.
Good luck.