Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-11-2012
Registered User
 
Join Date: Jul 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Grep with parent folder

Hi,

When I do


Code:
ls -l | grep -h STRING

I need not only to get those files, but also the parent folder of those files,
no idea how do that.

BTW the names of those parent folders are never longer than 3 digits, if that helps.

Any assistance most welcome,

/bw,
OmarKN


Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 03:23 AM.. Reason: code tags
Sponsored Links
    #2  
Old 07-11-2012
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 456
Thanks: 35
Thanked 57 Times in 55 Posts
Can you show the expected output?
Sponsored Links
    #3  
Old 07-11-2012
Registered User
 
Join Date: Jul 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Sure, here are the files without their parent folders:

i
Code:
3: okn$ ls | grep -h _i
_i-lklist.txt
_img
_img2
compig_i.html
compst_i.html

It's the parent folders which I 'd also like to be listed, :-)

bw, Omar KN


Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 03:46 AM.. Reason: code tags...
    #4  
Old 07-11-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
 
find /absolute/path/ -type f -name "*_i*" | while read filename
do
   echo "Parent Folder : $(dirname $filename)"
done

Sponsored Links
    #5  
Old 07-11-2012
Registered User
 
Join Date: Jul 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Well this is not bad,

I copied it and outcome was:


Code:
i3:chello uppd_G5 okn$ find . -type f -name "*_i*" | while read filename; do    echo "Parent Folder : $(dirname $filename)"; done
Parent Folder : .
Parent Folder : ./k
Parent Folder : ./k
Parent Folder : ./k
Parent Folder : ./n
Parent Folder : ./naw
Parent Folder : ./o

Which gives the Parent Folders of those files, but not the file-names themselves, so I don't know in which Parent Folder XY-file is.

Is there not a way with grep?

Thank you, bw, OmarKN


Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 04:36 AM.. Reason: code tags
Sponsored Links
    #6  
Old 07-11-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
 
find /absolute/path/ -type f -name "*_i*" | while read filename
do
   echo "File Name : $(basename $filename)"
   echo "Parent Folder : $(dirname $filename)"
done

absolute path --- give the path from / ( root )
Sponsored Links
    #7  
Old 07-11-2012
Registered User
 
Join Date: Jul 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Nope, sorry:


Code:
i3:/ okn$ find /Volumes/main_HD/Users/omarkn/Sites/chello\ uppd_G5 -type f -name "*_i*" | while read filename; do    echo "Parent Folder : $(dirname $filename)"; done
usage: dirname path
Parent Folder : 
usage: dirname path
Parent Folder : 
usage: dirname path
Parent Folder :
...
...

And so on, this is from root...

bw, OmarKN


Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 04:36 AM.. Reason: code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To replace a string in file by its parent folder name. arien15488 Shell Programming and Scripting 4 12-10-2010 01:21 AM
how to grep parent process PID in shell scripting raghur77 Shell Programming and Scripting 5 06-25-2009 08:36 AM
How to read a subfolder one by one in parent folder? kamatchirajan Shell Programming and Scripting 1 01-08-2009 04:56 PM
full path of a file situated either in parent's dir. or parent's parent dir. so on... yahoo! Shell Programming and Scripting 1 07-24-2008 07:24 AM
How do I search parent folder only? bbbngowc UNIX for Dummies Questions & Answers 3 04-17-2008 04:42 AM



All times are GMT -4. The time now is 12:00 AM.