Home > Mobile >  how to read multiple paths from a file and then use the path in find commands
how to read multiple paths from a file and then use the path in find commands

Time:03-04

I have a scenario to read multiple paths in find command and to cover that i cannot enter every single path in find command. is there any way to read the paths from the file or to ask find command to read the path from the file and then execute the command to search for a file from that path which are mentioned in the read file.

command:

find path1 path2 path 30 -name *.jks 2>/dev/null

CodePudding user response:

Just do this in a shell script :

#!/bin/bash
input="/path/to/txt/file"
paths= ""

while IFS= read -r line
do
  paths="$paths $line"
done < "$input"

find $paths -name "*.txt" -print

You can replace -print by -exec to execute à task on each file found

CodePudding user response:

Using you can read the file into an array (one line/item in array), and expand it into the first arguments for find:

readarray -t files < files.txt
find "${files[@]}" -name '*.jks'

The -t flag given to readarray will trim the trailing newline.

  • Related