0

I'm writing a small script that will help me debug some permission problems. I am passing the parent folder I wish to examine and am able to specify any sub-folders which I want to ignore.

I'm having a problem passing the constructed parameter string to find because some parts of it (the are being escaped. I can't seem to figure out how to provide the wildcard into the command in such a way that find accepts it properly. With the wildcard in place, that portion of the path string is qualified using single quotes that are escaped using '\'' and are confusing me (as I can't figure out how to control the transformation) and find (which is essentially ignoring my excludes)

I've been reading all about single and double quotes as well as escaping characters, but I haven't found an example similar to mine.

#!/bin/bash -f
# output permissions and ownership with path relative to specified parent.

Usage="$0 <parent path> <excluded child folder> ...."

if [ $# -lt 1 ]
then
    (>&2 echo -e $Usage)
    exit 1
else 
    parent=$1
    shift
    if [ $# -gt 0 ]
    then
        excludes=" ("
        for folder in $@
        do
            thisLine=" ! -path $parent$folder ! -path '$parent$folder/*'"    <=== the '*' wildcard is causing the problem I think.
            excludes=$excludes$thisLine
        done
        excludes=$excludes" )"
    fi
    (>&2 echo "=> find $parent $excludes -ls | awk '{print "'$3"|"$5"|"$6"|"$11}'"'")
    (>&2 echo )
set -vx
    find $parent $excludes -ls | awk '{print $3"|"$5"|"$6"|"$11}'
fi

The branch of the tree that I'm working with is /home/user/catkin_ws/src/clfsm which has three sub-folders, two of which I wish to exclude; cmake & include. The output below is in two parts: Top is the current output, which does not filter the folders I wish to exclude. The bottom part is correct, using the echoed command line from my code above.

The command to call the above script is: ~/myScripts/show_permissions.sh /media/nap/U14041/home/nap/catkin_ws/src/clfsm /cmake /include. Note that Stephen's solution requires that the sub-folders to be excluded be specified without a leading /.

user@rMBP-Ubuntu:[12:29]:/home/user/catkin_ws/src/clfsm$ ~/myScripts/show_permissions.sh /home/user/catkin_ws/src/clfsm /cmake /include
=> find /home/user/catkin_ws/src/clfsm  ( ! -path /home/user/catkin_ws/src/clfsm/cmake ! -path '/home/user/catkin_ws/src/clfsm/cmake/*' ! -path /home/user/catkin_ws/src/clfsm/include ! -path '/home/user/catkin_ws/src/clfsm/include/*' ) -ls | awk '{print $3"|"$5"|"$6"|"$11}'

++ find /home/user/catkin_ws/src/clfsm '(' '!' -path /home/user/catkin_ws/src/clfsm/cmake '!' -path ''\''/home/user/catkin_ws/src/clfsm/cmake/*'\''' '!' -path /home/user/catkin_ws/src/clfsm/include '!' -path ''\''/home/user/catkin_ws/src/clfsm/include/*'\''' ')' -ls
++ awk '{print $3"|"$5"|"$6"|"$11}'
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.layout
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/src
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitorsupport.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_machine.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitors.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_main.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_cc.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/cmake/FindLibDispatch.cmake
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/CMakeLists.txt
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_vector_factory.h
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/FSMControlStatus.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/FSM_Control.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/wb_fsm_control_status.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/typeClassDefs/wb_fsm_state_status.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/CLActionAction.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBQueryPredicate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMSuspensibleMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBSubMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/gu_util.h~
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/CLTransitionExpression.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBContext.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMState.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMAction.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMExpression.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_cc.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMTransition.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_visitorsupport.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/stringConstants.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_factory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMFactory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMachineVector.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMActivity.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_visitors.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMachine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_cc_delegate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_machine.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/gu_util.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSMWBPredicate.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/clfsm_wb_vector_factory.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/include/FSM.h
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/package.xml
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.cbp

user@rMBP-Ubuntu:[12:34]:/home/user/catkin_ws/src/clfsm$ find /home/user/catkin_ws/src/clfsm  \( ! -path /home/user/catkin_ws/src/clfsm/cmake ! -path '/home/user/catkin_ws/src/clfsm/cmake/*' ! -path /home/user/catkin_ws/src/clfsm/include ! -path '/home/user/catkin_ws/src/clfsm/include/*' \) -ls | awk '{print $3"|"$5"|"$6"|"$11}'
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.layout
drwxrwxr-x|user|user|/home/user/catkin_ws/src/clfsm/src
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitorsupport.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_machine.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_visitors.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_main.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/src/clfsm_cc.cc
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/CMakeLists.txt
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/package.xml
-rw-rw-r--|user|user|/home/user/catkin_ws/src/clfsm/clfsm.cbp
user@rMBP-Ubuntu:[12:35]:/home/user/catkin_ws/src/clfsm$ 
0

If I understand the requirement properly you should be using -path ... -prune to stop descending into trees.

Something like:

#!/bin/bash -f
# output permissions and ownership with path relative to specified parent.

Usage="$0 <parent path> <excluded child folder> ...."

if [ $# -lt 1 ]
then
    (>&2 echo -e $Usage)
    exit 1
else 
    parent=$1
    shift
    if [ $# -gt 0 ]
    then
        for folder in $@
        do
            thisLine=" ( -path $parent/$folder -prune ) -o"
            excludes=$excludes$thisLine
        done
    fi
set -vx
    find $parent $excludes -ls | awk '{print $3"|"$5"|"$6"|"$11}'
fi

The idea is to build out a string similar to

find /tmp/A \( -path /tmp/A/skip1 -prune \) -o -ls
9
  • Thnx for that. However, the `-prune isn't doing what I'm looking for. You'll notice that I'm echoing the command before the 'equivalent' gets executed. If you copy/paste it, and escape the leading/trailing parenthesis, you will see the output is different to what your script gives.
    – gone
    Jul 1 '16 at 2:32
  • Please provide an example of the tree and the output you're expecting. Your example code doesn't seem to match the written requirements, so I'm obviously not understanding your problem properly. Jul 1 '16 at 2:37
  • I have added the use case explanation to my OP. Also, what does the -o option do? I found it, but shouldn't it have a parameter 0-3.
    – gone
    Jul 1 '16 at 3:25
  • So that's the bit I don't understand. You added cmake as an "exclude" but I can see cmake lines in the output. So how is that excluded? That's what I tried to fix. But maybe I misunderstood the question. And I'm not sure I still do! If you're just worried about the lack of a / then change $parent/$folder to $parent$folder and you'll need it again. -o is "or". The command built is find ... \( -path ext1 -prune \) -o \( -path ext2 -prune \) -o -ls Jul 1 '16 at 10:34
  • Scroll down to the second part of the output.
    – gone
    Jul 1 '16 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.