Bash help

Say you have a directory

/dir/dir1/dir2/dirs2/subdir/subdir1/logs

Im trying to come up with a loop that will either start at the top level and work down… eg

/dir/
/dir/dir1/
/dir/dir1/dir2/
/dir/dir1/dir2/dirs2/
/dir/dir1/dir2/dirs2/subdir/

…etc

or in reverse…

/dir/dir1/dir2/dirs2/subdir/subdir1/logs
/dir/dir1/dir2/dirs2/subdir/subdir1/
/dir/dir1/dir2/dirs2/subdir/
/dir/dir1/dir2/dirs2/
/dir/dir1/dir2/

… etc

find /path -type d -exec <do your thing>

There are other ways

edit: doesn’t necessarily loop, as once its done going through all the directories it will stop

1 Like

not sure about the reverse thing, you may need to store in some variables and what not.
‘ls -r’ should list recursively e.g. iterate through libraries as they come.
maybe, and im not putting my head on the block here.
but ‘ls -R (path)| sort -R’

Won’t work.

It’ll only grab directories under the /path/path2 that I give it.
Thanks for the reply.

It’ll grab all directories under the base path you give it, example:

a@host:~$ find ~ -type d -exec echo {} \;
/home/a
/home/a/1
/home/a/1/1
/home/a/1/1/1
/home/a/1/1/2
/home/a/1/1/3
/home/a/1/2
/home/a/1/3
/home/a/2
/home/a/2/1
/home/a/2/2
/home/a/2/2/1
/home/a/2/2/2
/home/a/2/2/3
/home/a/2/3
/home/a/2/3/1
/home/a/2/3/2
/home/a/2/3/3
/home/a/2/3/3/1
/home/a/2/3/3/2
/home/a/2/3/3/3

What do you require thats different?

#!/bin/bash

cmd="ls -R (path)"
dirs=´eval $cmd´

for(( i=0 ; i<${dirs[@]} ; i++ ))
do
echo "I am larger"
done

for(( i=${dirs[@]} ;i>0 ;i-- ))
do
echo "I am lesser"
done

This should “approx” do it.
The script doesnt work but it’s base idea is read a path recursively, and store as a array.
and just print, and print recursively.
I wont give you the actual solution this is what google is for, but it’ll nudge you in the right direction.

1 Like

Not trying to get directories that are under the starting path.
Trying to set facls on a specific dir and then going back up the tree … set a facl for g+x so they can cd into the dir below.

I got it figured out… post the script later… if anyone cares

1 Like

Dont need subdirectories… explain more later.

Your solution post makes things far more clearer :smiley: . your OP says recurse directories, which both solutions do :stuck_out_tongue:

You litterally asked for a recursive iteration back, and forth in the OP.
down and up, what ever you wanna call it.

GL on your assignment.

Say the dir treee looks like this:

image

And I only want these two dirs:

/tmp/tmp/DJE4/LOGS/lows
/tmp/tmp/PIA/files/tif

image

I needed to set a write FACL on just those two… but for the other group/user to get into either of those dirs they need ‘x’ permission on the dirs leading into those two specific dirs.

So given i needed write on:

/tmp/tmp/DJE4/LOGS/lows
/tmp/tmp/PIA/files/tif

I needed to give execute on each of these:

/tmp
/tmp/tmp
/tmp/tmp/DJE4
/tmp/tmp/DJE4/LOGS
/tmp/tmp/PIA
/tmp/tmp/PIA/files

@Eden @Lauritzen
Sorry I was so bad in the OP trying to explain what I needed.
I really appreciate attempt to help me.
I was rushing when I made the OP trying to get some help.

I cant remember exactly… but the script ended up working looked like this:

LIST=$( echo /tmp/tmp/DJE4/LOGS/lows /tmp/tmp/PIA/files/tif )

for zz in $LIST ; do

if [ -d $zz ] ; then
 echo $zz
 setfacl -m g:devs:rwX $zz
 
 count=$( echo $zz | awk -F/ '{print NF}' )
  let count=count-1

   while [ "$count" -gt 1 ] 
     do 
     NEWDIR=$(echo $zz | cut -d/ -f1-$count )
     setfacl -m g:devs:x $NEWDIR
     let count=count-1
   done
 else 
    exit 0
 fi

done

Output looks like this
image