How to Use a Variable with the Find Command
by Steve R. from LinuxQuestions.org on (#5C70Y)
I have a homemade backup program where I use the find command to send the files to be backed-up to tar. The find command below works as expected with the filenames hard-coded.
Code:search_directory="/home/steve"
find $search_directory ! -path "${search_directory}/\.*" ! -path "${search_directory}/X\-Plane_11*" ! -path "${search_directory}/Torrent_Files*" > test_find.txtWhat I would like to do is have the search criteria use a variable (exclude_string) to exclude files that do not have to be backed-up. However, that is not working.
Code:exclude_string=' ! -path "${search_directory}/\.*" ! -path "${search_directory}/X\-Plane_11*" ! -path "${search_directory}/Torrent_Files*"'
find $search_directory ${exclude_string} > test_find.txtWhen run, no error is displayed. The find command simply shows all files as if the variable exclude_string did not exist.


Code:search_directory="/home/steve"
find $search_directory ! -path "${search_directory}/\.*" ! -path "${search_directory}/X\-Plane_11*" ! -path "${search_directory}/Torrent_Files*" > test_find.txtWhat I would like to do is have the search criteria use a variable (exclude_string) to exclude files that do not have to be backed-up. However, that is not working.
Code:exclude_string=' ! -path "${search_directory}/\.*" ! -path "${search_directory}/X\-Plane_11*" ! -path "${search_directory}/Torrent_Files*"'
find $search_directory ${exclude_string} > test_find.txtWhen run, no error is displayed. The find command simply shows all files as if the variable exclude_string did not exist.