5

I have a requirement where I need to validate the nested folder structure.

Ex: I have 3 folders: test1, test2, test3 (Practically there can be anynumber of such folders). Now all these folders should contain folder like V1, V2... like that ('V' followed by number). Under this folder (V1... ), there should be only two folders names 'dummy1' and 'dummy2'.

So all these checks should be done recursively until all the folders like test 1... test2... Are checked.

4 Answers 4

3

You could use the find command. Find recurses through a directory structure doing things to each file/directory. You can limit it to directories with the -type flag.

find start_dir -type d -print

To actually validate all have the structure I think you are going to have to write a script looking at this list - and I think you would have to do this in a more powerful scripting language than shell e.g. perl, python, ruby, tcl.

3
  • I agree but the requirement is on shell script only...if possible can you please tell me ...after finding the directory how to use the cd command recursively on that
    – Ankit
    Apr 18, 2011 at 10:50
  • This command will list all the directories so no need to recurse any more. You then need to process the output - why only a shell script as that is an artifical limit - any more than two lines in shell I write in something else.
    – mmmmmm
    Apr 18, 2011 at 11:01
  • will you be able to help me on this by writng it on another scripting tool..provided i will be able to execute it on putty..
    – Ankit
    Apr 18, 2011 at 11:09
1

Another option is the simple tree command.

Just do

tree directory/

then 'validate' the output of that.

4
  • can you please tell me on how to validate that recursively...as i need to have a shell script which will take care of all this in one shot.
    – Ankit
    Apr 18, 2011 at 10:56
  • Oh, then I guess a bunch of bash "for loops" with varying tests is what you want if you want the validation done automatically. I don't know how stringent you want things though? Can test1 ONLY contain V{1..X} or is a file 'readme.txt' in there also permissable etc. etc.?
    – Pricey
    Apr 18, 2011 at 11:15
  • well it will contain only V(1..X)
    – Ankit
    Apr 18, 2011 at 11:26
  • But then what about consecutive numbers... can you have V1, V2, V5, V9000 etc. ? I don't like your question I'm afraid, it isn't really a question but a request for us to do your work and write you a script. Using standard bash loops like 'for' and 'if' should make this reasonably easy.
    – Pricey
    Apr 18, 2011 at 11:42
1

positive test:

find test* -maxdepth 1 -type d -regex ".*/V[0-9]+" 
find test* -mindepth 2 -maxdepth 2 -type d -regex ".*/V[0-9]+/dummy[12]" 

negative test:

find test* -maxdepth 1 -type d -not -regex ".*/V[0-9]+" 
find test* -mindepth 2 -maxdepth 2 -type d -not -regex ".*/V[0-9]+/dummy[12]" 
3
  • thanks a lot for your help..it worked that way....what if i want to store this results in a file... thanks ANkit
    – Ankit
    Apr 19, 2011 at 7:25
  • find test* -maxdepth 1 -type d -regex ".*/V[0-9]+" > list1 Apr 19, 2011 at 13:41
  • yeah i did the same thing some thing like find test* -maxdepth 1 -type d -regex ".*/V[0-9]+" -print > filename.....
    – Ankit
    Apr 20, 2011 at 5:42
0

Try using the below command:

find . -name dummy1 -exec echo "Hello, '{}'" \;
find . -name dummy2 -exec echo "Hello, '{}'" \;

this gives the output along with the PWD.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .