Existem vários problemas aqui. Primeiro, não tenho idéia do que você está tentando fazer com IFS='\n'
, então vou ignorá-lo. Segundo, você parece estar copiando filename
não uma variável chamada filename
e, de qualquer forma, você não está configurando nada.
Se eu entendi o que você está tentando fazer corretamente, você está procurando por algo assim:
#!/usr/bin/env bash
## Pass the directory to search in as an argument,
## easier to use and avoids including /pathfolder/
## as long as your run the script from a different
## directory.
dir="$1";
## Destination directory
dest="$2"
## Don't use capitalized variables in bash,
## environmental vars are CAPS and that could cause
## problems if you use something like $USER.
keywords=("Florida" "FL" "Miami-Dade" "Aventura" "Bal Harbour" "Bay Harbor Islands")
## Find all files of the right size and pass their names
## to the while loop. Skip any files matching $dest
find "$dir" -size +1c -path "$dest" -prune -o -type f -print0 |
## This is to make sure it works with file names containing strange
## characters like newlines. If they don't, you can remove the -print0
## from find and simplify to 'while read file name'
while IFS= read -r -d $'./foo.sh "/source/dir" "/target/dir"
' filename
do
for keyword in "${keywords[@]}";
do
## -m 1: stop at first match
grep -qm 1 -Fw "$keyword" "$filename" &&
## Create a dir for the keyword if it does not exist
mkdir -p "$dest"/"$keyword" &&
## copy the file if grep found a match (that's what && above means)
cp -vf "$filename" "$dest"/"$keyword"/ &&
## move to the next filename if the copy succeds
break
done
done
Salve o script em algum lugar, foo.sh
e execute-o, dando a ele a pasta a ser pesquisada como um argumento:
#!/usr/bin/env bash
## Pass the directory to search in as an argument,
## easier to use and avoids including /pathfolder/
## as long as your run the script from a different
## directory.
dir="$1";
## Destination directory
dest="$2"
## Don't use capitalized variables in bash,
## environmental vars are CAPS and that could cause
## problems if you use something like $USER.
keywords=("Florida" "FL" "Miami-Dade" "Aventura" "Bal Harbour" "Bay Harbor Islands")
## Find all files of the right size and pass their names
## to the while loop. Skip any files matching $dest
find "$dir" -size +1c -path "$dest" -prune -o -type f -print0 |
## This is to make sure it works with file names containing strange
## characters like newlines. If they don't, you can remove the -print0
## from find and simplify to 'while read file name'
while IFS= read -r -d $'./foo.sh "/source/dir" "/target/dir"
' filename
do
for keyword in "${keywords[@]}";
do
## -m 1: stop at first match
grep -qm 1 -Fw "$keyword" "$filename" &&
## Create a dir for the keyword if it does not exist
mkdir -p "$dest"/"$keyword" &&
## copy the file if grep found a match (that's what && above means)
cp -vf "$filename" "$dest"/"$keyword"/ &&
## move to the next filename if the copy succeds
break
done
done