I have created the following code. At the end I want to process each file that is in the array individually but somewhere in this code I have not included the double quotes so the file names with a space in the middle it treats as 2 entries in the array:
#!/bin/bash
EXT=(sh mkv txt)
EXT_OPTS=()
# Now build the find command from the array
for i in "${EXT[@]}"; do
EXT_OPTS+=( -o -iname "*.$i" )
done
# remove the first thing in EXT_OPTS
EXT_OPTS=( "${EXT_OPTS[@]:1}" )
# Modify to add things to ignore:
EXT_OPTS=( '(' "${EXT_OPTS[@]}" ')' ! '(' -iname "*test*" -o -iname "*sample*" ')' )
#echo "${EXT_OPTS[@]}"
searchResults=($(find . -type f "${EXT_OPTS[@]}"))
#echo "$searchResults"
for R in "${searchResults[@]}"; do
echo "$R"
sleep 1
done
So the results I am getting are:
./Find2.sh
./untitled
2.sh
./countFiles.sh
./unrar.sh
./untitled
3.sh
./untitled
4.sh
./clearRAM.sh
./bash_test.sh
./Test_Log.txt
./untitled.txt
./Find.txt
./findTestscript.sh
./untitled.sh
./unrarTest.sh
./Test.sh
./Find.sh
./Test_Log
copy.txt
./untitled
5.sh
./IF2.sh
As an example untitled 5.sh has been added to the array as 2 entries. where have I forgot to add the "s
cheers
EDIT with suggested edits:
#!/bin/bash
EXT=(sh mkv txt)
EXT_OPTS=()
# Now build the find command from the array
for i in "${EXT[@]}"; do
EXT_OPTS+=( -o -iname "*.$i" )
done
# remove the first thing in EXT_OPTS
EXT_OPTS=( "${EXT_OPTS[@]:1}" )
# Modify to add things to ignore:
#EXT_OPTS=( "${EXT_OPTS[@]:-1}" )
EXT_OPTS=( '(' "${EXT_OPTS[@]}" ')' ! '(' -iname "*x0r*" -o -iname "*torrent*" ')' )
#echo "${EXT_OPTS[@]}"
#searchResults=($(find . -type f "${EXT_OPTS[@]}"))
#echo "$searchResults"
#for R in "${searchResults[@]}"; do
# echo "$R"
# sleep 1
#done
find . -type f "${EXT_OPTS[@]}" -exec sh -c '
for pathname do
printf "%s\n" "$pathname"
sleep 1
done' sh {} +
Now Produces:
./Find2.sh
./untitled 2.sh
./countFiles.sh
./unrar.sh
./untitled 3.sh
./Find3.sh
./untitled 4.sh
./clearRAM.sh
./bash_test.sh
./Test_Log.txt
./untitled.txt
./Find.txt
./findTestscript.sh
./untitled.sh
./unrarTest.sh
./Test.sh
./Find.sh
./Test_Log copy.txt
./untitled 5.sh
./IF2.sh
./Find4.sh
I have not been writing bash for very long so i'm still learning.
what I was trying to do was set up the next bit to deal with the various file types. For example if it was a .sh or a .txt files to be copied to one folder and additional to that all the .tmp files go to another folder and then .log files to another folder.
What function do I need to read about to do that ? I was going to use a case function but is that the best way to do it?
– Andy M Feb 08 '19 at 22:53find
for each case.find ... -type f -name '*.tmp' -exec mv -t tmp-files {} +
for.tmp
files, for example. – Kusalananda Feb 08 '19 at 22:58find
command with\( -name '*.tmp' -exec mv ... \) -o \( ... \) -o \( ... \)
etc., but it wouldbe ugly and hard to maintain. – Kusalananda Feb 08 '19 at 23:00