kaneyip
kaneyip

Reputation: 1267

Terminal command 'find' read from txt files

I would like to find all .html files from a directory but to ignore some files.

$ find /Volumes/WORKSPACE/MyProject/ -name '*.html' ! -name 'MyFile.html' 

Above command ignore MyFile.html only. However i have a lists of files to be ignore and wanna to maintain the ignore file in a single txt file.

MyFile1.html    
MyFile1.html
MyFile2.html
MyFile2.html

Above is the txt file content.

I would like to have the list of files to be ignored maintain in a single txt files and use the txt file in find command.

Please advice. Thanks in advance.

Upvotes: 1

Views: 186

Answers (1)

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11593

You would have to build something like an array from the file and then use it in the find syntax. i.e.

#!/bin/bash
while IFS= read -r file; do
  arr+=(-name "$file" -o)
done < list_of_filenames_to_omit.txt

#remove trailing -o
unset arr[$((${#arr[@]}-1))]

if ((${#arr[@]}!=0)); then
  find /Volumes/WORKSPACE/MyProject/ -name '*.html' ! \( "${arr[@]}" \)
else
  #list_of_filenames_to_omit.txt is empty
  find /Volumes/WORKSPACE/MyProject/ -name '*.html' 
fi

Upvotes: 2

Related Questions