![]() |
formatting find output
I was wondering what the best way to format a search would be through the console. I'm using cygwin because we use MS XP, and I would like to get a comprehensive list of certain files (*.xml) in two seperate dirs.
So far I have this: find ./ -name *.xml | sort > xmls.txt But there are a couple things wrong with the format. One is that they include the filepath when I only want the filename. The other is, for some reason that makes no sense to me, it does not put newlines after every file. I figure there is probably some way to do this using grep, but I must confess I don't know much about that at all. Thanks for your help! |
Hi,
This will remove the directory: find ./ -name *.xml -exec basename {} \; | sort > xmls.txt BTW: You cannot make the distinction anymore which file goes where if they have the same name. I cannot reproduce the lack of newlines so this is untested: Try adding -print to the find statement. I.e.: find ./ -name *.xml -exec basename {} \; -print | sort > xmls.txt Hope this helps. |
Awesome, that was exactly what I wanted. And the -print worked perfectly. Just to clarify, does the -exec basename {} extract the filename from the path? That's what I'm inferring from the argument and the result.
|
Quote:
"%f" should provide only filename and "\n" a newline. |
Hi,
Quote:
BTW: The problem (it does not put newlines after every file) could be related to \n vs \r\n (linux vs windows newline). |
Hi,
Quote:
The -exec <command> {} \; structure is part of find. You can substitute <command> with just about any command, the {} part is where find inserts what it has found so the command can do its thing with it. Have a look here (A Unix/Linux "find" Command Tutorial) for more examples and some explanations. Hope this clears things up. |
Thanks guys, you helped a lot. I'll take a look at that. Just curious, but would it be possible to use, instead of printf > filename, fprintf(filename,'%f\n') or something of the like? Perhaps not as efficient but I'm just curious if terminal supports fprintf.
I appreciate your quick replies! |
Hi,
-fprintf is part of the find command and I just found this (I've never played with this option myself): find ./ -fprintf outfile "%f\n", which will put the filnames found into a file called outfile. Rewriting your command (which now looses the sort part): find ./ -iname "*.xml" -fprintf xmls.txt '%f\n'; Hope this helps. |
| All times are GMT -5. The time now is 10:55 AM. |