2

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.

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

3 Answers 3

3
#!/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
0
4

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.

1
  • I will give a try to what you post, thanks for your help. Apr 23, 2014 at 17:59
0

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

2
  • 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. Apr 23, 2014 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, 2014 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .