LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   formatting find output (http://www.linuxquestions.org/questions/linux-newbie-8/formatting-find-output-817438/)

usr_handle 07-01-2010 11:21 AM

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!

druuna 07-01-2010 11:50 AM

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.

usr_handle 07-01-2010 12:01 PM

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.

unSpawn 07-01-2010 12:03 PM

Quote:

Originally Posted by druuna (Post 4020749)
This will remove the directory: find ./ -name *.xml -exec basename {} \;

Can't he use 'find ./ -iname \*.xml -printf '%f\n';'?
"%f" should provide only filename and "\n" a newline.

druuna 07-01-2010 12:16 PM

Hi,

Quote:

Can't he use 'find ./ -iname \*.xml -printf '%f\n';'?
"%f" should provide only filename and "\n" a newline.
Nice, I obviously didn't think of that.

BTW: The problem (it does not put newlines after every file) could be related to \n vs \r\n (linux vs windows newline).

druuna 07-01-2010 12:21 PM

Hi,

Quote:

Originally Posted by usr_handle (Post 4020762)
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.

basename (and its opposite dirname) are normal linux/unix commands.

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.

usr_handle 07-01-2010 12:44 PM

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!

druuna 07-01-2010 12:55 PM

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 02:16 PM.