1

I have a big bunch of directories, each with a series of .pdf files. Some of these files have successfully converted to .csv, some have not. An example directory listing using find ./:

./test5.pdf.csv
./test6.pdf
./test1.pdf.csv
./test3.pdf
./test1.pdf
./test3.pdf.csv
./test4.pdf.csv
./test7.pdf
./test2.pdf
./test5.pdf
./test4.pdf
./test2.pdf.csv
./test8.pdf

How would I return a list of files that have not successfully converted?

In other words, what are the files that don't have a .csv.pdf counterpart?

In this example, it would be test{6,7,8}.pdf.

2 Answers 2

5

Here's how it works with find:

find . -name "*.pdf" | while read F; do test -f "${F}.csv" || echo "$F"; done

Using find is preferable to shell globbing as it has no limitation on the number of arguments and it works with spaces and special characters.

1
  • Rad, this is totally what I was looking for. Just tested it, works great!
    – aendra
    Jan 23, 2014 at 20:52
3

I couldn't find a way to do it with find directly, but it's very easy using a for loop:

for file in *.pdf; do [ -f ${file}.csv ] || echo $file; done

If you need to run it at the top directory and have it recurse, you could use find like this:

for file in $(find . -iname '*.pdf'); do [ -f ${file}.csv ] || echo $file; done

which would output something like this:

./dir2/test7.pdf
./dir2/test6.pdf
./dir2/test8.pdf
./dir3/test7.pdf
./dir3/test6.pdf
./dir3/test8.pdf
./dir1/test7.pdf
./dir1/test6.pdf
./dir1/test8.pdf

Careful with that last one though, if you aren't sure there won't be any strange characters in the input filenames, make sure you set IFS environment variable accodingly:

OFS=$IFS
export IFS=$'\n'

Of course, from here on it only gets nastier.

I'm assuming you use bash.

1
  • This is a great, detailed answer if you have a less-than-stellar version of find (I think it's a bit different on Solaris) or don't like find -- I accepted the other answer because it's closer to my initial question, but this one is pretty terrific as well. Wish I could accept both!
    – aendra
    Jan 24, 2014 at 14:58

You must log in to answer this question.

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