0

I need to compare the contents of a file located in dir A with actual files in different directory. ex- directory A has a file test.txt , Item mentioned in test.txt and not present in directory B should be highlighted. im doing something like this but not working.. it is only searching last word from the file test.txt

#!/bin/sh
IFS=$'\n' dirA=$1 dirB=$2 
for x in $(cat < "$1"); do base_name="${x##/}" 
set -- "$dirB"/"$base_name"* 
  if [ -e "$1" ]; then 
    for y; do 
   echo "$base_name found in B as ${y##*/}" done 
  else 
     echo "$x not found in B" fi done.
Kusalananda
  • 333,661
  • I tested this program in my cygwin environment. It works well! Maybe it's bash setting problem? – JinChin Apr 11 '19 at 09:54
  • @JinChin Notice that the user is not executing this with bash, and that the code does not include anything that is specific to that shell. – Kusalananda Apr 11 '19 at 10:03

2 Answers2

0

Using diff may do the trick

diff -crs Dir1 Dir2

It will show you if the files are existing, same or different

with a grep on filename might be what you are looking for

PhLinuX
  • 31
  • The idea seems to be to verify some sort of manifest over files that are supposed to exist, not to compare two directories. – Kusalananda Apr 11 '19 at 09:16
  • i want to check file contents in another directory. example file 1 (located in /user/home) has a,b,c so i need to check if a,b,c is present as files in different folder (/etc) – Roops23 Apr 11 '19 at 09:17
  • @Kusalananda , yes that is correct, l'm looking for manifest over files that are supposed to exist – Roops23 Apr 11 '19 at 09:20
  • Ok, sorry, may be read a bit too fast. In that case you could use diff -crs ./file /path_to_file/DIr It also show you where are the differences – PhLinuX Apr 11 '19 at 09:52
0
#!/bin/sh

manifest=$1
topdir=$2

while IFS= read -r name; do
    pathname="$topdir/$name"

    if [ -e "$pathname" ]; then
        printf 'Found: %s\n' "$pathname" >&2
    else
        printf 'Not found: %s\n' "$pathname" >&2
    fi
done <"$manifest"

This script takes a manifest file as its first command line argument, and some directory path as its second argument.

It reads lines from the manifest and tests to see if the pathnames corresponding to those lines exists under the given directory.

Would you only want to test the base name of each name read from the file, then use

#!/bin/sh

manifest=$1
topdir=$2

while IFS= read -r name; do
    pathname="$topdir/$( basename "$name" )"

    if [ -e "$pathname" ]; then
        printf 'Found: %s\n' "$pathname" >&2
    else
        printf 'Not found: %s\n' "$pathname" >&2
    fi
done <"$manifest"

Related:

Kusalananda
  • 333,661
  • what is pathname="$topdir/$( basename "$name" )" in the above code.. it is showing opposite result Not found:/Users/dir2/a Not found: /Users/dir2/b Not found: /Users/dir2/c , but a ,b, c are present in dir2 as txt files. – Roops23 Apr 11 '19 at 09:49
  • @RoopakMurty The line creates a new value that is the directory path that is was given followed by the base name of the line read from the file. If the script says that /Users/dir2/a does not exist, well, then that pathname does not exist. The type of file is not taken into consideration. If you could describe a bit more, then maybe I would understand what you mean. – Kusalananda Apr 11 '19 at 09:55
  • @RoopakMurty Are you saying that /Users/dir2/a exists? Do you have spaces at the end of the lines in the file that you pass to the script? In that case, delete those spaces. – Kusalananda Apr 11 '19 at 09:57
  • yes a is a text file under /Users/dir2 that exists, actually I'm using git bash in windows to run this script, is this a problem ? – Roops23 Apr 11 '19 at 10:25
  • installed cygwin, and it works now. :-) – Roops23 Apr 11 '19 at 10:49
  • @RoopakMurty Sorry, what works? – Kusalananda Apr 11 '19 at 17:26
  • i'm using test.txt as manifest ,inside it given 4 lines a.txt , b.txt, c.txt, d.txt and in topdir i placed a.txt and b.txt files it is working partially, giving output as below. Found: /home/user/dir2/a.txt Not found: /home/user/dir2/ Found: /home/user/dir2/b.txt Not found: /home/user/dir2/ but when i'm using manifest file it is not able to find the path. it just says NOT Found: $TOPDIR/$Basename. my manifest file is like /s/a/b/filename.ext. I'm using cygwin, do i need to do any Path settings? – Roops23 Apr 12 '19 at 08:09
  • @RoopakMurty It's unclear what you have, both in terms of actual files and the contents of the text file that you want to read. Adding examples to the question itself would make it trivial to solve. Don't add clarifications in comments. – Kusalananda Apr 12 '19 at 09:34