Skip to content
Advertisement

How to use find, ls commands to display file names based on entries in an array?

I have an array, that contain the wildcards/filenames tha may be present in a directory. How can I display the files in a directory, whose names matches with names in the array. By the way, if possible, without using for loop.

Ex:

find ./ -maxdepth 1 -type f -name $FILE_ARR[@]

ls $FILE_ARR[@]

Advertisement

Answer

The correct syntax is to use curly braces when accessing arrays:

ls ${file_array[@]}

For find, it’s a bit more complex, as -name only accepts one argument, not multiple.

find . -maxdepth 1 -name $(echo ${file_array[@]}| sed 's/ / -o -name /g')

Note that it doesn’t work if any path contains whitespace.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement