0

Im trying to iteratively compose a directory exclusion list for Find. I construct the exclusion path through iterating through a text file. This is all working however Ive come across a stumbling block when it comes to paths with spaces in them. If I pass a path in with spaces Find doesn't recognise them. Despite the path being within speech marks

This demonstrates:

####
line="Documents/Microsoft User Data"
excludehead="-not ( -path "
excludetail=" -prune )"
excludefolder="$HOME/$line"

###
base_list=$(printf %s "$excludehead" "\"$excludefolder\""  "$excludetail" ) 
find  $HOME  $base_list  -name "*[<>:/|?#%\\\\*]*"

This results in:

find: User": unknown primary or operator

2 Answers 2

2

This is where you must use a shell array:

find_opts=( -not "(" -path "$HOME/$line" -prune ")" )
find "$HOME" "${find_opts[@]}" -name ...

You can see what the array contains:

$ declare -p find_opts
declare -a find_opts='([0]="-not" [1]="(" [2]="-path" [3]="/home/jackman/Documents/Microsoft User Data" [4]="-prune" [5]=")")'

Note that element #3 contains a string with spaces.

Sign up to request clarification or add additional context in comments.

Comments

1

Define a function instead of trying to construct a command line dynamically.

find_with_exclude () {
  find "$HOME" -not \( -path "$HOME/$1" -prune \) -name "*[<>:/|?#%\\\\*]*"
}

find_with_exclude "Documents/Microsoft User Data"

Comments

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.