From owner-freebsd-questions Thu Jan 9 7:41:44 2003 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9B27437B405 for ; Thu, 9 Jan 2003 07:41:40 -0800 (PST) Received: from mail.duth.gr (mail.duth.gr [192.108.114.110]) by mx1.FreeBSD.org (Postfix) with ESMTP id 398E443F5B for ; Thu, 9 Jan 2003 07:41:39 -0800 (PST) (envelope-from bigbrother@bonbon.net) Received: from bigb3server.bbcluster.gr (b9-19.xan.duth.gr [193.92.211.19]) by mail.duth.gr (8.12.6/8.12.6) with ESMTP id h09FfTBd011696 (version=TLSv1/SSLv3 cipher=EDH-DSS-DES-CBC3-SHA bits=168 verify=NO); Thu, 9 Jan 2003 17:41:30 +0200 (EET) (envelope-from bigbrother@bonbon.net) Received: from bigb3server.bbcluster.gr (localhost.bbcluster.gr [127.0.0.1]) by bigb3server.bbcluster.gr (8.12.6/8.12.6) with ESMTP id h09FfTPh011144; Thu, 9 Jan 2003 17:41:29 +0200 (EET) (envelope-from bigbrother@bonbon.net) Received: from localhost (bigbrother@localhost) by bigb3server.bbcluster.gr (8.12.6/8.12.6/Submit) with ESMTP id h09FfTv5011141; Thu, 9 Jan 2003 17:41:29 +0200 (EET) X-Authentication-Warning: bigb3server.bbcluster.gr: bigbrother owned process doing -bs Date: Thu, 9 Jan 2003 17:41:26 +0200 (EET) From: "BigBrother (BigB3)" X-X-Sender: bigbrother@bigb3server.bbcluster.gr To: Rob Cc: freebsd-questions@freebsd.org Subject: Re: Renaming files with spaces in the name to files without spaces.. In-Reply-To: <200301091519.h09FJIK7000567@goo.0x7e.net> Message-ID: <20030109173758.H65616@bigb3server.bbcluster.gr> References: <1042068862.1441.3.camel@duncan.au.darkbluesea.com>, <20030108175539.W65616@bigb3server.bbcluster.gr> <200301091519.h09FJIK7000567@goo.0x7e.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG -----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 majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message