lionel28
lionel28

Reputation: 65

Mass renaming files

I've got 25,000 ++ images in a folder on server. Lots of them have %20 in them and that prevents them from displaying. Does anyone know how I could do a command line to str_replace('%20', '_', $imagename) ?

Thanks

Upvotes: 1

Views: 1003

Answers (3)

ricardohg
ricardohg

Reputation: 39

this might work http://snipplr.com/view/2736/

(code provided here for future reference)

for i in *.avi
do
  j=`echo $i | sed 's/find/replace/g'`
  mv "$i" "$j"
done

Can also be written on a single line as

for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done

Upvotes: 3

favoretti
favoretti

Reputation: 30167

This small python snippet can help you probably:

import os

for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        if "%20" in name:
            os.rename(os.path.join(root, name), os.path.join(root, name.replace("%20", "_")))
            print("renamed: %s" % name)

Note the "." argument to os.walk. Either change it to the path to the directory where files reside, or otherwise run the script from that directory.

Upvotes: 0

mnain
mnain

Reputation: 46

One way to do this is get a list of files in a file i.e. using 'ls -1 | awk '{print "mv $1 $1"}' > torename.sh', then editing the torename.sh using regular expressions. Once you've got the script to what you want, run the script.

Upvotes: 0

Related Questions