将find命令的输出存储在一个数组中

时间:2021-10-21 14:02:37

How do I put the result of find $1 into an array?

如何将查找$1的结果放入数组中?

In for loop:

在for循环:

for /f "delims=/" %%G in ('find $1') do %%G | cut -d\/ -f6-

5 个解决方案

#1


-5  

To loop through a find, you can simply use find:

要循环查找,只需使用find:

for file in "`find "$1"`"; do
    echo "$file" | cut -d/ -f6-
done

It was what I got from your question.

这是我从你的问题中得到的。

#2


51  

I want to cry.

我想哭。

In bash:

在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

file_list现在是一个包含查找“$1”结果的数组

What's special about "field 6"? It's not clear what you were attempting to do with your cut command.

第6场有什么特别之处?现在还不清楚你想用cut命令做什么。

Do you want to cut each file after the 6th directory?

你想要在第6个目录之后删除每个文件吗?

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?

但为什么“6”?我能假设你真的想要返回路径的最后一个元素吗?

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

Explanation of the bash program elements:

(One should probably use the builtin readarray instead)

(人们可能应该使用内置的readarray)

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.

查找并“在标准输出上打印完整的文件名,后跟一个空字符”。这一点很重要,因为稍后我们将用null字符分割这个输出。

<(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)

“进程替换”:find子进程的输出通过FIFO读取(即,find子进程的输出行为类似于此处的文件)

while ... 
done < <(find "$1" -print0)

The output of the find subprocess is read by the while command via <

查找子进程的输出由while命令通过<

IFS= read -d $'\0' -r file

This is the while condition:

这是while条件:

read

Read one line of input (from the find command). Returnvalue of read is 0 unless EOF is encountered, at which point while exits.

读取一行输入(从查找命令)。除非遇到EOF,否则read的Returnvalue为0,此时退出。

-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.

…作为空字符的分隔符(参见bash manpage中的引用)。之所以这样做,是因为我们在前面使用了null字符-print0。

-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.

该命令使用IFS运行,IFS是一个特殊的变量,它包含读取的字符,将输入分割为未设置的单词。因为我们不想分裂。

And inside the loop:

和内部的循环:

file_list=("${file_list[@]}" "$file")

Inside the loop, the file_list array is just grown by $file, suitably quoted.

在循环中,file_list数组仅由$file增长,并适当地引用。

#3


9  

arrayname=( $(find $1) )

I don't understand your loop question? If you look how to work with that array then in bash you can loop through all array elements like this:

我不明白你的循环问题?如果你看看如何使用这个数组,那么在bash中,你可以循环遍历所有的数组元素,比如:

for element in $(seq 0 $((${#arrayname[@]} - 1)))
do
        echo "${arrayname[$element]}"
done

#4


1  

This is probably not 100% foolproof, but it will probably work 99% of the time (I used the GNU utilities; the BSD utilities won't work without modifications; also, this was done using an ext4 filesystem):

这可能不是100%绝对可靠的,但它可能在99%的时间内工作(我使用GNU实用程序;如果没有修改,BSD实用程序将无法工作;而且,这是使用ext4文件系统完成的):

declare -a BASH_ARRAY_VARIABLE=$(find <path> <other options> -print0 | sed -e 's/\x0$//' | awk -F'\0' 'BEGIN { printf "("; } { for (i = 1; i <= NF; i++) { printf "%c"gensub(/"/, "\\\\\"", "g", $i)"%c ", 34, 34; } } END { printf ")"; }')

Then you would iterate over it like so:

然后你会这样重复:

for FIND_PATH in "${BASH_ARRAY_VARIABLE[@]}"; do echo "$FIND_PATH"; done

Make sure to enclose $FIND_PATH inside double-quotes when working with the path.

在使用路径时,请确保在双引号中包含$FIND_PATH。

#5


1  

Here's a simpler pipeless version, based on the version of user2618594

这里有一个更简单的无pi版本,基于user2618594版本

declare -a names=$(echo "("; find <path> <other options> -printf '"%p" '; echo ")")
for nm in "${names[@]}"
do
    echo "$nm"
done

#1


-5  

To loop through a find, you can simply use find:

要循环查找,只需使用find:

for file in "`find "$1"`"; do
    echo "$file" | cut -d/ -f6-
done

It was what I got from your question.

这是我从你的问题中得到的。

#2


51  

I want to cry.

我想哭。

In bash:

在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

file_list现在是一个包含查找“$1”结果的数组

What's special about "field 6"? It's not clear what you were attempting to do with your cut command.

第6场有什么特别之处?现在还不清楚你想用cut命令做什么。

Do you want to cut each file after the 6th directory?

你想要在第6个目录之后删除每个文件吗?

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?

但为什么“6”?我能假设你真的想要返回路径的最后一个元素吗?

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

Explanation of the bash program elements:

(One should probably use the builtin readarray instead)

(人们可能应该使用内置的readarray)

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.

查找并“在标准输出上打印完整的文件名,后跟一个空字符”。这一点很重要,因为稍后我们将用null字符分割这个输出。

<(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)

“进程替换”:find子进程的输出通过FIFO读取(即,find子进程的输出行为类似于此处的文件)

while ... 
done < <(find "$1" -print0)

The output of the find subprocess is read by the while command via <

查找子进程的输出由while命令通过<

IFS= read -d $'\0' -r file

This is the while condition:

这是while条件:

read

Read one line of input (from the find command). Returnvalue of read is 0 unless EOF is encountered, at which point while exits.

读取一行输入(从查找命令)。除非遇到EOF,否则read的Returnvalue为0,此时退出。

-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.

…作为空字符的分隔符(参见bash manpage中的引用)。之所以这样做,是因为我们在前面使用了null字符-print0。

-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.

该命令使用IFS运行,IFS是一个特殊的变量,它包含读取的字符,将输入分割为未设置的单词。因为我们不想分裂。

And inside the loop:

和内部的循环:

file_list=("${file_list[@]}" "$file")

Inside the loop, the file_list array is just grown by $file, suitably quoted.

在循环中,file_list数组仅由$file增长,并适当地引用。

#3


9  

arrayname=( $(find $1) )

I don't understand your loop question? If you look how to work with that array then in bash you can loop through all array elements like this:

我不明白你的循环问题?如果你看看如何使用这个数组,那么在bash中,你可以循环遍历所有的数组元素,比如:

for element in $(seq 0 $((${#arrayname[@]} - 1)))
do
        echo "${arrayname[$element]}"
done

#4


1  

This is probably not 100% foolproof, but it will probably work 99% of the time (I used the GNU utilities; the BSD utilities won't work without modifications; also, this was done using an ext4 filesystem):

这可能不是100%绝对可靠的,但它可能在99%的时间内工作(我使用GNU实用程序;如果没有修改,BSD实用程序将无法工作;而且,这是使用ext4文件系统完成的):

declare -a BASH_ARRAY_VARIABLE=$(find <path> <other options> -print0 | sed -e 's/\x0$//' | awk -F'\0' 'BEGIN { printf "("; } { for (i = 1; i <= NF; i++) { printf "%c"gensub(/"/, "\\\\\"", "g", $i)"%c ", 34, 34; } } END { printf ")"; }')

Then you would iterate over it like so:

然后你会这样重复:

for FIND_PATH in "${BASH_ARRAY_VARIABLE[@]}"; do echo "$FIND_PATH"; done

Make sure to enclose $FIND_PATH inside double-quotes when working with the path.

在使用路径时,请确保在双引号中包含$FIND_PATH。

#5


1  

Here's a simpler pipeless version, based on the version of user2618594

这里有一个更简单的无pi版本,基于user2618594版本

declare -a names=$(echo "("; find <path> <other options> -printf '"%p" '; echo ")")
for nm in "${names[@]}"
do
    echo "$nm"
done