Jump to content

Search and replace (in filenames)


Guest xaff
 Share

Recommended Posts

Guest xaff

I was just wondering, is there a way to search and replace characters in filenames?

 

Example, I have a file named "Beatles%20-%20Can't%20Buy%20Me%20Love.mpg". Now, you might see the problem, those %20'es are annoying as hell. So, what I need to have replaced is "%20" on a lot of files.

 

Any ideas?

Link to comment
Share on other sites

you could also modify this script I use to rename "Some file or other" to "Some_file_or_other" - should be pretty easy to see what's going on and how to change it:

 

#!/bin/bash

# blank-rename.sh

#

# Substitutes underscores for blanks in all the filenames in a directory.



ONE=1                           # For getting singular/plural right (see below)

number=0                        # Keeps track of how many files actually renamed.

FOUND=0                         # Successful return value.



for filename in *               # Traverse all files in directory

do

       echo "$filename" | grep -q " "                  #  Check whether filename

       if [ $? -eq $FOUND ]                            #+ contains space(s).

       then

         fname=$filename                               #  Strip off path.

         n=`echo $fname | sed -e "s/ /_/g"`            #  Substitute underscore for blank.

         mv "$fname" "$n"                              #  Do the actual renaming.

         let "number += 1"

       fi

done



if [ "$number" -eq "$ONE" ]                             #  For correct grammar.

then

 echo "$number file renamed."

else

 echo "$number files renamed."

fi



exit 0

Link to comment
Share on other sites

Read the manpage for 'rename'

man rename

or in konqueror, put it in the location field:

man:/rename

 

basically it works like this:

rename "string you want to replace" "string to replace it with" [filename to do it on]

 

In you case, you can do

rename "%20" "_" *.mp3

which will change all the %20 in all filenames that end in .mp3 in your current dir into an underscore.

 

To do it on any file, just do:

rename "%20" "_" *

 

and

rename "%20" "_" */*

will do the same on all filenames one directory level deeper.

Just use arrow up and add a /* each time, to do it 5 levels deep in 5 seconds... ;)

Link to comment
Share on other sites

I agree, replace is a perfectly good way of doing it, except that mp3s might have twenty or thirty spaces (%20) in them. As far as writing the script, xaff just had to copy and paste it :) then, with the script, you just modify:

 

n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank.

 

to replace whatever strings you want.

 

Or get more ambitious, and add 2 args so you can specify what to replace anytime you run it. Then save it in your path and chmod +x it, go to the directory where the files are and run it.

Link to comment
Share on other sites

if [ "$number" -eq "$ONE" ]                             #  For correct grammar.

then

 echo "$number file renamed."

else

 echo "$number files renamed."

fi

 

Looks like something I would add into a script like this...LOL.

 

I have a question about this. Would it be a security risk to change the script so that you could pass the "find and replace" replace string into the script?

 

Ex:

./replace-script replacethis withthis

 

and change this:

n=`echo $fname | sed -e "s/ /_/g"`

 

to:

n=`echo $fname | sed -e "s/$1/$2/g"`

 

(possibly you have to assign $1 and $2 with other variable names at the beginning of the script because this line being piped might assign $1 and/or $2 to something else, maybe?)

Link to comment
Share on other sites

I can't see it being too much of a security threat, might want to keep it out of root's $PATH, of course. It's only changing filenames in a directory. I'd be more worried about accidently typing replace_anything with_blank (but at least the report would be grammatically correct :wink: )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...