I'm trying to change the suffix of all files in a directory hierarchy.
Trying to diagnose my problem I have run the following command:-
find info -name '*.txt' -exec echo `basename {} .txt` ;
A section of the output from the above command is as follows:-
info/motorbikes/zzr1200service.txt info/motorbikes/luggage.txt info/motorbikes/index.txt info/motorbikes/restindex.txt info/motorbikes/zzr1200info.txt info/motorbikes/lifts.txt info/index.txt info/test/test1.txt info/test/test2.txt info/test/test3.txt
So the `basename {} .txt` is doing nothing at all, why?
Looking further it seems as if the `` inside the find are causing the problem as:-
find info -name '*.txt' -exec basename {} .txt ;
produces the expected output:-
zzr1200service luggage index restindex zzr1200info lifts index test1 test2 test3
The actual command I want to execute is:-
find info -name '*.txt' -exec mv `basename {} .txt`.rst ;
to change the .txt suffix to .rst.
Any ideas how to do it?
On Fri, Jun 08, 2007 at 12:24:04PM +0100, Chris G wrote:
The actual command I want to execute is:-
find info -name '*.txt' -exec mv `basename {} .txt`.rst \;
to change the .txt suffix to .rst.
Any ideas how to do it?
I think I know the reason for the problem, bash is running the `basename {} .txt` *before* the find runs as it's part of the command parsing. So how do I delay it so that it's run on the output of the find command?
Chris G cl@isbd.net wrote:
find info -name '*.txt' -exec echo `basename {} .txt` \;
I think the backticks are run by the shell before find.
Try:
find info -name '*.txt' | while read filename ; do mv -v "$filename" "${filename/%.txt/.rst}" done
Hope that helps,
On Fri, Jun 08, 2007 at 12:37:22PM +0100, MJ Ray wrote:
Chris G cl@isbd.net wrote:
find info -name '*.txt' -exec echo `basename {} .txt` \;
I think the backticks are run by the shell before find.
Try:
find info -name '*.txt' | while read filename ; do mv -v "$filename" "${filename/%.txt/.rst}" done
In the end I just wrote the output of find to a file and then edited the file to a series of mv commands and ran that.
The issue is definitely that bash parses the `` before the find is run.
On Fri, Jun 08, 2007 at 12:24:04PM +0100, Chris G wrote:
The actual command I want to execute is:-
find info -name '*.txt' -exec mv `basename {} .txt`.rst \;
to change the .txt suffix to .rst.
How about this instead...
find info -name '*.txt' -print0 | xargs -0 rename 's/.txt$/.rst/;'