LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2008, 03:55 AM   #1
jeffers
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Rep: Reputation: 0
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.
 
Old 04-17-2008, 04:03 AM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
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 ...
 
Old 04-17-2008, 04:31 AM   #3
jeffers
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks - will try it out now (with backup of course ;-)
 
Old 04-17-2008, 05:44 AM   #4
jeffers
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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???

Last edited by jeffers; 04-17-2008 at 05:47 AM.
 
Old 04-17-2008, 06:03 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.

Last edited by colucix; 04-17-2008 at 06:04 AM.
 
Old 04-17-2008, 06:07 AM   #6
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
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)

Last edited by blacky_5251; 04-17-2008 at 06:08 AM.
 
Old 04-17-2008, 06:30 AM   #7
jeffers
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Original Poster
Rep: Reputation: 0
AND THE WINNER IS! (fanfare)

Quote:
Originally Posted by colucix View Post
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.

Last edited by jeffers; 04-17-2008 at 06:34 AM.
 
Old 05-27-2008, 09:01 AM   #8
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
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
 
Old 05-27-2008, 07:05 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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 ...)
 
  


Reply

Tags
filenames


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text transformation Sabinou Linux - General 6 04-15-2007 12:01 PM
Transformation Matrix Senatla Programming 1 03-26-2007 07:17 PM
LXer: Ubuntu begins its transformation LXer Syndicated Linux News 0 12-04-2006 02:21 AM
XSLT transformation of XML document using XMLNS Foomajick Programming 7 10-25-2006 05:27 AM
Windows to Linux transformation... salik Linux - Networking 3 11-27-2005 06:56 AM


All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration