1

I am looking to traverse a directory using a conditional for / while Loop

Scenario : path is /home/ABCD/apple/ball/car/divider.txt

Say I always start my program from /home I need to iterate from home -- > ABCD --> apple --> ball --> car --> divider.txt

Every time I iterate, check if the obtained path is a directory or a file, if file exit the loop and return me the path if the returned path is directory, loop one more round and continue..

Updated question

FILES="home"
for f in $FILES
do

    echo "Processing $f"  >> "I get ABCD as output
        if[-d $f]
  --> returns true, in my next loop, I should get the output as /home/ABCD/apple..
        else 
          Break;
        fi

done

after I exit the for loop, I should have the /home/ABCD/apple/ball/car/ as output

8
  • 2
    How do you know what to loop over? Commented Feb 26, 2013 at 4:37
  • I get that folder starting point from other program, in the above case I get input as /home/ABCD and other case, I may get a parameter as /home/example, then I need to start iterating Commented Feb 26, 2013 at 4:50
  • You need to iterate over every file in /home/ABCD? Commented Feb 26, 2013 at 4:51
  • Yes I need to iterate over every entry, checking if the contents is a directory or a file, if Files, exit with the path, if its directory, continue the iteration one more time with updated path in the above case /home/ABCD/apple/ then /home/ABCD/apple/ball ..etc Commented Feb 26, 2013 at 4:53
  • Maybe find will help you Commented Feb 26, 2013 at 4:57

3 Answers 3

2

Besides the multi-purpose find you might also want to take a look at tree. It will list contents of directories in a tree-like format.

$ tree -F /home/ABCD/
/home/ABCD/
`-- apple/
    `-- ball/
        `-- car/
            `-- divider.txt

3 directories, 1 file
Sign up to request clarification or add additional context in comments.

2 Comments

I tried tree command, its giving command not found error message
You might have to install it first, in Debian/Ubuntu sudo aptitude install tree.
1

This is the way I have implemented to get it working

for names in $(find /tmp/files/ -type f); 
do
    echo " ${directoryName} -- Directory Name found after find command : names" 

    <== Do your Processing here  ==>

done

Names will have each file with the complete folder level

/tmp/files is the folder under which I am finding the files

1 Comment

If you have any files (or directories) with spaces in them, change the line for names in $(find /tmp/files/ -type f); to find /tmp/files/ -type f | while read names
0

find /home -type d

will give you all the directories under /home and nothing else. replace /home with the directory of your choice and you will get directories under that level.

if your heart is set on checking every file one by one, the if..then test condition you are looking for is :

if [ -f $FILE ]
then
echo "this is a regular file"
else 
echo "this is not a regular file, but it might be a special file, a pipe etc."
fi

-or-

if [ -d $FILE ]
then
echo "this is a directory. Your search should go further"
else 
echo "this is a file and buck stops here"
fi

1 Comment

Your second answer looks good, I was able to get up to that level, if I find that as a directory, again I need to search , that part I am getting stuck Not able to find any ways to implement that, 2) one more point I find is every time I see the level of sub directories will be varying for every input, In my example I have used 4 directories after home, it may 3 levels for other input, it may be 7 levels for other input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.