Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
You need to find non existing files? –  Florin Asavoaie Apr 23 at 14:53
    
Hi Florin, i need to find existing and non existing files, thanks for your comment. –  ravasquezgt Apr 23 at 14:54
1  
How can you find a file that does not exist? –  Florin Asavoaie Apr 23 at 15:01
    
I want the log of the command or script to say that the file doesn't exist –  ravasquezgt Apr 23 at 17:00

3 Answers 3

up vote 1 down vote accepted
#!/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
share|improve this answer
    
Thanks works like a charm. –  ravasquezgt Apr 23 at 20:21

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|improve this answer
    
I will give a try to what you post, thanks for your help. –  ravasquezgt Apr 23 at 17:59

May I try to understand your question.

You have a list that contains file names that your are searching for. And you want a list of files that are not yet present?

Is that correct?

So assuming TEST is the file containig your search-list then you might use this:

ALIST=$(cat TEST | sort);BLIST=$(ls -1 | sort);comm -3 <( echo "${ALIST[*]}" ) <( echo "${BLIST[*]}" )

cheers Nick

share|improve this answer
    
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. –  ravasquezgt Apr 23 at 17:58
    
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. –  Nicolaie Apr 24 at 16:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.