From owner-freebsd-questions Thu Jan 9 8:31:11 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 D60E237B401 for ; Thu, 9 Jan 2003 08:31:09 -0800 (PST) Received: from pcwin002.win.tue.nl (pcwin002.win.tue.nl [131.155.71.72]) by mx1.FreeBSD.org (Postfix) with ESMTP id EE3DD43F43 for ; Thu, 9 Jan 2003 08:31:08 -0800 (PST) (envelope-from stijn@pcwin002.win.tue.nl) Received: from pcwin002.win.tue.nl (orb_rules@localhost [127.0.0.1]) by pcwin002.win.tue.nl (8.12.6/8.12.6) with ESMTP id h09GUjJB002631; Thu, 9 Jan 2003 17:30:45 +0100 (CET) (envelope-from stijn@pcwin002.win.tue.nl) Received: (from stijn@localhost) by pcwin002.win.tue.nl (8.12.6/8.12.6/Submit) id h09GUiLm002630; Thu, 9 Jan 2003 17:30:44 +0100 (CET) Date: Thu, 9 Jan 2003 17:30:44 +0100 From: Stijn Hoop To: Rob Cc: "BigBrother (BigB3)" , Duncan Anker , freebsd-questions@freebsd.org Subject: Re: Renaming files with spaces in the name to files without spaces.. Message-ID: <20030109163044.GG938@pcwin002.win.tue.nl> References: <20030108175539.W65616@bigb3server.bbcluster.gr> <200301091519.h09FJIK7000567@goo.0x7e.net> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="V32M1hWVjliPHW+c" Content-Disposition: inline In-Reply-To: <200301091519.h09FJIK7000567@goo.0x7e.net> User-Agent: Mutt/1.4i X-Bright-Idea: Let's abolish HTML mail! 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 --V32M1hWVjliPHW+c Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. >=20 > 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 =3D "/"; o =3D $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 --=20 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. --V32M1hWVjliPHW+c Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+HaO0Y3r/tLQmfWcRArXFAJ9tglUM+mPKXU+RW7grjfJ5Nn2A9gCffWPn HcsZeVca5J85XhZIKjNC3bQ= =goWy -----END PGP SIGNATURE----- --V32M1hWVjliPHW+c-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message