Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare outputs of two `find` commands

Tags:

grep

bash

I have a folder with sub-folders as below (for the sake of the question, I have simplified)

I am able to find the folders where check.lock files are present using find . -type f -name "check.lock"

and find the folders where local.lock files are present using find . -type f -name "local.lock"

As you can see variant2 is folders binary_test and component_test do NOT have the local.lock files (but they have the check.lock files).

How can I find the these (do not have to the variant names, the whole paths are sufficient). In other words, the output of find . -type f -name "check.lock" will be 9 lines & the output of find . -type f -name "local.lock" will be 6 lines & I want those 3 lines where the "local.lock" was not found.

so I want to take the output of find . -type f -name "check.lock" and then compare that with find . -type f -name "local.lock" and do a grep (with unit_test/variant1 or something like that) so that I get a smaller list to visually compare.

I tried { find . -type f -name "check.lock" && find . -type f -name "local.log"; } | grep "unit_test" but the output is strange (shows only one line with "check.lock"`

Any helpy is appreciated. If you can at least let me know how to pass out of 2 commands into grep, that would also help.

├── src
│   ├── unit_test
│   │   ├── variant1
│   │   │   ├── check.lock
│   │   ├── variant2
│   │   │   ├── check.lock
│   │   ├── variant3
│   │   │   ├── check.lock
│   ├── binary_test
│   │   ├── variant1
│   │   │   ├── check.lock
│   │   ├── variant2
│   │   │   ├── check.lock
│   │   ├── variant3
│   │   │   ├── check.lock
│   ├── component_test
│   │   ├── variant1
│   │   │   ├── check.lock
│   │   ├── variant2
│   │   │   ├── check.lock
│   │   ├── variant3
│   │   │   ├── check.lock
├── public
│   ├── unit_test
│   │   ├── variant1
│   │   │   ├── local.lock
│   │   ├── variant2
│   │   │   ├── local.lock
│   │   ├── variant3
│   │   │   ├── local.lock
│   ├── binary_test
│   │   ├── variant1
│   │   │   ├── local.lock
│   │   ├── variant2
│   │   ├── variant3
│   │   │   ├── local.lock
│   ├── component_test
│   │   ├── variant1
│   │   │   ├── local.lock
│   │   ├── variant2
│   │   ├── variant3
│   │   │   ├── local.lock

like image 502
moys Avatar asked Oct 18 '25 01:10

moys


2 Answers

You can try this:

find src -type f -name check.lock -exec sh -c '
for p; do
  sub=${p#*/}; sub=${sub%/*}
  if ! test -f "public/$sub/local.lock"; then
    echo "$sub"
  fi
done' sh {} +
like image 99
oguz ismail Avatar answered Oct 20 '25 23:10

oguz ismail


As you need to process two different rootdirs in //, I'm not sure you'll be able to avoid spawning a shell from your find command. So, if your bash supports globstar then a simple for loop with some parameter expansions should be enough for the task:

#!/bin/bash
shopt -s globstar nullglob

for file1 in src/**/check.lock
do
    dir1=${file1%/*}
    dir2=public/${dir1#*/}
    file2=${dir2}/local.lock

    ! [[ -e "$file2" ]] && printf '%s\n' "$dir2" 
done
like image 28
Fravadona Avatar answered Oct 20 '25 23:10

Fravadona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!