How do I put the result of find $1
into an array?
In for loop:
for /f "delims=/" %%G in ('find $1') do %%G | cut -d\/ -f6-
The array+=("$REPLY") statement appends the new file name to the array array . The final line combines redirection and command substitution to provide the output of find to the standard input of the while loop.
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
The Bash find Command 101 The find command allows you to define those criteria to narrow down the exact file(s) you'd like to find. The find command finds or searches also for symbolic links (symlink). A symbolic link is a Linux shortcut file that points to another file or a folder on your computer.
I want to cry.
In bash:
file_list=()
while IFS= read -d $'\0' -r file ; do
file_list=("${file_list[@]}" "$file")
done < <(find "$1" -print0)
echo "${file_list[@]}"
file_list
is now an array containing the results of find "$1
What's special about "field 6"? It's not clear what you were attempting to do with your cut
command.
Do you want to cut each file after the 6th directory?
for file in "${file_list[@]}" ; do
echo "$file" | cut -d/ -f6-
done
But why "field 6"? Can I presume that you actually want to return just the last element of the path?
for file in "${file_list[@]}" ; do
echo "${file##*/}"
done
Or even
echo "${file_list[@]##*/}"
Which will give you the last path element for each path in the array. You could even do something with the result
for file in "${file_list[@]##*/}" ; do
echo "$file"
done
(One should probably use the builtin readarray
instead)
find "$1" -print0
Find stuff and 'print the full file name on the standard output, followed by a null character'. This is important as we will split that output by the null character later.
<(find "$1" -print0)
"Process Substitution" : The output of the find
subprocess is read in via a FIFO (i.e. the output of the find
subprocess behaves like a file here)
while ...
done < <(find "$1" -print0)
The output of the find
subprocess is read by the while
command via <
IFS= read -d $'\0' -r file
This is the while condition:
read
Read one line of input (from the find command). Returnvalue of read
is 0 unless EOF is encountered, at which point while exits.
-d $'\0'
...taking as delimiter the null character (see QUOTING in bash manpage). Which is done because we used the null character using -print0
earlier.
-r
backslash is not considered an escape character as it may be part of the filename
file
Result (first word actually, which is unique here) is put into variable file
IFS=
The command is run with IFS
, the special variable which contains the characters on which read
splits input into words unset. Because we don't want to split.
And inside the loop:
file_list=("${file_list[@]}" "$file")
Inside the loop, the file_list array is just grown by $file
, suitably quoted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With