Grep with parent folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep with parent folder
# 1  
Old 07-11-2012
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:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 04:23 AM.. Reason: code tags
# 2  
Old 07-11-2012
Can you show the expected output?
# 3  
Old 07-11-2012
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:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 04:46 AM.. Reason: code tags...
# 4  
Old 07-11-2012
Code:
 
find /absolute/path/ -type f -name "*_i*" | while read filename
do
   echo "Parent Folder : $(dirname $filename)"
done

# 5  
Old 07-11-2012
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:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 05:36 AM.. Reason: code tags
# 6  
Old 07-11-2012
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 )
# 7  
Old 07-11-2012
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:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 05:36 AM.. Reason: code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Move parent folder if contents match a parameter

Hi all, Hoping someone can help. I'm looking to be able to search a particular folder path for folders that contain certain files / formats and then move the parent folder. eg. /foo/bar/folder1/file.exe /foo/bar/folder2/file.exe and then move the folder1/2 tp another folder. So if... (1 Reply)
Discussion started by: springs2
1 Replies

3. Shell Programming and Scripting

To replace a string in file by its parent folder name.

Hi all, I have a directory structure like "folder1/folder2/website_name/folder3/folder4/file.php." I need to write a script which will enter into file.php and replace a particular string of characters with "website_name" folder name. In folder2,there are many such website's folders kept.So... (4 Replies)
Discussion started by: arien15488
4 Replies

4. Shell Programming and Scripting

how to grep parent process PID in shell scripting

hi all, for an example: $ ps -ef | grep apache | awk '{ print $2, $3 }' 24073 11784 28021 1 28022 1 28038 1 28041 28040 28045 28041 28047 28041 28040 1 28049 28041 28051 28041 28053 28041 28030 1 28054 28041 28055 28041 28056 28041 28057 28041 (5 Replies)
Discussion started by: raghur77
5 Replies

5. UNIX for Dummies Questions & Answers

Execute grep command within a folder

Experts, i need your help to made an enhance query using the existing one. i'm using a command "find . -exec grep -l 'search string' {} \;" to search a particular sting within the file within a folder. Basically, my idea is to fine tune my search. What i want is, instead of searching all files... (2 Replies)
Discussion started by: gav_dhiman
2 Replies

6. Shell Programming and Scripting

How to read a subfolder one by one in parent folder?

Hi friends, I am getting some trubles in folder reading. I am having 10 subfolders inside server7 folder. i wanna to read a subfolder name first and check if the foldername gets started with "http". if so , i need to read a file inside that folder. This willl continue for... (1 Reply)
Discussion started by: kamatchirajan
1 Replies

7. Shell Programming and Scripting

multiple child scripts running in backgroud, how to use grep on the parent?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (5 Replies)
Discussion started by: albertashish
5 Replies

8. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

9. UNIX for Dummies Questions & Answers

How do I search parent folder only?

I have a directory /dir1/dir2/dir3/dir4/dir5. How can I search for files only in dir4/ and not in dir5? I tried the maxdepth command, but it doesn't work in this flavor of sun OS. When I search for files in dir4, it's giving me results from dir5 as well. I don't want this. Thanks. (3 Replies)
Discussion started by: bbbngowc
3 Replies

10. UNIX for Dummies Questions & Answers

Grep from different folder

Hi All, Need your assistance/advice. I have 4 folders but using same name log file. /user/access/server1 /user/access/server2 /user/access/server3 /user/access/server4 filename is access.log.20060716.gz I want to grep the transaction id in the log file but I want to do... (2 Replies)
Discussion started by: nazri76
2 Replies
Login or Register to Ask a Question