Guest xaff Posted July 8, 2003 Report Share Posted July 8, 2003 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? Quote Link to comment Share on other sites More sharing options...
Dyslexic Posted July 8, 2003 Report Share Posted July 8, 2003 This should do the trick. http://www.krename.net/ Quote Link to comment Share on other sites More sharing options...
qnr Posted July 8, 2003 Report Share Posted July 8, 2003 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 Quote Link to comment Share on other sites More sharing options...
aRTee Posted July 8, 2003 Report Share Posted July 8, 2003 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... ;) Quote Link to comment Share on other sites More sharing options...
qnr Posted July 8, 2003 Report Share Posted July 8, 2003 yes, there's rename every once in a while to ensure that you have them all and aren't continuing needlessly, but it's a perfectly good way of doing it. Quote Link to comment Share on other sites More sharing options...
aRTee Posted July 8, 2003 Report Share Posted July 8, 2003 Well, didn't know that rename only takes care of one %20 per filename, but I bet it's still faster to type arrow-up, enter a couple of times than actually do things with a script, if you have to figure out how to do scripting... ;) Quote Link to comment Share on other sites More sharing options...
qnr Posted July 8, 2003 Report Share Posted July 8, 2003 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. Quote Link to comment Share on other sites More sharing options...
Guest xaff Posted July 8, 2003 Report Share Posted July 8, 2003 Thanks a lot qnr. (-: (At times like these, I really love linux) Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted July 9, 2003 Report Share Posted July 9, 2003 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?) Quote Link to comment Share on other sites More sharing options...
qnr Posted July 9, 2003 Report Share Posted July 9, 2003 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: ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.