2

I am using Debian Linux. I'm a newbie. I'll do my best to ask in the simplest way I know.

I have a pretty deep tree of directories on a drive that contain thousands of .tif files and .txt files. I'd like to recursively find (list) all .txt files that do not have a matching .tif file (basename). The .tif files and .txt files are also located in separate directories throughout the tree.

In simple form it could look like this...

directory1: hf-770.tif, hf-771.tif, hf-772.tif

directory2: hf-770.txt, hf-771.txt, hf-771.txt, hr-001.txt, tb-789.txt

I need to find (list) hr-001.txt and tb-789.txt as they do not have a matching .tif file. Again the directory tree is quite deep with multiple sub-directories throughout.

I researched and experimented with variations of the following commands but cannot seem to make it work. Thank you so much.

find -name "*.tif" -name "*.txt" | ls -1 | sed 's/\([^.]*\).*/\1/' | uniq
Robert
  • 7,394
  • 40
  • 45
  • 64
allenjm
  • 23
  • 2

1 Answers1

1

You can write a shell script for this:

#!/bin/bash
set -ue
while IFS= read -r -d '' txt
do
    tif=$(basename "$txt" | sed s/\.txt$/.tif/)
    found=$(find . -name "$tif")
    if [ -z "$found" ]
    then
        echo "$txt has no tif"
    fi
done < <(find . -name \*.txt -print0)

This has a loop over all .txt files it finds in the current directory or below. For each found file, it replaces the .txt extension with .tif, then tries to find that file. If it cannot find it (returned text is empty), it prints the .txt file name.

robert@saaz:$ tree
.
├── bar
│   └── a.txt
├── foo
│   ├── a.tif
│   ├── b.tif
│   ├── c.tif
│   └── d.txt
└── txt-without-tif

2 directories, 6 files
robert@saaz:$ bash txt-without-tif
./foo/d.txt has no tif
Robert
  • 7,394
  • 40
  • 45
  • 64
  • Did as you did.. ran the script on an example and it ran without a glitch. Running the script right now. Approx 600k files throughout multiple directories. Already receiving verbose output of .txt files that exist without a matching .tif. I will followup once the script has finished. Your programming expertise is greatly appreciated! – allenjm Nov 26 '18 at 00:13
  • I apologize for the delayed reply back. Your script worked perfectly and thank you. I tried to add a plus check to your response but seems I need 15 posts to change the score of your response. So if someone out there has a few minutes to give Robert a thumbs up I'd really appreciate it. Test as he did if you must. Thank you! – allenjm Dec 09 '18 at 21:59
  • I didn't realize there's a difference between clicking on the up arrow and the check mark. I did as you suggested. Again thanks! The newbie is learning. :) – allenjm Dec 11 '18 at 00:58