Re: Renaming files with spaces in the name to files without spaces..
Sorry for this OT but I am trying for some hours to achieve a massive rename of files using a simple script and I have not success yet. I want to rename files like RESULTS OF JAN 01 2002.txt to RESULTS_OF_JAN_01_2002.txt i.e. all the spaces, being substituted by '_', and the last space being completely removed [yes it has a space after the suffix] I tried to experiment with sed/awk and creating a sample sh script with for i in 'ls' but the i takes values of 'RESULTS' 'OF' 'JAN'. This means that it doesnt take the full filename as value, but parts of the filenames. Can u please suggest an easy way to implement the massive rename? If you want to do it for all files in a directory: # for file in *; do mv $file `echo $file | sed -e 's/ /_/g'`; done should do the trick. I think Perl is overkill for something this simple. Someone else suggested tr, which probably works, but I've had more success with sed. But if you do this, won't the spaces be mistaken for filename separators? Try this instead - make sure you're using sh, not csh: ls *\ * | while read OLD ; do NEW=`echo $OLD | tr ' ' _` echo mv -i $OLD $NEW done This works because ls prints them on separate lines. Once you're sure that it will do the right thing, take out the echo and run it for real. If the files are all over the place, you can use find the same way: find * -name '* *' -type f | while read OLD ; do NEW=`echo $OLD | tr ' ' _` echo mv -i $OLD $NEW done You'll have to fix the directories separately (otherwise find gets lost). To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Renaming files with spaces in the name to files without spaces..
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Fri, 10 Jan 2003, Rob wrote: Sorry for this OT but I am trying for some hours to achieve a massive rename of files using a simple script and I have not success yet. I want to rename files like RESULTS OF JAN 01 2002.txt to RESULTS_OF_JAN_01_2002.txt i.e. all the spaces, being substituted by '_', and the last space being completely removed [yes it has a space after the suffix] I tried to experiment with sed/awk and creating a sample sh script with for i in 'ls' but the i takes values of 'RESULTS' 'OF' 'JAN'. This means that it doesnt take the full filename as value, but parts of the filenames. Can u please suggest an easy way to implement the massive rename? If you want to do it for all files in a directory: # for file in *; do mv $file `echo $file | sed -e 's/ /_/g'`; done should do the trick. I think Perl is overkill for something this simple. Someone else suggested tr, which probably works, but I've had more success with sed. But if you do this, won't the spaces be mistaken for filename separators? Try this instead - make sure you're using sh, not csh: ls *\ * | while read OLD ; do NEW=`echo $OLD | tr ' ' _` echo mv -i $OLD $NEW done This works because ls prints them on separate lines. Once you're sure that it will do the right thing, take out the echo and run it for real. If the files are all over the place, you can use find the same way: find * -name '* *' -type f | while read OLD ; do NEW=`echo $OLD | tr ' ' _` echo mv -i $OLD $NEW done You'll have to fix the directories separately (otherwise find gets lost). Thank you all for your quick reply. I followed Rob's way and it was fairly easy to do. I had to change a bit something but it worked. The rename script that I used is: - --cut here-- #!/bin/sh ls *\ * | while read OLD ; do NEW=`echo $OLD | tr ' ' _` mv -i $OLD $NEW done - -cut here-- As u notice I had to add the semicolon in the $OLD variable because otherwise the mv was complaining. So this was a nice and fast way to do it. Thank you all people for your quick reply!! BigBrother - --- We are being monitored..but there is a solution... Use PGP for signing and encrypting emails Download my public key at http://www.us.pgp.net -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+HZgpGe/V3CxAyHoRAnYRAJ9qGvtXc7cA7bdGJAbmRGNbyrHW9ACeLN95 1+0+V1Q76jtCW1jbVMdZZQA= =8IWO -END PGP SIGNATURE- To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Renaming files with spaces in the name to files without spaces..
On Fri, Jan 10, 2003 at 01:49:18AM +1030, Rob wrote: If you want to do it for all files in a directory: # for file in *; do mv $file `echo $file | sed -e 's/ /_/g'`; done should do the trick. I think Perl is overkill for something this simple. Someone else suggested tr, which probably works, but I've had more success with sed. But if you do this, won't the spaces be mistaken for filename separators? No, he has quotes around his $file, and the `` part replaces spaces, so this should work. Witness: $ touch a b c $ for i in *; do echo arg $i endarg; done arg a b endarg arg c endarg Another solution is using a find awk combo, which makes it work recursively (ie one commandline instead of one per directory): $ find -d . -name '* *' | awk -F/ '{ OFS = /; o = $0; gsub( , _, $NF); print mv \ o \ \ $0 \ }' | sh This can probably be written in a shorter way by awk gurus, I'm just trying to learn it and this was a good exercise, which is the reason I'm replying to this post :) HTH, --Stijn -- The rain it raineth on the just And also on the unjust fella, But chiefly on the just, because The unjust steals the just's umbrella. msg14837/pgp0.pgp Description: PGP signature
Re: Renaming files with spaces in the name to files without spaces..
Conider the file freebsd help.doc mv freebsd\ help.doc freebsdhelp.doc - Original Message - From: Gary W. Swearingen [EMAIL PROTECTED] To: BigBrother (BigB3) [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Wednesday, January 08, 2003 10:05 PM Subject: Re: Renaming files with spaces in the name to files without spaces.. Paste this into your shell: XXX=RESULTS OF JAN 01 2002.txt YYY=${XXX%% } ZZZ=$(echo ${YYY} | tr ' ' '_') for III in $XXX $YYY $ZZZ; do echo '$III' done To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Renaming files with spaces in the name to files without spaces..
in message [EMAIL PROTECTED], wrote Stijn Hoop thusly... On Fri, Jan 10, 2003 at 01:49:18AM +1030, Rob wrote: If you want to do it for all files in a directory: # for file in *; do mv $file `echo $file | sed -e 's/ /_/g'`; done ^ ^ ^ ^ But if you do this, won't the spaces be mistaken for filename separators? No, he has quotes around his $file, and the `` part replaces spaces, so this should work. notice that $file is not enclosed in the sub shell (in between ``) as an argument to echo. if the $file happens to have end blanks, they will be eaten up. try... f=' p q r '; echo $( echo $f | sed 's/ /_/g' ) ...output /should/ have been '_p_q_r_', but is 'p_q_r'. $ touch a b c $ for i in *; do echo arg $i endarg; done ^ ^ ^ ^ arg a b endarg arg c endarg notice that you have enclosed the string to echo in double quotes, which preserves the spaces. - parv -- To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Renaming files with spaces in the name to files without spaces..
in message [EMAIL PROTECTED], wrote BigBrother (BigB3) thusly... Sorry for this OT but I am trying for some hours to achieve a massive rename of files using a simple script and I have not success yet. I want to rename files like RESULTS OF JAN 01 2002.txt to RESULTS_OF_JAN_01_2002.txt here is another way in perl (though it changes blanks to '-'; edit as you desire)... http://www103.pair.com/parv/comp/src/perl/sanename.perl description... http://www103.pair.com/parv/comp/src/perl/sanename.perl.pod - parv -- To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Renaming files with spaces in the name to files without spaces..
On Wed, Jan 08, 2003 at 06:01:50PM +0200, BigBrother (BigB3) said: -BEGIN PGP SIGNED MESSAGE- Sorry for this OT but I am trying for some hours to achieve a massive rename of files using a simple script and I have not success yet. I want to rename files like there is already a general utility for this: /usr/src/contrib/perl5/eg/rename . leviathan:/home/chris:1168 /usr/src/contrib/perl5/eg/rename Usage: rename perlexpr [filenames] not only already written and tested, but you get to use perl regexen. :-) HTH, Chris --- Chris Doherty chris [at] randomcamel.net I think, said Christopher Robin, that we ought to eat all our provisions now, so we won't have so much to carry. -- A. A. Milne --- To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message