简体   繁体   中英

Find using file for folder locations Linux Bash

I am trying to use a txt file to store folder locations to use in find command. But keep getting folder not found works with only one folder location in file

with "$addfolder"

found=$(find "$addfolder" ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)

and replacing \\"

addfolder="$addfolder $Folder"

folder.txt :- Main/Public Main/General Not Used Old Backup Files

#!/bin/bash 
addfolder=""    
filename="Settings/folders.txt"

#Read Folder.txt for locations
while read -r Folder; do
 if [ ! "$Folder" == "" ];then
  if [ -d "$Folder" ]; then
   addfolder="$addfolder \"$Folder\""
   echo "$addfolder"
  fi
fi
done < "$filename"

if [  "$addfolder" == "" ]; then
 exit
fi

echo -e "\e[36mEnter Filename To Find :-\e[0m"
read -p "" ffind
echo -e "\e[92mSearching:\e[0m"
found=$(find $addfolder ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)

echo -e "\e[33m$found\e[0m"
echo "Press Enter To Exit"
read -s -n 1 -p ""

Regular variables should only hold single strings.

To hold lists of strings, use an array:

#!/bin/bash 
addfolder=()    
filename="Settings/folders.txt"

#Read Folder.txt for locations
while IFS= read -r Folder; do
 if [ ! "$Folder" == "" ];then
  if [ -d "$Folder" ]; then
   addfolder+=( "$Folder" )
   echo "${addfolder[@]}"
  fi
fi
done < "$filename"

if [  "${#addfolder[@]}" == 0 ]; then
 exit
fi

echo -e "\e[36mEnter Filename To Find :-\e[0m"
read -p "" ffind
echo -e "\e[92mSearching:\e[0m"
found=$(find "${addfolder[@]}" ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)

echo -e "\e[33m$found\e[0m"
echo "Press Enter To Exit"
read -s -n 1 -p ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM