Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store the output of find command in an array [duplicate]

Tags:

arrays

bash

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-
like image 768
Gábor Varga Avatar asked Nov 21 '11 14:11

Gábor Varga


People also ask

How do you store output of find command in array?

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.

How do you store output of find command in variable in UNIX?

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

What does find do in bash?

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.


1 Answers

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

Explanation of the bash program elements:

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

like image 82
sorpigal Avatar answered Sep 18 '22 16:09

sorpigal