Question

In a directory, I have a bunch of files like file1.tex, file1.pdf, file2.tex, file2.pdf etc along with other files. I want, preferably a one liner in bash (to include in a make file as dependency), to

  1. list all tex files if their pdf versions are also available.
  2. list all pdf files if their tex versions are also available.

EDIT

I had tried

find . \( -name '*.pdf' -name '*.tex' \) 

but it did not work. I guess the above approach is useful if I want to find files with pdf or tex extensions by using the -o option between two name switches. thanks

suresh

Was it helpful?

Solution

The Solution

This isn't very readable--it would be much better to make this a script or shell function with some decent line wrapping--but you can force the loop to be a one-liner if you really want to.

# List TeX files with matching PDFs.
for file in *.pdf; do [ -f "${file/.pdf}.tex" ] && ls "${file/.pdf/.tex}"; done

# List PDFs with matching TeX files.
for file in *.tex; do [ -f "${file/.tex}.pdf" ] && ls "${file/.tex/.pdf}"; done

Validating the Solution

You can quickly test that the solution works properly with with some sample data.

$ touch file1.pdf file1.tex file2.pdf file2.tex file3.pdf file4.tex

$ for file in *.pdf; do [ -f "${file/.pdf}.tex" ] && ls "${file/.pdf/.tex}"; done
file1.tex
file2.tex

$ for file in *.tex; do [ -f "${file/.tex}.pdf" ] && ls "${file/.tex/.pdf}"; done
file1.pdf
file2.pdf

Note that in both cases, files without complements in the other format are silently ignored.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top