Find folder within folder, then find other folder in same dir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find folder within folder, then find other folder in same dir
# 1  
Old 01-27-2011
Find folder within folder, then find other folder in same dir

Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do:

I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory contains several directories within with numbers instead of names. The directory 'fooBar' is inside of one of these directories. So for example: /var/root/Applications/2348970343/fooBar. I need to find 'fooBar' and then use that directory that contains it /var/root/Applications/2348970343/, and find a directory named "Documents" in that same directory. ALL of the numbered directories contain a folder named "Documents" so searching for that won't help. I need to find the 'Documents' folder within the same folder that contains 'fooBar'

Code:
find /var/root/Applications/ -type d "fooBar* -maxdepth 1 2>/dev/null

That code effectively returns the folder containing 'fooBar' for me. But from there I can't wrap my mind around getting the folder 'Documents' where 'fooBar' is contained. Thank you for your help in advance. Smilie
# 2  
Old 01-27-2011
Code:
find /var/root/Applications/ -type d "fooBar* -maxdepth 1 2>/dev/null | while read dir
do
   [ -d "$dir/Documents" ] && echo $dir/Documents
done

or

Code:
for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && echo $dir/Documents
done

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-27-2011
Thank you again, just one more thing now...

Ok awesome, now that I can find that 'Documents' folder, I need to search it for a filetype (.rar) and copy those to a folder. When I know where the 'Documents' folder is I normally do this:
Code:
find /var/root/Applications/known_folder/Documents/ -type f   "*.rar" | xargs cp -n - p -t /to/my/directory/

I dunno how to combine your search with my normal search. Thanks again.

edit: how bout this
Code:
for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar | xargs blah blah blah?
done


Last edited by DC Slick; 01-27-2011 at 09:18 PM.. Reason: Possible solution...
# 4  
Old 01-27-2011
Close :

Code:
for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar 
done | xargs cp -n -p -t /to/my/directory/

# 5  
Old 01-27-2011
so close but not quite yet

Ok so the 'Documents' folder is in the same folder as fooBar but its not a subdirectory of fooBar. When I type what you've offered in terminal, it returns nothing. I look at it and it appears your asking me to have it test for 'fooBar/Documents' when in fact its /this/way/to/fooBar which is also /this/way/to/Documents. Documents and fooBar are in the "to" folder using this example.

Code:
ls -a /this/way/to

would produce about 50 obfuscated dirnames, inside of one of these dirs theres a subdir called Documents and a file named fooBar.
# 6  
Old 01-27-2011
OK how about this
(requires bash or ksh shell)

Code:
for dir in /var/root/Applications/*/fooBar
do
    dir=${dir%/fooBar}
    [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar 
done | xargs cp -n -p -t /to/my/directory/

# 7  
Old 01-27-2011
Quote:
Originally Posted by Chubler_XL
OK how about this
(requires bash or ksh shell)
Eek, sadly I'm trying to do this on my iPhone which does not have bash nor korn, just plain ol' sh.
Also I'm getting paths must precede expression error...
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

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. UNIX for Dummies Questions & Answers

How to find from / but exclude certain folder?

:)Hi Unix Specialists, I need your advice on how to find all the files from root ( / ) filesystem but exclude those from /export/home (different filesystem) folder. Below are some of the find statements that I have tried without success: find / -name '/export/home' -prune -o print -ls ... (6 Replies)
Discussion started by: gjackson123
6 Replies

4. Shell Programming and Scripting

Find and Tar a Folder

Hi all, I have created a function that looks for a folder in a particular directory, checks the date it was last modified and if its old then compress it. This works fine for files using gzip. However for folders I had to use tar. This is my function: compressOldFolder() { # $1 is... (10 Replies)
Discussion started by: TasosARISFC
10 Replies

5. Shell Programming and Scripting

Find a folder from a directory

Hi, I have a folder name XX and a directory N:. Now I want to find the folder on that directory and print the path. I tried with grep "XX" N: but also search for files named as XX, but I want to find onlt folder and print. please help me.. Thanks in advance (12 Replies)
Discussion started by: arup1980
12 Replies

6. Shell Programming and Scripting

find the folder with space in name.

A solaris server with SAMBA share folder. The PC users created many folders with space on it, I want to find them out, but not list its subfolders. For example, I have below folders Copy of ABC/efg/xy sa/Test again/xyt If I use command: find . -type d |grep " " I will list 6 folders, but... (2 Replies)
Discussion started by: rdcwayx
2 Replies

7. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

9. Shell Programming and Scripting

find how many files are in use in a folder

How can i find how many files are in use in a folder or its sub folders in unix i tried with fuser .. but i coulnt getting ... (3 Replies)
Discussion started by: smongam
3 Replies

10. Shell Programming and Scripting

Take a folder name and find it in another folder (Complicated)

Hi Script Experts, Here is my scenario: 1. /var/mqm/qmgrs folder will contain 11 folders as follows: 1. /var/mqm/qmgrs/Folder_Name1 ....................../Folder_Name2 ....................../Folder_Name3 ....... ...................../Folder_Name11 2. if Folder_Name1 exists... (5 Replies)
Discussion started by: hkhan12
5 Replies
Login or Register to Ask a Question