LinuxQuestions.org
Review your favorite Linux distribution.
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, 04: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 04:10 PM. Reason: More accurate / descriptive title
 
Old 04-12-2014, 12:48 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,036

Rep: Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762
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-12-2014 at 12:54 AM.
 
Old 04-12-2014, 09: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, 12:59 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,036

Rep: Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762
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 01:56 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

All times are GMT -5. The time now is 12:44 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration