LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bulk transformation of filenames (https://www.linuxquestions.org/questions/linux-newbie-8/bulk-transformation-of-filenames-635923/)

jeffers 04-17-2008 03:55 AM

bulk transformation of filenames
 
Hi all

RHEL 4

I have a folder in which there are hundreds of files that have been created with %20 in them. Is there any way of doing a bulk transformation on those names to replace them with spaces or underscores?

Thanks.

bhaslinux 04-17-2008 04:03 AM

find . -type f >list

for f in `cat list`
do
fname=`echo $f | sed s/%20/\ /g`
mv $f "$fname"
done


Warning: Backup the data before doing this ...

jeffers 04-17-2008 04:31 AM

Thanks - will try it out now (with backup of course ;-)

jeffers 04-17-2008 05:44 AM

Unfortunately I just get the following error


mv: cannot move `jeff%20file%20test.txt' to `echo $f | sed -e s/%20/\\ /g': No such file or directory

(the double backslash at %20/\\ only appears in this output it is not in the original command)

it thinks the sed command is the destination string - perhaps it needs an exec???

colucix 04-17-2008 06:03 AM

Really strange. You can try with exec - or better eval. Or you can do a simple string replacement like this
Code:

mv $file "${file//\%20/ }"
Anyway, I strongly suggest to replace with an underscore instead of a blank space. This will avoid some other problems when managing the renamed files.

blacky_5251 04-17-2008 06:07 AM

I think you used the wrong character in the line reading
Code:

fname=`echo $f | sed s/%20/\ /g`
The single quote used here must be from the key next to the "1" at the top right hand corner of the US keyboard layout - NOT the single quote found underneath the double quote. Another way to express the same command, without the potential for this error, is:-
Code:

fname=$(echo $f | sed s/%20/\ /g)

jeffers 04-17-2008 06:30 AM

AND THE WINNER IS! (fanfare)

Quote:

Originally Posted by colucix (Post 3124006)
Code:

mv $file "${file//\%20/ }"


Works like a dream.

Thanks to everyone who took the time to help me. Thanks to Blacky for pointing out the possible confusion between us and uk keyboard layouts.

Jeff.

kinetik 05-27-2008 09:01 AM

Tried to do something like this, and going by the contributions made in this thread, got the following done:

Code:

ls -l | awk {'print $9'} | grep '%20' > list

for f in `cat list`
do
fname=`echo $f | sed 's/\%20/ /g'`
echo $fname
mv $f "$fname"
done


Works like a charm, also will display whatever it's renamed. So even though I wasn't the one that asked this question, thanks for the codes :)

chrism01 05-27-2008 07:05 PM

Just to reinforce colucix' comment, do replace with underscores, not spaces, as many Unix cmds/tools do not behave well with spaces in filenames (default design is that args to cmds are space separated ...)


All times are GMT -5. The time now is 08:01 AM.