i am looking for a way to sort the results of find
returning a number of directories correctly for further processing in a bash script. since filenames can\'t conta
-t
is the field separator. If you want to use \0
as the line separator then you need to use -z
.
For further processing in a Bash script see, for example:
Capturing output of find . -print0 into a bash array
# cf. http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS='' read -r -d $'\0' dir; do
a[i++]="$dir" # or however you want to process each directory
done < <(find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | LC_ALL=C sort -z)
printf '%s\n' "${#a[@]}"
printf '%s\n' "${a[@]}"
# btw, you may use printf to add zero bytes
printf '%c\000' g u b k | sort -z | tr '\0' ' '
printf '%s\000' g1 u2 b3 k4 | sort -z | tr '\0' ' '
Use the -z
option to sort
zero-terminated data sets:
find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '\0' '\n'