LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2010, 11:21 AM   #1
usr_handle
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
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!
 
Old 07-01-2010, 11:50 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 07-01-2010, 12:01 PM   #3
usr_handle
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 07-01-2010, 12:03 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by druuna View Post
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.
 
Old 07-01-2010, 12:16 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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).
 
Old 07-01-2010, 12:21 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
Originally Posted by usr_handle View Post
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.

Last edited by druuna; 07-01-2010 at 12:22 PM. Reason: Fixed a typo
 
Old 07-01-2010, 12:44 PM   #7
usr_handle
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 07-01-2010, 12:55 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 07-01-2010 at 12:57 PM. Reason: Fixed a command typo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
formatting iostat output suran Linux - General 4 07-14-2009 07:00 AM
[SHELL] Output formatting Adrnalnrsh Programming 3 09-22-2008 10:29 AM
Formatting output of find suitable for rm JussiKp Linux - Newbie 3 12-24-2006 03:00 AM
Formatting output krock923 Programming 2 06-07-2006 07:26 AM
Formatting output of ls doodar Linux - Newbie 29 07-29-2004 01:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration