Click to See Complete Forum and Search --> : Bulk renaming of files
glenmark
11-22-2000, 06:15 PM
How does one go about changing the suffix of a large number of files? When I try the move command, it complains that the target must be a directory. For example:
$ mv *.php *.php3
mv: when moving multiple files, last argument must be a directory
On a slightly related note, what is the simplest way of performing a find/replace operation across multiple files? (i.e. replace all instances of string "hello" in *.html)
Thanks in advance for any pointers...
glenmark
11-22-2000, 07:24 PM
Found an answer to the second part of my own question at http://linux.about.com/compute/linux/library/weekly/aa042999q-a.htm
there is no one-liner command to rename multiple files. you need to create a shell script that gets all the file names ending in <suffix> (use 'ls' with 'grep') then renames each filename to <whatever> (use 'mv' in a 'for' loop). sorry i haven't done any scripting in years so i can't provide you with the actual code. but i think the operation you want is often used as an example in scripting tutorials.
mastersibn
11-23-2000, 06:15 AM
Originally posted by kid:
there is no one-liner command to rename multiple files. you need to create a shell script that gets all the file names ending in <suffix> (use 'ls' with 'grep') then renames each filename to <whatever> (use 'mv' in a 'for' loop). sorry i haven't done any scripting in years so i can't provide you with the actual code. but i think the operation you want is often used as an example in scripting tutorials.
There is too a simple one line command that will do the trick:
for i in `ls | grep .php | grep -v .php3`; do mv $i `echo $i`3; done
For those of you who don't understand at a glance (and really do want to understand) the for loop checks all the files in . that contain .php that do NOT contain .php3. Then it moves that particular file to its own name appending a 3 to the end of it. http://www.linuxnewbie.org/ubb/biggrin.gif
You can (and I do) use this for any kind of file, with little modification. People make all these complex perl scripts to do the simplest tasks... sheesh! http://www.linuxnewbie.org/ubb/biggrin.gif http://www.linuxnewbie.org/ubb/wink.gif
<EDIT>Oh, you meant one-liner as in 'simple command not needing pipes or other fun things.' It's not all that complicated though... it's simple enough that I type it on the rare occasions that I need it, rather than save it as a shell script. I use a different variation every time, but a shell script wouldn't be hard to do either. http://www.linuxnewbie.org/ubb/tongue.gif</EDIT>
------------------
grab my gnupg key (http://jove.prohosting.com/~msibn/sibn-p.asc) if you feel so inclined.
cAPS lOCK? wHAT cAPS lOCK?
I cna ytpe 300 wrods pre mniuet!!!
[This message has been edited by mastersibn (edited 23 November 2000).]