Hi All,
I need to store the output of "find ." to an array one by one. Output of find . in my case will look like :-
.
./one
./one/a
./one/b
./one/c
./two
So my first array element should be "/one" and second one "/one/a" (need to remove "." from the output as well).
Then I need to access each element from the array. Please help me out
Thanks
Renjesh
clx
2
Try:
arr=($(find . ! -name "." | tr '\n' ' '))
echo ${arr[0]}
echo ${arr[1]}
.
.
.
I tried but not getting proper output.
rtq1@server [/home/rtq1]
$ cat test.sh
cd /home/rtq1/ESR11.2
arr=$(find . ! -name "." | tr '\n' ' ')
echo ${arr[0]}
rtq1@server [/home/rtq1]
$ ./test.sh
./one ./one/a ./one/b ./one/c ./two ./three ./content.txt ./b ./a
When i tried to print the 1st element from array it is printing all elements ..
You have missed ( )
arr=($(find . ! -name "." | tr '\n' ' '))
If i put that (), it is throwing a syntax error .. 
$ ./test.sh
./test.sh[2]: Syntax error at line 2 : `(' is not expected.
clx
6
what shell you are using?
the above should work for bash.
for ksh, try
set -A arr $(find . ! -name "." | tr '\n' ' ')
fhernu
7
You can also try this
tst_arr=`find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}`
for (( i = 0 ; i < ${#tst_arr[@]} ; i++))
do
echo "${tst_arr[$i]}"
done
Now it is working, but "." is still there with the output. I want to remove the "." then store data in array.
$ ./test.sh
./two
fhernu
9
tst_arr=`find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}`
should remove the leading "."
clx
10
Do you mean dot in filenames'?
I assumed the first dot in your listing.
In that case simply replace the dot after the find with the current dir.
set -A arr $(find /path/to/dir | tr '\n' ' ')
Btw, This would also show the sub directories (if any). If you don't need those use -type f switch.
This will show the absolute path of the files.
Hi All,
it is working fine now ...
set -A arr `find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}`
This works for me ..
Thanks a lot
Renjesh