From owner-freebsd-questions@FreeBSD.ORG Fri Jan 28 06:41:04 2005 Return-Path: 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 584BA16A4CE for ; Fri, 28 Jan 2005 06:41:04 +0000 (GMT) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 69F7E43D1F for ; Fri, 28 Jan 2005 06:41:03 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from gothmog.gr (patr530-a155.otenet.gr [212.205.215.155]) j0S6f1E1015681 for ; Fri, 28 Jan 2005 08:41:01 +0200 Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.13.1/8.13.1) with ESMTP id j0S6evlE001627 for ; Fri, 28 Jan 2005 08:40:57 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.gr (8.13.1/8.13.1/Submit) id j0S6evxY001626 for freebsd-questions@freebsd.org; Fri, 28 Jan 2005 08:40:57 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Date: Fri, 28 Jan 2005 08:40:57 +0200 From: Giorgos Keramidas To: freebsd-questions@freebsd.org Message-ID: <20050128064057.GB1559@gothmog.gr> References: <1098984237.20050128065616@wanadoo.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1098984237.20050128065616@wanadoo.fr> Subject: Re: One-line global string replace in all files with sed (or awk?) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Jan 2005 06:41:04 -0000 On 2005-01-28 06:56, Anthony Atkielski wrote: > My thanks to all who replied. I ended up using this form (I don't > recall who suggested it): > > find . -type f | xargs sed -i '' -e 's/foo/bar/g' > > One problem, though: It appears that sed touches every file, resetting > the last modification time, even if it didn't actually change anything. Indeed. > This reset the last modification dates for every file on my site, which > wasn't much fun. Oops. Sorry about that :-/ > Is there another command I could put between find and xargs that would > filter only the names of files containing the string? (grep would do > it, but grep outputs the lines found to stdout, so that won't do.) grep will do. You just have to pass it the right option: find . -type f | xargs grep -l 'foo' | \ xargs sed -i '' -e 's/foo/bar/g' When passed the -l option (this is a lowercase 'EL'), it will not print the matched lines. Only the name of the files that *do* match. Then, once you have a list of files that really do match with 'foo' as a pattern, you can xargs sed on the list to substitute whatever you want :-)