LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2014, 03:07 PM   #1
psion
LQ Newbie
 
Registered: Jun 2002
Posts: 8

Rep: Reputation: 0
How to recursively find files with folder-specific results per line?


I'm trying to figure out a way to recursively search a directory structure for files matching a specific string, and then displaying multiple matches per line, with each directory's results on a new line... So, for example, searching for files matching *index*.jpg in the following directory structure:

toplevel\ (folder where script is run)
toplevel\subfolder1\some index file.jpg
toplevel\subfolder1\another index file.jpg
toplevel\subfolder2\more index file.jpg
toplevel\subfolder2\yet another index file.jpg

would result in a list that looks something like this:

toplevel\subfolder1\:
some index file.jpg TAB another index file.jpg
toplevel\subfolder2\:
more index file.jpg TAB yet another index file.jpg

The TAB can be a | or some other text string, doesn't have to be an actual TAB, but something I can easily regex or grep on the result file.

I've been trying to work something along the lines of:

Code:
find -maxdepth 2 -name Contact*index*jpg -execdir ls -C {} \; > output.txt
but it's listing everything in a single line so I'm not quite sure how to approach this. Even the command above would work if I could somehow append a custom string after the last result in each folder, though it wouldn't be as elegant as automatically breaking each directory's results onto its own line. Can someone offer some help?

Last edited by psion; 04-11-2014 at 03:10 PM. Reason: More accurate / descriptive title
 
Old 04-11-2014, 11:48 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
I suggest wrapping the find command in a loop that looks at individual subdirectories. If you set the bash globstar shell option then the **/ pattern will only match directory names. With find, if you use the print0 option then the file names are NULL separated.
Code:
bash-4.2$ cat test.sc
#!/bin/bash

shopt -s globstar

for d in **/; do
  echo "$d"
  find ./$d -name "*index*jpg" -print0
  echo ""
done

bash-4.2$ ./test.sc
subfolder1/
./subfolder1/another index file.jpg./subfolder1/some index file.jpg
subfolder2/
./subfolder2/yet another index file.jpg./subfolder2/more index file.jpg

Last edited by allend; 04-11-2014 at 11:54 PM.
 
Old 04-12-2014, 08:43 AM   #3
psion
LQ Newbie
 
Registered: Jun 2002
Posts: 8

Original Poster
Rep: Reputation: 0
Interesting, thank you for this. Is there a way to filter just the "file.jpg" in the result line and not show "./path/file.jpg" ? i.e. keep the path on a separate line and the files found in that path go to the line below.
 
Old 04-12-2014, 11:59 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
That was left as an exercise.
Code:
#!/bin/bash

shopt -s globstar

for d in **/; do
  echo "$d"
  cd $d
  find . -name "*index*jpg" -print0 | sed 's:\.\/::g'
  echo ""
  cd ..
done

Last edited by allend; 04-12-2014 at 12:56 PM.
 
  


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
Site Hacked - Deleting Specific Line from files recursively? CrewXp Linux - Security 6 04-27-2011 07:23 AM
How to recursively find files over a certain size beckettisdogg Linux - Newbie 1 04-15-2010 04:56 PM
awk, sed find and replace recursively from files bluewind Linux - Newbie 17 02-26-2010 10:06 AM
How do you Load files recursively from the command-line for console programs flupwatson Linux - General 4 08-25-2008 07:27 AM
how to find files in current directory only (not recursively) babu198649 Linux - Newbie 2 12-27-2007 01:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 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