0
#!/usr/bin/ksh

# *****************************************************************************************
# copy_properties.sh
# This script copies *.properties files from all directory excluding the 
# ones provided as the args. to a folder location of our choice.
# 
# *****************************************************************************************

echo "Starting the find and replace process for :" "$1"
set -x
# **************** Change Variables Here ************

startdirectory=$2 #"/home/ardsingh/test_properties/properties_files"
destinationFolder=$3

if [ -n "$4" ]; then
listOfFolderTobeIgnored=$4
#@list = split(/ /, $listOfFolderTobeIgnored);
#mapfile -t list << ($listOfFolderTobeIgnored)
IFS=' '
set -A list $listOfFolderTobeIgnored
foldersToBeIgnored="-o -name "${list[0]}
unset 'list[0]'
for item in "${list[@]}"
do
foldersToBeIgnored="$foldersToBeIgnored -o -name "
foldersToBeIgnored="$foldersToBeIgnored$item"
done
else
  echo "No input provided for folders to be ignored."
fi

#echo $foldersToBeIgnored

find "$startdirectory" -type d \( -name properties_file_folder_02_25 -o -name brmsdeploy -o -name TempJobs -o -name tmp -o -name logs -o -name deploy "$foldersToBeIgnored" \) -prune -o -name "*.properties" -type f -print -exec cp {} "$destinationFolder" \;

Getting below error:

find: bad option -o -name abc -o -name xyz
find: [-H | -L] path-list predicate-list

closed as off-topic by roaima, jimmij, Thomas, Kiwy, ilkkachu Mar 12 at 10:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – roaima, jimmij, Thomas, Kiwy, ilkkachu
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you sure that this part ` -prune -o -nameis correct? Maybe you can debug this find` statement by starting with a smaller number of concatenated -name filters and expand the statement in small steps. – mnille Mar 12 at 7:02
  • Found the solution to the problem. was struggling for more than 2-3 hours. It turned out to be the problem with space. between -o -name deploy "$foldersToBeIgnored". To fix it i changed it to -o -name deploy"$foldersToBeIgnored" – Ardhendu Shekhar Singh Mar 12 at 7:02
  • @mnille Yes the line pointed out by you is correct. – Ardhendu Shekhar Singh Mar 12 at 7:05
4

Rather than building up a string with command line options for ignoring directories with certain names, use an array. This way, the options for find that you add dynamically from the command line would be properly delimited.

The following script is a very basic one that takes a single search path, a destination directory, followed by any number of directory names to ignore. The code builds up a set of options for find in $@, the array of positional parameters.

#!/usr/bin/ksh

searchpath=$1
destdir=$2
shift 2

ignore=( brmsdeploy TempJobs tmp logs deploy )

if [ ! -d "$destdir" ]; then
    printf 'Destination directory "%s" does not exist\n' >&2
    exit 1
fi

# Process the directories to ignore from the command line.
# This replaces the positional parameters.
for name do
    set -- "$@" -o -name "$name"
    shift
done

# Process the directories that we always ignore.
# This adds to the positional parameters.
for name in "${ignore[@]}"; do
    set -- "$@" -o -name "$name"
done

shift # shift off the first -o

find "$searchpath" \( -type d \( "$@" \) -prune \) -o \
    \( -type f -name '*.properties' -exec cp {} "$destdir" \; \)

To use:

./script.sh /path/to/topdir /path/to/destdir somename someothername fluff

This would execute find, ignoring directories with names somename, someothername, and fluff. Note that this also allows these names to contain spaces, tabs and newlines (if they are quoted on the command line).

The actual find command executed by this command would be

find /path/to/topdir '(' -type d '(' -name somename -o -name someothername -o -name fluff -o -name brmsdeploy -o -name TempJobs -o -name tmp -o -name logs -o -name deploy ')' -prune ')' -o '(' -type f -name '*.properties' -exec cp '{}' /path/to/destdir ';' ')'
  • Thanks @Kusalananda for above snippet i will try it once. Will the above script work with Solaris as well as RHEL7. As my original script didn't worked well with Solaris, so i had to do some alteration to make it work on Solaris. – Ardhendu Shekhar Singh Mar 13 at 10:06
  • @ArdhenduShekharSingh I believe that the script would work with ksh/ksh93 or bash regardless of the actual Unix being used. – Kusalananda Mar 13 at 10:20

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