linux: find files from a list in txt, the files contain spaces

10,933

Solution 1

#!/bin/bash
while read -r FILE
do
  FOUND="$(find . -name "$FILE" -print -quit)"
  if [ "x$FOUND" != "x" ]
  then
    echo "FOUND: $FILE"
  else
    echo "NOT FOUND: $FILE"
  fi
done <filelist.txt

Solution 2

This is harder than it seems

while read -r file
do
    find . -name "$file"
done <txt

will find the files that exist and print the full path for them. Unfortunately find sets $? to 0 (SUCCESS) even if the file is not found.

Share:
10,933

Related videos on Youtube

Vamsi M
Author by

Vamsi M

I'm a systems administrator with advance knowledge in IT security.

Updated on September 18, 2022

Comments

  • Vamsi M
    Vamsi M almost 2 years

    I need to find files from a list in txt (i already have the txt with all the files, are separated with lines), the files contain spaces and the extension of the files are pdf, if you can suggest how to make an output of the command or script to another txt file.

    What I try (this one retrieve all the files in the directory and contain spaces, but only the existing ones, i need to find the non existing too):

    find . -type f -name *.pdf

    Thank's in advance for any help.

    • Florin Asăvoaie
      Florin Asăvoaie about 10 years
      You need to find non existing files?
    • Vamsi M
      Vamsi M about 10 years
      Hi Florin, i need to find existing and non existing files, thanks for your comment.
    • Florin Asăvoaie
      Florin Asăvoaie about 10 years
      How can you find a file that does not exist?
    • Vamsi M
      Vamsi M about 10 years
      I want the log of the command or script to say that the file doesn't exist
  • Vamsi M
    Vamsi M about 10 years
    Its correct I have the list of files and some of those are not yet present, i need to report which of those are there, and which not.
  • Vamsi M
    Vamsi M about 10 years
    I will give a try to what you post, thanks for your help.
  • Nicolaie
    Nicolaie about 10 years
    A bit of explanation regarding my answer: ALIST and BLISt are arrays. And comm -3 shows differences of the two contained in the second expression.